Skip to content

Securelay API serverless implementation for hosting on Vercel. Uses Redis as DB.

License

Notifications You must be signed in to change notification settings

securelay/api-serverless-redis-vercel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

About

This is a serverless, NodeJS implementation of the Securelay API, using Redis as database. It is configured to be hosted on Vercel's Fluid Compute out of the box. However, with a few variations, this implmentation may be run on any serverless platform such as AWS Lambda, provided a Redis DB with REST API, such as offered by Upstash, can be used.

How to host on Vercel

Using GUI

  • Create a Vercel account.
  • Get Redis REST API in the form of Upstash.
  • Import this project and deploy it by following this tutorial.
  • Note: Before deploying, set the environment variables through Vercel's Project Settings page by following the template provided in example.env.
  • Note: Before deploying, enable Fluid Compute.

Using CLI

  • Create a Vercel account.
  • Get Redis REST API in the form of Upstash.
  • Set the environment variables through Vercel's Project Settings page by following the template provided in example.env.
  • Enable Fluid Compute.
  • Get vercel CLI: npm i -g vercel.
  • Clone this repo: git clone https://github.com/securelay/api-serverless-redis-vercel.
  • cd api-serverless-redis-vercel.
  • vercel login.
  • Deploy locally and test: vercel dev.
  • If everything is working fine, deploy to production on Vercel: vercel --prod.

Note on Redis

This implementation, given its serverless nature, uses the REST API provided by Upstash for accessing Redis.

One database (i.e. user-account in Upstash), is used as the main silo, whereas another is used as cache, and also for rate-limiting. If you can manage a large enough Redis database, you can use it for both the above purposes, simply by providing its credentials for both main and cache in the environment variables. If using a single database, the dbKeyPrefix used in helper.js prevents key collisions.

Sharding

If using free-tiers of Upstash, multiple accounts may be used to scale up the main silo. User data may then be distributed among the shards based on, say, the first letter of <random> of the Securelay public key. E.g. if number of shards = 5, and the key is 'D', the shard index would be $D_{base64url}\ \% \ 5$.

Note on CDN

This implementation uses a GitHub repo for storing CDN data. jsDelivr is then used to serve the content through its CDN.

To this aim, create a GitHub repo and a fine-grained access token GITHUB_PAT with write access.

Contact

If you have any queries, comments or feedback, please get in touch.