Skip to content

Commit

Permalink
Update Dockerfile to use node20, cleanup, docs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nonword committed Mar 15, 2024
1 parent 3fea7ad commit dc42fb0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 33 deletions.
20 changes: 3 additions & 17 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,35 +10,21 @@ 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

# 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
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
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand All @@ -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

Expand Down
24 changes: 10 additions & 14 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down

0 comments on commit dc42fb0

Please sign in to comment.