diff --git a/Dockerfile b/Dockerfile index e41bbf5..2739cb6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,6 +5,9 @@ FROM node:iron-alpine as build_frontend ARG NODE_ENV ENV NODE_ENV $NODE_ENV +RUN apk update +RUN apk add curl + COPY ./package.json ./tmp/package.json COPY ./package-lock.json ./tmp/package-lock.json diff --git a/Dockerfile.production b/Dockerfile.production index 873144d..963b36b 100644 --- a/Dockerfile.production +++ b/Dockerfile.production @@ -8,6 +8,9 @@ ARG FORCE_SSL ENV NODE_ENV $NODE_ENV RUN adduser -D outpost-user +RUN apk update +RUN apk add curl + COPY ./package.json ./tmp/package.json COPY ./package-lock.json ./tmp/package-lock.json diff --git a/docker-compose.yml b/docker-compose.yml index 526230f..0981f74 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,17 +14,17 @@ services: # command: ["-f", "/dev/null"] environment: DB_URI: mongodb://${MONGO_INITDB_USERNAME:-outpost}:${MONGO_INITDB_PASSWORD:-password}@mongo/${MONGO_INITDB_DATABASE:-outpost_api_development} - platform: linux/amd64 + platform: linux/arm64 volumes: - ./:/app:cached - /app/node_modules ports: - - 3001:3000 + - "${HOST_PORT:-3000}:3000" networks: - internal_network - external_network healthcheck: - test: ["CMD", "curl", "-f", "http://localhost:3001/health"] + test: ["CMD-SHELL", "curl -f http://localhost:3000/health"] interval: 1m30s timeout: 10s retries: 3 diff --git a/index.js b/index.js index 94f02da..d7f3bf0 100644 --- a/index.js +++ b/index.js @@ -11,6 +11,7 @@ const v1 = require("./src/controllers/v1") const router = express.Router() const server = express() const port = process.env.PORT || 3000 +const host_port = process.env.HOST_PORT || process.env.PORT || 3000 const environment = process.env.NODE_ENV || "production" const isDevelopment = environment === "development" @@ -21,7 +22,7 @@ connect(() => logger.info( `📡 Database connection established http${ !isDevelopment ? "s" : "" - }://localhost:${port}/api/v1/services` + }://localhost:${host_port}/api/v1/services` ) ) @@ -92,6 +93,6 @@ server.use((err, req, res, next) => { * Start the server */ server.listen(port, () => { - logger.info(`Server is running on port ${port}`) + logger.info(`Server is running on port ${host_port}`) logger.info(`Logging level is set to ${logger.level}`) }) diff --git a/readme.md b/readme.md index 84b67a6..de8beaa 100644 --- a/readme.md +++ b/readme.md @@ -34,62 +34,37 @@ To run it on your machine you need Node.js, npm, nvm (https://github.com/nvm-sh/ ## 🧬 Configure Outpost API -It expects a few environment variables. - -`DB_URI` - -- MongoDB connection URI - -`GOOGLE_API_KEY` - -- Used for geocoding `location=` parameters. -- **Needs the geocoding API enabled.** - -Other environmental variables: - -`COMPOSE_PROJECT_NAME` - -- This is used to name the docker images and containers - ---- +See [environmental variables](#environmental-variables) below. ## 💻 Running it locally +It is recommended to use docker but you can choose to run the project locally as well. + ```sh # get the code git clone git@github.com:wearefuturegov/outpost-api-service.git && cd outpost-api-service -# make sure your using the correct node version -nvm use - # setup env and database variables -# see setting up database below cp sample.env .env -# Setup your database locally (see below) or start up a database using docker -# NB this runs mongo on a non-standard port `27018` in case you have mongo already running, you can change it to `27017` if you would like to -# connect with compass or mongosh with: mongodb://outpost:password@localhost:27018 -docker compose up -d mongo - -# once its setup you can just use this to restart it -docker start outpost-api-db - -# install dependencies -npm install +# setup +docker compose up -d -# run development mode -npm run dev +# if you need dummy data +docker compose exec app npm run dummy-data +# access the api +open http://localhost:3001/api/v1/services -# add some dummy data (if required) -npm run dummy-data +# access the mongodb +# NB this runs mongo on a non-standard port `27018` in case you have mongo already running, you can change it to `27017` if you would like to +mongo "mongodb://outpost:password@localhost:27018/outpost_api_development" -# stop your database +# stop the application at any time by running docker compose stop -# delete your database image +# remove the running containers by running docker compose down - ``` ### Setting up database locally @@ -102,8 +77,6 @@ mongosh .docker/services/mongo/setup-mongodb.js npm run prepare-indices ``` -or you can use the following commands to create your indices - # 🧬 Configuration ## Environmental Variables @@ -112,10 +85,13 @@ You can provide config with a `.env` file. Run `cp sample.env .env` to create a The following environmental variables are required. -| Variable | Description | Example | Required? | -| ---------------- | ------------------ | -------------------------------------------------------------------- | --------- | -| `DB_URI` | Mongo database url | `mongodb://outpost:password@localhost:27018/outpost_api_development` | Yes | -| `GOOGLE_API_KEY` | Google API Key | `1234` | Yes | +| Variable | Description | Example | Required? | +| ---------------- | ------------------------------------------------------------------------- | -------------------------------------------------------------------- | --------- | +| `DB_URI` | Mongo database url | `mongodb://outpost:password@localhost:27018/outpost_api_development` | Yes | +| `GOOGLE_API_KEY` | Google API Key | `1234` | Yes | +| `DEBUG_LEVEL` | Debug logging level options are: error, warn, info, http, debug | `debug` | No | +| `FORCE_SSL` | Force SSL defaults to false unless this is set to true | true | No | +| `HOST_PORT` | If running in docker set this to change the exposed port, default is 3000 | 3001 | No | # ✨ Features @@ -133,6 +109,8 @@ You will need to enable the `Geocode API`, no restrictions are needed. It's suitable for 12-factor hosting like Heroku. It has a [Procfile](https://devcenter.heroku.com/articles/procfile) that will make sure the proper MongoDB indices are set up. +Ensure you set `FORCE_SSL` to true. + ### Deploying using docker We also provide a Docker image if you would like to host the application in a container diff --git a/sample.env b/sample.env index 2a17834..6107f2c 100644 --- a/sample.env +++ b/sample.env @@ -3,9 +3,12 @@ # doesn't apply in development env # FORCE_SSL=true +# if you need to run the api on a different port on the host +# HOST_PORT=3000 + # ----- MONGO CONFIG -DB_URI=mongodb://localhost:27018/outpost_api_development +# DB_URI=mongodb://localhost:27018/outpost_api_development # ----- GEOCODING GOOGLE_API_KEY=