Skip to content

Simple serverless authenticated stocks api deployed to Cloudflare Workers with containerized PostgREST endpoint and database

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE_APACHE
MIT
LICENSE_MIT
Notifications You must be signed in to change notification settings

betolabrada/stocks-api

Stocks REST API

A simple REST API that manages stock information.

Source code is contained at index.js

Dockerizing PostgREST API

The application connects to a PostgREST endpoint which is containerized using docker-compose with postgres, postgrest and cloudfare as pulled images. Port is exposed using cloudflared service. Follow the steps on this repo for more information.

The application communicates with this endpoint via postgrest-js library

Authentication

We have two types of users (admin and anonymous) those with admin role can create and update stock information. Implemented a JWT middleware solution that ensures this.

Cloudflare Workers + Wrangler

Project was generated using wrangler.

wrangler generate projectname https://github.com/cloudflare/worker-template

Further documentation for Wrangler can be found here.

Endpoints

GET list of stocks

$ curl 'https://stocks-api.albertolabrada99.workers.dev/api/stocks'

GET specific stock

$ curl 'https://stocks-api.albertolabrada99.workers.dev/api/stocks/<id-of-stock>'

POST create new stock (protected with jwt)

$ curl 'https://stocks-api.albertolabrada99.workers.dev/api/stocks?accessToken=<accessTokenString>' -X POST -H "Content-Type: application/json" \
-d '{ "ticker": "Ticker", "name": "Microsoft", "price": 143.45, "category": "Technology" }'

PUT update stock (protected with jwt)

$ curl 'https://stocks-api.albertolabrada99.workers.dev/api/stocks/<id-of-stock>?accessToken=<accessTokenString>' -X POST -H "Content-Type: application/json" \
-d '{ "ticker": "Ticker", "name": "Microsoft", "price": 234.45, "category": "Technology" }'

POST login (verify with db). Returns token to access protected requests

$ curl 'https://stocks-api.albertolabrada99.workers.dev/api/auth/login' -X POST -H "Content-Type: application/json" \
-d '{ "username": "betolabrada", "password": "betolabrada"'

About

Simple serverless authenticated stocks api deployed to Cloudflare Workers with containerized PostgREST endpoint and database

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE_APACHE
MIT
LICENSE_MIT

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published