covidgreen/covid-green-backend-api

JavaScript

101

212 commits

updated Aug 17, 2022

See the code

README

Exposure Notification API Service

Local Development: Running the API and Database

The backend API consists of both a Fastify server and a PostgreSQL database. To get these running will require the following steps. You will need Docker installed to run the database, and you may use Docker to run the API service as well.

You have two options if you want to run the API and database locally: run everything inside Docker or run just the database inside Docker. It's up to you whether you like doing it one way or the other. If the API doesn't appear to be starting correctly outside of a Docker then try running everything inside Docker - the issue might just be a local environment thing.

In either case, basic setup is necessary before anything else. Please run these commands to install dependencies and create a basic environment configuration. ​

$> npm install
$> npm run create:env

Running API Outside Docker

You can run the API outside of Docker but the database still requires Docker.

First we'll start and setup the database.

$> npm run db:up
   # Postgres database is started

$> npm run db:migrate
   # Migration script is run to setup the tables and indexes

Next, we'll want to start the API service itself

$> npm run start:dev
[nodemon] 2.0.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node .`

[1596743208358] INFO  (80 on e1655c760ecf): Server listening at http://0.0.0.0:5000
[1596743208359] INFO  (80 on e1655c760ecf): Server running at: http://0.0.0.0:5000

At this point the API service is up and listening on port 5000. Editing files will cause nodemon to restart the service.

Running API with Docker

You can also run the API with Docker as well. This is recommended if running outside of Docker fails for unexpected local environment or dependency conflicts. For example, we've seen issues running the API with Node 14.x as it's only been certified with Node 12.x.

You can start everything with one single command.

$> npm run system:up
....
Starting covid-green-db            ... done
Starting covid-green-db-migrations ... done
Starting covid-green-api           ... done
Attaching to covid-green-db, covid-green-db-migrations, covid-green-api
....
covid-green-db               | 2020-08-06 20:03:49.104 UTC [1] LOG:  database system is ready to accept connections
....
covid-green-db-migrations    | > while ! node lib/migrate.js; do sleep 1; done && npm run db:check:readiness
covid-green-db-migrations    | No migrations run for schema "public". Already at the latest one.
covid-green-db-migrations    | Migration done
covid-green-db-migrations    |
covid-green-db-migrations    | > exposure-notification-api@1.0.0 db:check:readiness /covid-green
covid-green-db-migrations    | > node lib/is-database-ready.js
covid-green-db-migrations    |
covid-green-db-migrations    | Database is Ready: Version 2
covid-green-db-migrations exited with code 0
....
covid-green-api              | > npm run db:wait:readiness && npm run start:dev
....
covid-green-api              | Database is Ready: Version 2
....
covid-green-api              | [nodemon] 2.0.4
covid-green-api              | [nodemon] to restart at any time, enter `rs`
covid-green-api              | [nodemon] watching path(s): *.*
covid-green-api              | [nodemon] watching extensions: js,mjs,json
covid-green-api              | [nodemon] starting `node .`
covid-green-api              | [1596744238998] INFO  (72 on e1655c760ecf): Server listening at http://0.0.0.0:5000
covid-green-api              | [1596744238999] INFO  (72 on e1655c760ecf): Server running at: http://0.0.0.0:5000

And again, at this point the API service is up and listening on port 5000. You can confirm this with docker ps

$> docker ps
IMAGE                                     PORTS                    NAMES
covid-green-backend-api_covid-green-api   0.0.0.0:5000->5000/tcp   covid-green-api
postgres:10.4-alpine                      0.0.0.0:5432->5432/tcp   covid-green-db

​ Editing files outside the container will cause nodemon to restart the service.

Backend API Development

​ There are a number of handy commands you can run to help with development. ​

CommandAction
npm run start:devRun the server in dev mode, automatically restarts on file change
npm run create:envCreate a new .env file
npm testRun unit tests
npm run test:watchRun backend tests in watch mode, running on changed test files
npm run db:migrateRun database migrations.
npm run db:upRun the database server
npm run db:downShutdown the database server
npm run db:deleteDelete the database server. You will need to run db:up and db:migrate again.
npm run lintRun eslint
npm run lint:fixRun eslint in fix mode
api:start:when-readyStart the API only after the database is ready and migrated
db:migrate:until-doneKeep trying to migrate the database until it completes successfully
db:check:readinessReturn 0 rcode if the database is migrated and ready
db:wait:readinessWait until the database is migrated and ready
system:upStart everything using docker-compose
system:downStop everything using docker-compose
system:nukeStop and delete everything using docker-compose

Configuration & Secrets

All the endpoints in this service use varied sets of configuration settings and secrets.

Non Secret Settings

.envAWS Property KeyDescription
AWS_REGIONNoneAWS region where we're running
CONFIG_VAR_PREFIXNonePrefix of all configuration property and secret names
NODE_ENVNoneSome string with env name in it - Some string with 'production' in it means running in production
API_HOSTapi_hostName of host API is running on
API_PORTapi_portPort API is listening on
ENABLE_CALLBACKenable_callbackBoolean indicating whether feature is turned on
ENABLE_CHECK_INenable_check_inBoolean indicating whether feature is turned on
ENABLE_METRICSenable_metricsBoolean indicating whether feature is turned on
ENABLE_NOTICESenable_self_isolation_noticesBoolean indicating whether feature is turned on
LOG_LEVELlog_levelStandard logging level setting
CORS_ORIGINcors_originBoolean indicating something about CORS
DB_HOSTdb_hostConnection host for database (writing/reading)
DB_READ_HOSTdb_read_hostConnection host for database (reading)
DB_PORTdb_portPort the database is listening on
DB_DATABASEdb_databaseName of database to connect to
DB_SSLdb_sslBoolean indicating whether database connection should be SSL
DEFAULT_REGIONdefault_regionDefault Covid region
REFRESH_TOKEN_EXPIRYsecurity_refresh_token_expiry??
EXPOSURE_LIMIT????
CODE_LIFETIME_MINSsecurity_code_lifetime_mins??
TOKEN_LIFETIME_MINSsecurity_token_lifetime_mins??
UPLOAD_TOKEN_LIFETIME_MINSupload_token_lifetime_mins??
VERIFY_RATE_LIMIT_SECSsecurity_verify_rate_limit_secs??
CALLBACK_QUEUE_URLcallback_url??
NOTICES_QUEUE_URLself_isolation_notices_url??
ASSETS_BUCKETs3_assets_bucket??
CALLBACK_RATE_LIMIT_SECSsecurity_callback_rate_limit_secs# of seconds required before a user can request another callback
CALLBACK_RATE_LIMIT_REQUEST_COUNTsecurity_callback_rate_limit_request_count# of callback requests allowed before rate limit goes into effect
NOTICES_RATE_LIMIT_SECSsecurity_notices_rate_limit_secs# of seconds required before a user can request another notice unique key

Secret Settings

.envAWS Secret KeyFieldMeaning
DB_USERrds-read-write-createusernameDatabase username
DB_PASSWORDrds-read-write-createpasswordDatabase password
ENCRYPT_KEYencryptkeyKey used to encrypt things
JWT_SECRETjwtkey??
DEVICE_CHECK_KEY_IDdevice-checkkeyId??
DEVICE_CHECK_KEYdevice-checkkey??
DEVICE_CHECK_TEAM_IDdevice-checkteamId??
DEVICE_CHECK_PACKAGE_NAMEdevice-checkapkPackageName??
DEVICE_CHECK_PACKAGE_DIGESTdevice-checkapkDigestSha256??
DEVICE_CHECK_CERTIFICATE_DIGESTdevice-checkapkCertificateDigestSha256??
DEVICE_CHECK_ROOT_CAdevice-checksafetyNetRootCa??
DEVICE_CHECK_TIME_DIFF_THRESHOLD_MINSdevice-checktimeDifferenceThresholdMins??

Team

Lead Maintainers

Core Team

Contributors

Past Contributors

Hosted By

Linux Foundation Public Health

Acknowledgements

License

Copyright (c) 2020 HSEIreland Copyright (c) The COVID Green Contributors

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Contributors

colmharte

66 commits

dennisgove

56 commits

jackmurdoch

28 commits

segfault

25 commits

covidgreen/covid-green-backend-api

JavaScript

101

212 commits

updated Aug 17, 2022

See the code

README

Exposure Notification API Service

Local Development: Running the API and Database

The backend API consists of both a Fastify server and a PostgreSQL database. To get these running will require the following steps. You will need Docker installed to run the database, and you may use Docker to run the API service as well.

You have two options if you want to run the API and database locally: run everything inside Docker or run just the database inside Docker. It's up to you whether you like doing it one way or the other. If the API doesn't appear to be starting correctly outside of a Docker then try running everything inside Docker - the issue might just be a local environment thing.

In either case, basic setup is necessary before anything else. Please run these commands to install dependencies and create a basic environment configuration. ​

$> npm install
$> npm run create:env

Running API Outside Docker

You can run the API outside of Docker but the database still requires Docker.

First we'll start and setup the database.

$> npm run db:up
   # Postgres database is started

$> npm run db:migrate
   # Migration script is run to setup the tables and indexes

Next, we'll want to start the API service itself

$> npm run start:dev
[nodemon] 2.0.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node .`

