Api to support Adopt a Drain Goal: Develop a standard secure architecture. Strategy: Create an application that communicates with a database via a Restful API. Strategy: Use GitHub Actions to deploy app and api to Heroku. Strategy: JWT to control access to application and api.
- Done:
communications between Client and API - Done: Database (postgres) docker setup
- Done: Database (postress) herok setup
- Database implement a review table
- Database implement a staging table
Start rewrite of this readme
- Create Github Repository
- Clone repo
- Setup Hapi
- Configure docker-compose
- Commit to Git repository
A Hapi API Goal: Create an node API deployed to Heroku Strategy: Use Hapi api framework
- Github
- Hapi
- Docker-Compose
- Heroku
- Git Actions
- Swagger
- Enable a postgres installation in heroku
- Create a staging process for heroku
- create a repo to store your Hapi
- Setup node application
mkdir lyb-api
cd lyb-api\
npm init
- Setup hapi api
cd lyb-api\
npm init
npm install @hapi/hapi
- Add node packages
cd lyb-api\
# Development Environment
npm install dotenv
# Swagger
npm install @hapi/hapi
npm install @hapi/inert
npm install @hapi/vision
npm install hapi-swagger
# JOI Validation
npm install joi
-
Add a Hapi route
- lib/routes/salutaion_route.js
-
Configure CORS
- Setup CORS in handler option of route (eg. lib/routes/salutation_route.js)
cors: {
origin:["*"],
headers:['Accept', 'Authorization', 'Content-Type', 'If-None-Match', 'Content-Profile']
}
- Setup Development Environment
# .env
echo "# lyb-api" > .env
echo "NODE_ENV='developmemt'" >> .env
echo "HAPI='{\"host\":\"0.0.0.0\",\"port\":\"5555\"}'" >> .env
Before using docker-compose, build the dev-swagger-hapi-api container
- Manually Build Docker Container
#scripts/dk.build.sh
docker build -t dev-swagger-hapi-api --force-rm .
docker images
- Start docker-compose
docker-compose up
- Manually create heroku app
- Set GitHub Repository
- Configure Application Environment
# scripts/heroku.config.sh
heroku login
heroku config:set HOST=0.0.0.0 -a lyb-api
heroku config:set NODE_ENV=production -a lyb-api
heroku config:set NPM_CONFIG_PRODUCTION=false -a lyb-api
- Manually Add Buildpack heroku/nodejs
- Setup Heroku Environment
- Configure GitHub Actions
- .github/workflows/ci_api_staging.yml
- Configure GitHub secret.HEROKU_API_KEY=<your heroku key>
- Run swagger to check docker-compose
open -a safari http://0.0.0.0:5555/documentation
- Run swagger to check heroku install
open -a safari https://lyb-api.herokuapp.com/documentation
- Invalid token signature
{
"statusCode": 401,
"error": "Unauthorized",
"message": "Invalid token signature",
"attributes": {
"error": "Invalid token signature"
}
}
- Cause: Indcates a bad password/secret. This often happens when the JWT_SECRET has changed. Solution: Generate a new token using the updated secret.
- Route testing isn't working. Cannot connect to database.
- route.signin.post.test.js
- route.signup.post.test.js
Usage Headers 'Accept', 'Authorization', 'Content-Type', 'If-None-Match', 'Content-Profile'
- add eslint and cleanup code
- disable user routes
- disable home route
- disable salutation route
- disable Joi in lib/server.js
- enable detection of heroku production environment
- redirect api home to swagger/documentation
- create database model in node
- create node classes to encapcilate: * db, * extensions, * table, * functions
- Add CORS Access-Control-Allow-Origin header