Contributing to GradeJS Backend
Before doing the further actions, install Node.js and Yarn, clone this repository, open a terminal in the root of the repository and run:
yarn install
The required Node.js version is specified in the package.json
file. Consider using NVM utility to switch to the correct version.
Running locally
For some unit tests, such as API route testing, you will need to use a local PostgreSQL instance. A local database is also needed for running the application locally.
Create a docker container with a local PostgreSQL instance by running this commands:
docker-compose up -d db
# Create all required tables.
yarn --cwd packages/shared migration:run
# Run later to stop the database service:
docker-compose down
Run the server API:
yarn --cwd packages/public-api build
yarn --cwd packages/public-api start
Run the web application:
yarn --cwd packages/web start
In order to view database logs, consider enabling TypeORM logging in the connection.ts
file at the @gradejs/public-api
package or using the following command:
docker logs -f db
Server Deployment
Initial setup
- Sign in to AWS.
- Consider creating a PostgreSQL table before the first deployment:
- Open the AWS RDS page.
- In the database list click on the
Create Database
button. - Select the following parameters: Standart create, Amazon Aurora PostgreSQL-Compatible Edition version 13, Production template.
- Enter the database name, for example
gradejs-public
. - Select a burstable DB instance class:
db.t3.medium
- Select
Don't create an Aurora Replica
option. - Consider disabling public access for the production environment.
- In the
Additional configuration
section set theInitial database name
togradejs
. - Click in the create button. The initial migration will be executed by the application during the first deployment.
- Create an AWS Elastic Beanstalk application:
- Open the AWS Elastic Beanstalk page, select the
Applications
section and click on theCreate a new application
button. - Enter or select the application name, for example
gradejs
.
- Create Amazon SQS worker queues:
- Open the page and click on the
Create queue
button. - Select the standart queue option and specify the name
gradejs-public
. - Set the
visibility timeout
to 2 minutes. - Click on the
Create queue
button.
- Create an AWS Elastic Beanstalk API environment:
- Open the AWS Elastic Beanstalk page and click on the
Create a new environment
button. - Select
Web server environment
option. - Type the newly created application name (
gradejs
). - Specify an environment name, for example
gradejs-public-api
. Leave the domain and description name fileds blank. - Select the latest version of Node.js 16 managed platform on Amazon Linux 2. The version minimal required version is 5.5.1.
- Use the sample application code and click on the
Configure more options
button. - Select the
Custom configuration
configuration preset. - In the
Software
section click on theEdit
button. - Enable the
CloudWatch
log streaming with a 5 day retention. - Specify the
AWS_REGION
environment variable. It should be the same as your current AWS environemnt. For example,us-east-2
. - Specify the
DB_URL
environment variable. It should point to the newly created RDS database. For example:postgres://postgres:<secret>@<hostname>/gradejs
. - Specify the
INTERNAL_API_ORIGIN
environment variable. Consider using our staging internal API origin:http://fpjs-dev-gradejs-internal-api.eba-fybi4md5.us-east-1.elasticbeanstalk.com
- Specify the
SQS_WORKER_QUEUE_URL
queue. It should be a newly createdgradejs-backend
queue. For examplehttps://sqs.us-east-2.amazonaws.com/<account_id>/gradejs-backend
. - Specify the
AWS_REGION
environment variable. It should be the same as your current AWS environemnt. For example,us-east-2
. - Specify the
EB_START
environment variable:api
. - Click on the
Save
button and then click on theCreate environment
button.
- Create an AWS Elastic Beanstalk worker environment:
- Select
Worker environment
option. - Type the newly created application name (
gradejs-public
). - Specify an environment name:
gradejs-public-worker
. Leave the domain and description name fileds blank. - Select the latest version of Node.js 16 managed platform on Amazon Linux 2. The version minimal required version is 5.5.1.
- Use the sample application code and click on the
Configure more options
button. - Select the
Custom configuration
configuration preset. - In the
Software
section click on theEdit
button. - Enable the
CloudWatch
log streaming with a 5 day retention. - Set the same
AWS_REGION
,DB_URL
,SQS_WORKER_QUEUE_URL
environment variables. - Set the
EB_START
environment variable toworker
. - Click on the
Save
button and then click on theCreate environment
button.
- Setting up a
Continuous Deploy
pipeline:
- Go to the AWS CodePipeline page and click on the
Create pipeline
page. - Type a pipeline name
gradejs-public@production-deploy
and click on theNext
button. - Select the
Github 2
source provider and select thegradejs
repository. For the production environment abranch name
should bemaster
and for staging --develop
. - Click on the
Next
button. - Select the
AWS CodeBuild
build provider. - Create a
AWS CodeBuild
project. The name of a project should begradejs-public
. Select theUbuntu
operating system with the latest version of theaws/codebuild/standart:5.0
image. Click on theprivileged
flag. Leave the rest as defaults and click on theContinue to CodePipeline
button. - Click on the
Next
button on the build stage page. - Select the
AWS Elastic Beanstalk
deploy provider. - Select the
gradejs-public-api
environment name. - Click on
Next
button and then on theCreate pipeline
button. - Click on the
Edit
button in the newly created pipeline. Edit theDeploy
stage and click on theAdd action group
button - Type the
deploy-public-worker
action name and select theAWS CodeBuild
build provider. - Select the
BuildArtifact
as input artifact. - Select the
gradejs-public-worker
environment name. - Click on
Done
button and then onSave
button.