Skip to content

Commit

Permalink
Update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
apricot13 committed Aug 19, 2023
1 parent 62a6f78 commit 5d9a67e
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 66 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ RUN npm ci --omit=dev
FROM development_base as development
ENV NODE_ENV development
WORKDIR /usr/src/app
COPY --chown=node:node --from=development_base /usr/build/app/node_modules ./
COPY --chown=node:node --from=development_base /usr/build/app/node_modules ./node_modules
USER node
CMD ["npm", "run", "dev" ]

Expand All @@ -28,7 +28,7 @@ CMD ["npm", "run", "dev" ]
FROM production_base as production
ENV NODE_ENV production
WORKDIR /usr/src/app
COPY --chown=node:node --from=production_base /usr/build/app/node_modules /usr/src/app
COPY --chown=node:node --from=production_base /usr/build/app/node_modules ./node_modules
COPY --chown=node:node . /usr/src/app
USER node
CMD ["npm", "run", "start" ]
16 changes: 8 additions & 8 deletions docker-compose.development.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: "3.7"
services:
outpost-api:
outpost-api-dev:
stdin_open: true
image: "outpost-api:development"
container_name: outpost-api-dev
Expand All @@ -23,26 +23,26 @@ services:
NODE_ENV: development
DB_URI: mongodb://mongo:27017/outpost_api_development
networks:
- outpos_api_external_network
- outpos_api_internal_network
- outpost_api_dev_external_network
- outpost_api_dev_internal_network
mongo:
image: mongo:6
container_name: outpost-api-mongo
restart: unless-stopped
ports:
- 27017:27017
volumes:
- outpost-api-mongo-volume:/data/db
- outpost-api-dev-mongo-volume:/data/db
- ./setup-mongodb-development.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
environment:
MONGO_INITDB_DATABASE: outpost_api_development
networks:
- outpos_api_external_network
- outpost_api_dev_external_network

volumes:
outpost-api-mongo-volume:
outpost-api-dev-mongo-volume:

networks:
outpos_api_external_network:
outpos_api_internal_network:
outpost_api_dev_external_network:
outpost_api_dev_internal_network:
internal: true
10 changes: 5 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ services:
NODE_ENV: production
DB_URI: mongodb://mongo:27017/outpost_api
networks:
- outpos_api_internal_network
- outpos_api_external_network
- outpost_api_internal_network
- outpost_api_external_network

mongo:
image: mongo:6
Expand All @@ -35,12 +35,12 @@ services:
- outpost-api-mongo-volume:/data/db
- ./setup-mongodb-production.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
networks:
- outpos_api_external_network
- outpost_api_external_network

volumes:
outpost-api-mongo-volume:

networks:
outpos_api_external_network:
outpos_api_internal_network:
outpost_api_external_network:
outpost_api_internal_network:
internal: true
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "api_service",
"name": "@outpost_platform/api_service",
"version": "1.0.0",
"description": "",
"main": "index.js",
Expand All @@ -9,6 +9,7 @@
"test": "jest",
"prepare-indices": "node lib/prepare-indices"
},
"test": "exit 0",
"keywords": [],
"author": "",
"license": "ISC",
Expand Down
102 changes: 52 additions & 50 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ It's not useful by itself — it depends on a public index built by [Outpost](ht

It's a simple Node.js app which queries information from a MongoDB collection and publishes it as a read-only, rate-limited REST API.

