XronTrix10 / Cloudflare-RESTful-KV

Serverless RESTful API Built for Cloudflare Workers. uses Cloudflare KV as Database

Home Page:https://restapi-kv-demo.xrontrix.workers.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Serverless RESTful API

Built for Cloudflare Workers. uses Cloudflare KV as Database

Blazing Fast Serverless πŸ”₯


Features πŸš€

  • TOKEN based Authentication System
  • Every type Error Handling
  • Real time http response code

Authentication

Need to pass Authorization header along with AUTH_TOKEN. Otherwise it will return Unauthorized (http - 401)

Example

      curl -x POST https://backend.workers.dev/faculty
         -H 'Authorization: AUTH_TOKEN'  
         -d '{name: "Xron Trix"}'

Available HTTP Responses

  • 200 - OK
  • 204 - No Content
  • 400 - Bad Request
  • 401 - Unauthorized
  • 404 - Not Found
  • 405 - Method not Allowed
  • 409 - Data conflicts
  • 422 - Unprocessable Entity
  • 500 - Server error

Allowed HTTP Methods

  • GET
  • POST
  • PUT
  • DELETE

Api Routes

  • / - Check if Server is Online
  • /faculty - Access Faculty Members Data
  • /member - Access Regular Members Data
  • /event - Access Event Data

JSON Data Formats

Unique ID Will be Auto Added on Creation on Each Data

Each Field is Required on POST request

  • Faculty

     {
        name: "String",
        role: "String",
        image: "String",
        mobile: "String"
     }
  • Member

     {
        name: "String",
        role: "String",
        image: "String",
        mobile: "String",
        roll: "String"
     }
  • Event

     {
        title: "String",
        page: "String",
        image: "String",
        teams: [Array]   # Optional in Creation
     }

CRUD Operation

Accepts

  • POST /<API_ROUTE> {json in body} - Create Single Data
  • GET /<API_ROUTE> - Read All Data
  • GET /<API_ROUTE>/<ID> - Read Single Data
  • PUT /<API_ROUTE>/<ID> - Update Single Data
  • DELETE /<API_ROUTE> - Delete All Data
  • DELETE /<API_ROUTE>/<ID> - Delete Single Data

Returns

  • Create

    • Returns http - 200 ID and Collection of new data in JSON on Success
    • Returns http - 409 on Data Conflict
    • Returns http - 422 on Unprocessable Entity
    • Returns http - 500 on Creation error
  • Read

    • Returns http - 200 requested data in JSON on Success
    • Returns http - 404 on Not Found
  • Update

    • Returns http - 200 Request Permitted! on Success
    • Returns http - 404 on Not Found
    • Returns http - 422 on Unprocessable Entity
    • Returns http - 500 on Update error
  • Delete

    • Returns http - 200 Request Permitted! on Success
    • Returns http - 404 on Not Found
    • Returns http - 500 on Update error

Deploy Step by Step πŸ¦€

1. Set up Cloudflare

  • Create a Cloudflare Account if haven't already πŸ™‚

  • Create a subdomain for your workers pages.

    Your Projects will be visible as https://project.SUB_DOMAIN.workers.dev

2. Set up Project

  • Install node.js if haven't already πŸ™‚

  • Install Wrangler as

    npm install wrangler --save-dev
  • Login with Cloudflare Account

    wrangler login
  • Clone This Repository

    git clone https://github.com/XronTrix10/Cloudflare-RESTful-KV.git
  • Rename wrangler.example.toml file to wrangler.toml

  • Rename example.dev.vars file to .dev.vars

  • Put a secret Auth Token in .dev.vars file. This Token will be used to Authenticate You with Your API

  • Put the auth token in Wrangler as well

    wrangler secret put AUTH_TOKEN

    Enter The Same Token you Placed inside .dev.vars file

  • Install dependencies

    npm install
  • Create a KV Namespace in Cloudflare

    wrangler kv:namespace create YOUR_NAMESPACE

    NOTE: Replace your own desired Database Name with YOUR_NAMESPACE

    On Hitting Enter, an ID of the created namespace will be returned.

  • Put Binding in wrangler.toml file

    • Replace <NAMESPACE_ID> with the ID you got on namespace Creation

    NOTE: ❌ Don't Edit the Binding Name binding = "DATABASE"

3. Deploy Project

  • To Deploy/Test in Local

    wrangler dev
  • To Deploy in Cloudflare

    wrangler deploy

    You can change the project name by editing the name field from wrangler.toml file before deploying your project


Hosted Demo 🐬

Live @Cloudflare

Test

curl https://restapi-kv-demo.xrontrix.workers.dev

Returns

Server is up and running!

About

Serverless RESTful API Built for Cloudflare Workers. uses Cloudflare KV as Database

https://restapi-kv-demo.xrontrix.workers.dev


Languages

Language:JavaScript 100.0%