From dc42fb071beacf3e0bbfd07cc51a82dc260eef1a Mon Sep 17 00:00:00 2001 From: Paul Beaudoin Date: Fri, 15 Mar 2024 19:46:46 -0400 Subject: [PATCH] Update Dockerfile to use node20, cleanup, docs Also, no longer log to discovery-api.log since it appears it may be enough for deployed code to write to stdout. Simplify how json/plain logging is triggered. --- Dockerfile | 20 +++----------------- README.md | 23 +++++++++++++++++++++-- lib/logger.js | 24 ++++++++++-------------- 3 files changed, 34 insertions(+), 33 deletions(-) diff --git a/Dockerfile b/Dockerfile index e6eb5045..4b602e10 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM node:18-bullseye as production +FROM node:20-bullseye as production RUN apt-get update RUN apt-get upgrade -y @@ -10,8 +10,6 @@ WORKDIR /usr/src/app # A wildcard is used to ensure both package.json AND package-lock.json are copied # where available (npm@5+) COPY package.json ./ -ENV NODE_ENV=production -ENV APP_ENV=production RUN npm cache verify RUN npm install @@ -19,6 +17,7 @@ RUN npm install # Bundle app source # Do not copy non-essential files COPY . . + # Remove any unneeded files RUN rm -rf /usr/src/app/.DS_Store RUN rm -rf /usr/src/app/.ebextensions @@ -26,19 +25,6 @@ RUN rm -rf /usr/src/app/.elasticbeanstalk RUN rm -rf /usr/src/app/.git RUN rm -rf /usr/src/app/.gitignore -# Environment variables for hosted stack in AWS Secrets Manager -# COPY ./config/production.env /usr/src/app/.env - -# Link logs to stdout -RUN ln -sf /dev/stdout /usr/src/app/log/discovery-api.log +ENV LOG_STYLE=json CMD [ "npm", "start" ] - - -FROM production as qa - -ENV NODE_ENV=qa -ENV APP_ENV=qa - -# Environment variables for hosted stack in AWS Secrets Manager -# COPY ./config/qa.env /usr/src/app/.env diff --git a/README.md b/README.md index e4faf27a..618e5d2c 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,20 @@ This is the API providing most of bibliographic data to the [NYPL Research Catal ## Installing & Running Locally -Start the container with AWS creds so that the app can decrypt config from `.env-docker`: +For local development, it's easiest to just use local node binaries: + +``` +nvm use; ENV=qa npm start +``` + +Note that when developing locally, you may need to [add your IP to the access control policy of the relevant ES domain](https://github.com/NYPL/aws/blob/b5c0af0ec8357af9a645d8b47a5dbb0090966071/common/elasticsearch.md#2-make-the-domain-public-restrict-by-ip). + +### Using Docker + +Docker files are included for deployment and can be used locally. + +To start the container with AWS creds so that the app can decrypt config from `config/*`: + ``` AWS_ACCESS_KEY_ID=... AWS_SECRET_ACCESS_KEY=... docker-compose up ``` @@ -16,7 +29,13 @@ After making changes, rebuild the image: docker-compose build ``` -Note that when developing locally, you may need to [add your IP to the access control policy of the relevant ES domain](https://github.com/NYPL/aws/blob/b5c0af0ec8357af9a645d8b47a5dbb0090966071/common/elasticsearch.md#2-make-the-domain-public-restrict-by-ip). +Or, equivalently, to build and run the image and container directly: + +``` +docker image build -t discovery-api:local . +docker container rm discovery-api +docker run --name discovery-api -e ENV=qa -e AWS_ACCESS_KEY_ID=... -e AWS_SECRET_ACCESS_KEY=... -p 8082:8082 -it discovery-api:local +``` ## Contributing diff --git a/lib/logger.js b/lib/logger.js index e4195c41..122a90cf 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -1,25 +1,21 @@ const winston = require('winston') +// In deployed code, let's do JSON logging to enable CW JSON queries +const format = process.env.LOG_STYLE === 'json' + ? winston.format.json() + // Locally, let's do colorized plaintext logging: + : winston.format.combine( + winston.format.colorize(), + winston.format.simple() + ) + const logger = winston.createLogger({ level: process.env.LOG_LEVEL || 'info', - format: winston.format.json(), transports: [ - new winston.transports.File({ - filename: './log/discovery-api.log' - }) + new winston.transports.Console({ format }) ] }) -// -// If we're not in production then log to the `console` with the format: -// `${info.level}: ${info.message} JSON.stringify({ ...rest }) ` -// -if (process.env.NODE_ENV !== 'production') { - logger.add(new winston.transports.Console({ - format: winston.format.simple() - })) -} - logger.setLevel = (level) => { logger.level = level }