Trackovid19-Backend

Trackovid19-Backend
Trackovid19 API

Built with ❤︎ by the tech4covid19.org community

Table of Contents

Guidelines

  • YARN as Package Manager
  • ASYNC/AWAIT Promise Style
  • Database is PostgreSQL
  • Sequelize as ORM using ASYNC Client
  • FASTIFY as base framework (we need something simple yet speedy)
  • JWT as Authentication and Authorization System
  • JSON requests & responses

This code is running in a serverless environment. You cannot rely on your server being 'up' in the sense that you can/should not use in-memory sessions, web sockets, etc. You are also subject to provider specific restrictions on request/response size, duration, etc.

Instalation

You will need atleast nodejs version 12 LTS to run this server.

Generate Private and Public keys to be used with JWT:

$ openssl genrsa -out private.key 2048
$ openssl rsa -in private.key -outform PEM -pubout -out public.key
$ mv private.key public.key plugins/certs

Install the dependencies and devDependencies and start the server:

$ yarn
$ yarn start

Generate DB Models from Remote deployed PGSQL DB at AWS:

$ yarn generate-db

After the task is done, you will see the models inside models folder with the current definition from the remote DB.

Side Notes

You will see a weird snippet inside package.json:

"peerDependencies": {
	"sequelize": "^5.21.5"
}

This is to handle external dependencies for 3rd party modules, otherwise we will receive security warnings from deprecated / insecure modules.

Documentation

  1. Login using your facebook account on: http://localhost:3000/login/facebook;
  2. You will receive a JWT token, use it as a Bearer Token in all the authenticated requests;
  3. Please load the insomnia.json file into your Insomnia Rest Client and call the routes or consult https://trackcovid19.docs.apiary.io;

Do not forget to set the Bearer token on insomnia!

Serverless Usage

Install the framework:

$ npm install -g serverless
$ serverless plugin install --name serverless-stage-manager

Verify its installation:

$ serverless --version

In dev, use:

$ yarn start

Configure your environment: Credentials were given before, use 'lambda' user.

$ export AWS_ACCESS_KEY_ID=
$ export AWS_SECRET_ACCESS_KEY=
$ export AWS_PROFILE="default"
$ touch ~/.aws/config

Paste inside ~/.aws/config:

[default]
region=eu-west-1

For dev deploy, use:

$ sls deploy --stage dev --db_pass 'PASSWORD'

For prd deploy, use:

$ sls deploy --stage dev --db_pass 'PASSWORD'

You should see

Serverless: Uploading artifacts...
Serverless: Uploading service trackovid19-backend.zip file to S3 (2.78 MB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
..............

Serverless: Stack update finished...
Service Information
service: trackovid19-backend
stage: dev
region: eu-west-1
stack: trackovid19-backend-dev
resources: 12
api keys:
None
endpoints:
ANY - https://2ceesuwr5l.execute-api.eu-west-1.amazonaws.com/dev/
ANY - https://2ceesuwr5l.execute-api.eu-west-1.amazonaws.com/dev/{proxy+}
functions:
server: trackovid19-backend-dev-server
layers:
None
Serverless: Removing old service artifacts from S3...
Serverless: Run the "serverless"  command to setup monitoring, troubleshooting and testing.

Debug

$ sls logs --stage prd -f app -t
$ sls logs --stage dev -f app -t

Local Development

A docker compose yml file was added for launching a local postgres database and a web based admin app (adminer).

You can check/change all db parameters by editing the docker compose file:

$ docker/database/tracovid.yml

You can use the convenience start/stop scripts for starting/stopping the database, but first you need to add execute permissions:

$ chmod +x docker/database/*.sh

Then for starting the local database:

$ docker/database/start.sh

You can then go to your browser and access the database via adminer at http://localhost:8080

Stopping the local database:

$ docker/database/stop.sh

See also

Authors

Special thanks to @victorfern91 @hugoduraes @ludwig801 @palminha @lcfb91 @zepcp @jcazevedo @cchostak for the work and efforts to bootstrap this service!

Feel free to change / improve / delete everything you want!

License

MIT