Code Monkey home page Code Monkey logo

cloudflare-workers-keypair's Introduction

REST API with a Cloudflare Worker and a MongoDB Atlas Cluster

This project explains how to build a REST API in a Cloudflare worker using MongoDB Realm Web SDK and a MongoDB Atlas cluster to store the data.

How it Works

  • The MongoDB Atlas cluster stores the data in the cloudflare.todos collection.
  • A MongoDB Realm App manages the authentication and the collection access rules.
  • A Cloudflare worker uses the Realm Web SDK to authenticate and retrieve the data that is then exposed with a REST API.

Prerequisites

  • MongoDB Cloud account.
  • MongoDB Atlas Cluster (M0 is fine).
  • MongoDB Realm Application created & deployed.
    • with Authentication API Keys turned on + an API key created.
    • with a rule on the collection cloudflare.todos with a role "owner" with read and write access on all the fields, applied when {"owner": "%%user.id"}.
  • Cloudflare account (free plan is fine) with a *.workers.dev subdomain.

To deploy & test the API we need:

  • The Realm Application ID (top left corner).
  • The Realm authentication API key (in Authentication tab > API Keys).
  • The Cloudflare account login/password.
  • The Cloudflare account ID (in Workers tab > Overview).
  • The Cloudflare *.workers.dev subdomain (in Workers tab > Overview).

Build and Deploy

Clone this repository

git clone [email protected]:mongodb-developer/cloudflare-worker-rest-api-realm-atlas.git
cd cloudflare-worker-rest-api-realm-atlas

Edit the file wrangler.toml

  • replace CLOUDFLARE_ACCOUNT_ID by your real Cloudflare account ID.
  • replace MONGODB_REALM_APPID by your real MongoDB Realm App ID.

If you want to use the bash files to test the REST API, edit the file api_tests/variables.sh:

  • replace YOUR_SUBDOMAIN so the final worker URL matches yours.
  • replace YOUR_REALM_AUTH_API_KEY with your Realm auth API key.

Run the following commands:

$ npm i @cloudflare/wrangler -g
$ wrangler login
$ wrangler publish
$ cd api_tests
$ ./post.sh "Write a good README.md for Github"
$ ./post.sh "Commit and push"
$ ./findAll.sh
$ ./findOne.sh <OBJECT_ID> # replace with an _id from the previous command
$ ./patch.sh <OBJECT_ID> true
$ ./findAll.sh # note that done=true now on your todo
$ ./deleteOne.sh <OBJECT_ID>
$ ./findAll.sh # only one left

You can also navigate to your MongoDB Atlas Cluster and browse your collection cloudflare.todos to confirm the above tests.

Authors

cloudflare-workers-keypair's People

Contributors

rohsaurus avatar dependabot[bot] avatar

Watchers

 avatar

cloudflare-workers-keypair's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.