Skip to content

Commit

Permalink
fix: redis error handler, stage docker build.
Browse files Browse the repository at this point in the history
  • Loading branch information
morganney committed Jan 6, 2024
1 parent 08f8ab7 commit e3807d1
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ FROM nginx:1.25.3 as web
ARG HOST_NAME=busmap.localhost
COPY packages/web/certs/ /etc/nginx/certs/
COPY packages/web/conf.d/core/ /etc/nginx/conf.d/core/
COPY packages/web/templates/ /etc/nginx/templates/
COPY packages/web/templates/core/ /etc/nginx/templates/core/
COPY packages/web/templates/default.conf.template /etc/nginx/templates/default.conf.template
COPY packages/web/nginx.conf /etc/nginx/nginx.conf
COPY --from=builder /app/packages/ui/dist /var/www/${HOST_NAME}
EXPOSE 80 443
Expand Down
48 changes: 42 additions & 6 deletions packages/api/src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,46 @@ import { env } from 'node:process'

import postgres from 'postgres'

import { logger } from './logger.js'

/**
* @see https://aws.github.io/copilot-cli/docs/developing/storage/
*/
interface AuroraCluster {
host: string
port: string
dbname: string
username: string
password: string
dbClusterIdentifier: string
engine: string
}

let host = 'db'
let port = '5432'
let database = env.POSTGRES_DB
let username = env.POSTGRES_USER
let password = env.POSTGRES_PASSWORD

/**
* Check for environment variable injected by AWS copilot.
*/
if (env.DB_SECRET) {
const secrets = JSON.parse(env.DB_SECRET) as AuroraCluster

host = secrets.host
port = secrets.port
database = secrets.dbname
username = secrets.username
password = secrets.password
}

const sql = postgres({
host: 'db',
port: 5432,
host,
database,
username,
password,
port: Number(port),
/**
* Sets db connections.
* @see https://github.com/porsager/postgres/issues/369#issuecomment-1129580233
Expand All @@ -14,10 +51,9 @@ const sql = postgres({
* Not sure if the same as what is in this doc.
* @see https://www.postgresql.org/docs/current/runtime-config-client.html#GUC-IDLE-SESSION-TIMEOUT
*/
idle_timeout: 0,
database: env.POSTGRES_DB,
username: env.POSTGRES_USER,
password: env.POSTGRES_PASSWORD
idle_timeout: 5
})

logger.info({ host: host, port: port, name: database }, 'Database connected.')

export { sql }
4 changes: 4 additions & 0 deletions packages/api/src/handlers/error.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import errors from 'http-errors'

import { logger } from '../logger.js'

import type { ErrorRequestHandler } from 'express'

const handler: ErrorRequestHandler = (err, req, res, next) => {
logger.error(err, 'Error handler invoked.')

if (res.headersSent) {
return next(err)
}
Expand Down
7 changes: 7 additions & 0 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,16 @@ let redisClient: ReturnType<typeof createClient> | null = null
if (env.BM_SESSION_STORE === 'redis') {
debug('initializing redis store at host', env.BM_REDIS_HOST)
redisClient = createClient({ url: env.BM_REDIS_HOST })
redisClient.on('error', err => {
logger.error(err, 'Redis unexpected error.')
})
redisClient.on('reconnecting', () => {
logger.info('Redis reconnecting.')
})

try {
await redisClient.connect()
logger.info('Redis client connected.')
/**
* TTL for the redis session key is derived from `cookie.maxAge` (SESSION_DURATION_MS).
* Setting `disableTouch` to `true` prevents rolling backend sessions (connect-redis updates the TTL).
Expand Down

0 comments on commit e3807d1

Please sign in to comment.