Covidografia-Backend

Covidografia-Backend
Covidografia 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.

Installation

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

Make a copy of the env.local

Create a local .env file and copy the contents of the env.local template file.

There you'll find a few variables that need to be set for your server to run locally, like JWT secret, database credentials and so on.

Install the dependencies and devDependencies and start the server:

$ yarn
$ yarn start

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 or Google account on http://localhost:3000/login/google;
  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 environment and check other variables!

Serverless Usage

The project is designed to run into a serverless environment although it can also run locally. For deployment the project to a serverless platform, like AWS Lambda, follow the steps below.

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 prd --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 @fernanri for the work and efforts to bootstrap this service!

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

License

MIT