From 5d9a67edd7462aa4086e3b878af2df2c0291d354 Mon Sep 17 00:00:00 2001 From: Han Date: Sat, 19 Aug 2023 21:37:52 +0100 Subject: [PATCH] Update documentation --- Dockerfile | 4 +- docker-compose.development.yml | 16 +++--- docker-compose.yml | 10 ++-- package.json | 3 +- readme.md | 102 +++++++++++++++++---------------- 5 files changed, 69 insertions(+), 66 deletions(-) diff --git a/Dockerfile b/Dockerfile index 66164ae..184e7c1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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" ] @@ -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" ] diff --git a/docker-compose.development.yml b/docker-compose.development.yml index 878b95d..d981ad7 100644 --- a/docker-compose.development.yml +++ b/docker-compose.development.yml @@ -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 @@ -23,8 +23,8 @@ 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 @@ -32,17 +32,17 @@ services: 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 diff --git a/docker-compose.yml b/docker-compose.yml index 35cf144..2c43d02 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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 @@ -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 diff --git a/package.json b/package.json index eb442a1..1ca8254 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "api_service", + "name": "@outpost_platform/api_service", "version": "1.0.0", "description": "", "main": "index.js", @@ -9,6 +9,7 @@ "test": "jest", "prepare-indices": "node lib/prepare-indices" }, + "test": "exit 0", "keywords": [], "author": "", "license": "ISC", diff --git a/readme.md b/readme.md index dfc3c40..7b5f2f4 100644 --- a/readme.md +++ b/readme.md @@ -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. @@ -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 git@github.com: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 git@github.com: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 @@ -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 @@ -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. + +``` + +```