Skip to content

Commit

Permalink
Add HOST_PORT option
Browse files Browse the repository at this point in the history
  • Loading branch information
apricot13 committed Jun 11, 2024
1 parent db5a314 commit b3ac5e6
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 51 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions Dockerfile.production
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -21,7 +22,7 @@ connect(() =>
logger.info(
`📡 Database connection established http${
!isDevelopment ? "s" : ""
}://localhost:${port}/api/v1/services`
}://localhost:${host_port}/api/v1/services`
)
)

Expand Down Expand Up @@ -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}`)
})
68 changes: 23 additions & 45 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected]: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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion sample.env
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit b3ac5e6

Please sign in to comment.