[1596743208358] INFO  (80 on e1655c760ecf): Server listening at http://0.0.0.0:5000
[1596743208359] INFO  (80 on e1655c760ecf): Server running at: http://0.0.0.0:5000

At this point the API service is up and listening on port 5000. Editing files will cause nodemon to restart the service.

Running API with Docker

You can also run the API with Docker as well. This is recommended if running outside of Docker fails for unexpected local environment or dependency conflicts. For example, we've seen issues running the API with Node 14.x as it's only been certified with Node 12.x.

You can start everything with one single command.

$> npm run system:up
....
Starting covid-green-db            ... done
Starting covid-green-db-migrations ... done
Starting covid-green-api           ... done
Attaching to covid-green-db, covid-green-db-migrations, covid-green-api
....
covid-green-db               | 2020-08-06 20:03:49.104 UTC [1] LOG:  database system is ready to accept connections
....
covid-green-db-migrations    | > while ! node lib/migrate.js; do sleep 1; done && npm run db:check:readiness
covid-green-db-migrations    | No migrations run for schema "public". Already at the latest one.
covid-green-db-migrations    | Migration done
covid-green-db-migrations    |
covid-green-db-migrations    | > exposure-notification-api@1.0.0 db:check:readiness /covid-green
covid-green-db-migrations    | > node lib/is-database-ready.js
covid-green-db-migrations    |
covid-green-db-migrations    | Database is Ready: Version 2
covid-green-db-migrations exited with code 0
....
covid-green-api              | > npm run db:wait:readiness && npm run start:dev
....
covid-green-api              | Database is Ready: Version 2
....
covid-green-api              | [nodemon] 2.0.4
covid-green-api              | [nodemon] to restart at any time, enter `rs`
covid-green-api              | [nodemon] watching path(s): *.*
covid-green-api              | [nodemon] watching extensions: js,mjs,json
covid-green-api              | [nodemon] starting `node .`
covid-green-api              | [1596744238998] INFO  (72 on e1655c760ecf): Server listening at http://0.0.0.0:5000
covid-green-api              | [1596744238999] INFO  (72 on e1655c760ecf): Server running at: http://0.0.0.0:5000