To run it on your machine you need Node.js, npm, nvm (https://github.com/nvm-sh/nvm) and a working MongoDB database [with the right indices](#indices) available on `localhost:27017`.

## 🧬 Configure Outpost API

It expects a few environment variables.
Expand All @@ -55,79 +53,55 @@ Other environmental variables:

## 💻 Getting started

### Using docker
To get up and running quickly with some data use docker compose.

```sh
git clone [email protected]:wearefuturegov/outpost-api-service.git && cd outpost-api-service

# build the image - if using for the first time
docker build --tag outpost-api-service:development --target development .

# run the image in local environment
docker run -p 3001:3001 --name outpost-api-service -v $(pwd):/app:cached -i -d outpost-api-service:development

# setup indices
docker exec -it outpost-api-service npm run prepare-indices
# build the image
docker compose -f docker-compose.development.yml build

# access the site
open http://localhost:3001/api/v1/services
# run the container
docker compose -f docker-compose.development.yml up -d

# open shell in container
docker exec -it outpost-api-service /bin/ash;
docker compose -f docker-compose.development.yml exec outpost-api-dev /bin/ash;

# run tests
docker exec -it outpost-api-service npm run test
# run the tests
docker compose -f docker-compose.development.yml exec outpost-api-dev npm run test

# stop the container
docker stop outpost-api-service

# start again
docker start outpost-api-service
docker compose -f docker-compose.development.yml stop
```

### Using docker-compose
If you want to use it in conjunction with a local mongodb database, for example you are using [Outpost](https://github.com/wearefuturegov/outpost/) you could also run it using just docker.

```sh
git clone [email protected]:wearefuturegov/outpost-api-service.git && cd outpost-api-service

# build the image
docker compose -f docker-compose.development.yml build
# build the image - if using for the first time
docker build --tag outpost-api-dev --target development .

# run the container
docker compose -f docker-compose.development.yml up -d
# run the image in local environment
docker run -p 3001:3001 --name outpost-api-dev -v $(pwd):/usr/src/app:cached -i -d outpost-api-dev

# setup indices
docker compose -f docker-compose.development.yml exec outpost-api npm run prepare-indices;
docker exec -it outpost-api-dev npm run prepare-indices

# access the site
open http://localhost:3001/api/v1/services

# open shell in container
docker compose -f docker-compose.development.yml exec outpost-api /bin/ash;
docker exec -it outpost-api-dev /bin/ash;

# run tests
docker compose -f docker-compose.development.yml exec outpost-api npm run test
docker exec -it outpost-api-dev npm run test

# stop the container
docker compose -f docker-compose.development.yml stop

```

### On your machine

To run it on your machine you need Node.js, npm, nvm (https://github.com/nvm-sh/nvm) and a working MongoDB database [with the right indices](#indices) available on `localhost:27017`.

```
# use the right node version
nvm use
# install npm packages
npm i
# if this is your first time installing it prepare the database
npm run prepare-indices
docker stop outpost-api-dev

# start the local development server
npm run dev
# outpost api is running on http://localhost:3001
# start again
docker start outpost-api-dev
```

## Deploying it
Expand All @@ -140,7 +114,7 @@ It's suitable for 12-factor hosting like Heroku. It has a [Procfile](https://dev
npm start
```

You can also deploy via docker
You can also deploy via docker compose

```sh
# build the image
Expand All @@ -150,13 +124,41 @@ docker compose build
docker compose up -d
```

and docker

```sh
# build the image
docker build --tag outpost-api --target production .

# run the container
docker run -p 3001:3001/tcp --name outpost-api -i -d outpost-api
```

## Mongodb container running in docker

```sh
docker run -p 27017:27017/tcp \
--name outpost-api-mongo \
-e MONGO_INITDB_DATABASE=outpost_api \
--volume outpost-api-mongo-volume:/data/db \
--volume $(pwd)/setup-mongodb-production.js:/docker-entrypoint-initdb.d/mongo-init.js:ro -i -d mongo:6
```

-v $(pwd):/usr/src/app

## Indices

It needs the right indices on the MongoDB collection to enable full-text and geo search. Something like:

```
db.indexed_services.createIndex({ name: "text", description: "text" })
db.indexed_services.createIndex({ "locations.coordinates": "2dsphere" })
```

You can create these two, plus an index of taxonomy slugs, automatically with the `npm run prepare-indices` command.

```
```

0 comments on commit 5d9a67e

Please sign in to comment.