And again, at this point the API service is up and listening on port 5000. You can confirm this with docker ps

$> docker ps
IMAGE                                     PORTS                    NAMES
covid-green-backend-api_covid-green-api   0.0.0.0:5000->5000/tcp   covid-green-api
postgres:10.4-alpine                      0.0.0.0:5432->5432/tcp   covid-green-db

​ Editing files outside the container will cause nodemon to restart the service.

Backend API Development

​ There are a number of handy commands you can run to help with development. ​

CommandAction
npm run start:devRun the server in dev mode, automatically restarts on file change
npm run create:envCreate a new .env file
npm testRun unit tests
npm run test:watchRun backend tests in watch mode, running on changed test files
npm run db:migrateRun database migrations.
npm run db:upRun the database server
npm run db:downShutdown the database server
npm run db:deleteDelete the database server. You will need to run db:up and db:migrate again.
npm run lintRun eslint
npm run lint:fixRun eslint in fix mode
api:start:when-readyStart the API only after the database is ready and migrated
db:migrate:until-doneKeep trying to migrate the database until it completes successfully
db:check:readinessReturn 0 rcode if the database is migrated and ready
db:wait:readinessWait until the database is migrated and ready
system:upStart everything using docker-compose
system:downStop everything using docker-compose
system:nukeStop and delete everything using docker-compose

Configuration & Secrets

All the endpoints in this service use varied sets of configuration settings and secrets.

Non Secret Settings

.envAWS Property KeyDescription
AWS_REGIONNoneAWS region where we're running
CONFIG_VAR_PREFIXNonePrefix of all configuration property and secret names
NODE_ENVNoneSome string with env name in it - Some string with 'production' in it means running in production
API_HOSTapi_hostName of host API is running on
API_PORTapi_portPort API is listening on
ENABLE_CALLBACKenable_callbackBoolean indicating whether feature is turned on
ENABLE_CHECK_INenable_check_inBoolean indicating whether feature is turned on
ENABLE_METRICSenable_metricsBoolean indicating whether feature is turned on
ENABLE_NOTICESenable_self_isolation_noticesBoolean indicating whether feature is turned on
LOG_LEVELlog_levelStandard logging level setting
CORS_ORIGINcors_originBoolean indicating something about CORS
DB_HOSTdb_hostConnection host for database (writing/reading)
DB_READ_HOSTdb_read_hostConnection host for database (reading)
DB_PORTdb_portPort the database is listening on
DB_DATABASEdb_databaseName of database to connect to
DB_SSLdb_sslBoolean indicating whether database connection should be SSL
DEFAULT_REGIONdefault_regionDefault Covid region
REFRESH_TOKEN_EXPIRYsecurity_refresh_token_expiry??
EXPOSURE_LIMIT????
CODE_LIFETIME_MINSsecurity_code_lifetime_mins??
TOKEN_LIFETIME_MINSsecurity_token_lifetime_mins??
UPLOAD_TOKEN_LIFETIME_MINSupload_token_lifetime_mins??
VERIFY_RATE_LIMIT_SECSsecurity_verify_rate_limit_secs??
CALLBACK_QUEUE_URLcallback_url??
NOTICES_QUEUE_URLself_isolation_notices_url??
ASSETS_BUCKETs3_assets_bucket??
CALLBACK_RATE_LIMIT_SECSsecurity_callback_rate_limit_secs# of seconds required before a user can request another callback
CALLBACK_RATE_LIMIT_REQUEST_COUNTsecurity_callback_rate_limit_request_count# of callback requests allowed before rate limit goes into effect
NOTICES_RATE_LIMIT_SECSsecurity_notices_rate_limit_secs# of seconds required before a user can request another notice unique key

Secret Settings

.envAWS Secret KeyFieldMeaning
DB_USERrds-read-write-createusernameDatabase username
DB_PASSWORDrds-read-write-createpasswordDatabase password
ENCRYPT_KEYencryptkeyKey used to encrypt things
JWT_SECRETjwtkey??
DEVICE_CHECK_KEY_IDdevice-checkkeyId??
DEVICE_CHECK_KEYdevice-checkkey??
DEVICE_CHECK_TEAM_IDdevice-checkteamId??
DEVICE_CHECK_PACKAGE_NAMEdevice-checkapkPackageName??
DEVICE_CHECK_PACKAGE_DIGESTdevice-checkapkDigestSha256??
DEVICE_CHECK_CERTIFICATE_DIGESTdevice-checkapkCertificateDigestSha256??
DEVICE_CHECK_ROOT_CAdevice-checksafetyNetRootCa??
DEVICE_CHECK_TIME_DIFF_THRESHOLD_MINSdevice-checktimeDifferenceThresholdMins??

Team

Lead Maintainers

Core Team

Contributors

Past Contributors

Hosted By

Linux Foundation Public Health

Acknowledgements

License

Copyright (c) 2020 HSEIreland Copyright (c) The COVID Green Contributors

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Contributors

colmharte

66 commits

dennisgove

56 commits

jackmurdoch

28 commits

segfault

25 commits

Languages

JavaScript

100.0%