Skip to content

Commit

Permalink
Add graceful shutdown and health check url
Browse files Browse the repository at this point in the history
  • Loading branch information
juffalow committed Sep 29, 2024
1 parent 39b5e3c commit 2a23f49
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 25 deletions.
21 changes: 13 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
FROM node:22-alpine AS build

RUN apk add dumb-init

WORKDIR /home/node

COPY . .
RUN yarn install --frozen-lockfile && yarn build

RUN yarn install --frozen-lockfile
RUN yarn build

FROM node:22-alpine

ENV NODE_ENV production

RUN apk update && apk upgrade --no-cache
COPY --from=build /usr/bin/dumb-init /usr/bin/dumb-init

RUN addgroup --gid 3000 --system juffgroup \
&& adduser --uid 2000 --system --ingroup juffgroup juffuser
RUN apk update && \
apk upgrade --no-cache && \
addgroup --gid 3000 --system juffgroup && \
adduser --uid 2000 --system --ingroup juffgroup juffuser && \
mkdir /home/juffuser/express-graphql-example/

USER 2000:3000

RUN mkdir /home/juffuser/express-graphql-example/
WORKDIR /home/juffuser/express-graphql-example/

COPY --from=build /home/node/dist ./dist
COPY --from=build /home/node/package.json /home/node/yarn.lock ./
RUN yarn install --frozen-lockfile --production
COPY --from=build /home/node/node-modules ./node-modules

EXPOSE 3010

CMD [ "node", "dist/index.js" ]
CMD [ "dumb-init", "node", "dist/index.js" ]
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"main": "./dist/index.js",
"license": "MIT",
"dependencies": {
"@godaddy/terminus": "^4.12.1",
"dataloader": "^2.0.0",
"express": "^4.16.4",
"graphql": "^16.0.1",
Expand Down
21 changes: 21 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import express from 'express';
import { createHandler } from 'graphql-http/lib/use/express';
import responseTime from './middlewares/reponseTime';
import cors from './middlewares/cors';
import context from './context';
import schema from './schema';

const app = express();

app.disable('x-powered-by');
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(responseTime);
app.use(cors);

app.all('/graphql', createHandler({
schema,
context: context as any,
}));

export default app;
40 changes: 23 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
import express from 'express';
import { createHandler } from 'graphql-http/lib/use/express';
import responseTime from './middlewares/reponseTime';
import cors from './middlewares/cors';
import http from 'http';
import { createTerminus } from '@godaddy/terminus';
import app from './app';
import config from './config';
import context from './context';
import schema from './schema';
import { checkConnection, migrate } from './database';

const app = express();
const server = http.createServer(app);

app.disable('x-powered-by');
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(responseTime);
app.use(cors);
async function onSignal(): Promise<void> {
console.warn('Server is going to shut down! Starting cleanup...');
}

async function onShutdown (): Promise<void> {
console.warn('Server is shutting down!');
}

async function onHealthCheck(): Promise<void> {
return;
}

app.all('/graphql', createHandler({
schema,
context: context as any,
}));
createTerminus(server, {
healthChecks: {
'/health/liveness': onHealthCheck,
},
onSignal,
onShutdown,
});

async function start(): Promise<void> {
try {
Expand All @@ -30,7 +36,7 @@ async function start(): Promise<void> {
}
}

app.listen(config.port, () => {
server.listen(config.port, () => {
console.log(`Server started at http://localhost:${ config.port }`);
});
} catch(error) {
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,13 @@
resolved "https://registry.yarnpkg.com/@eslint/js/-/js-8.57.1.tgz#de633db3ec2ef6a3c89e2f19038063e8a122e2c2"
integrity sha512-d9zaMRSTIKDLhctzH12MtXvJKSSUhaHcjV+2Z+GK+EEY7XKpP5yR4x+N3TAcHTcu963nIr+TMcCb4DBCYX1z6Q==

"@godaddy/terminus@^4.12.1":
version "4.12.1"
resolved "https://registry.yarnpkg.com/@godaddy/terminus/-/terminus-4.12.1.tgz#c4fdc280a4ac9655d4734250e22299a4ad46c54c"
integrity sha512-Tm+wVu1/V37uZXcT7xOhzdpFoovQReErff8x3y82k6YyWa1gzxWBjTyrx4G2enjEqoXPnUUmJ3MOmwH+TiP6Sw==
dependencies:
stoppable "^1.1.0"

"@humanwhocodes/config-array@^0.13.0":
version "0.13.0"
resolved "https://registry.yarnpkg.com/@humanwhocodes/config-array/-/config-array-0.13.0.tgz#fb907624df3256d04b9aa2df50d7aa97ec648748"
Expand Down Expand Up @@ -3445,6 +3452,11 @@ [email protected]:
resolved "https://registry.yarnpkg.com/statuses/-/statuses-2.0.1.tgz#55cb000ccf1d48728bd23c685a063998cf1a1b63"
integrity sha512-RwNA9Z/7PrK06rYLIzFMlaF+l73iwpzsqRIFgbMLbTcLD6cOao82TaWefPXQvB2fOC4AjuYSEndS7N/mTCbkdQ==

stoppable@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/stoppable/-/stoppable-1.1.0.tgz#32da568e83ea488b08e4d7ea2c3bcc9d75015d5b"
integrity sha512-KXDYZ9dszj6bzvnEMRYvxgeTHU74QBFL54XKtP3nyMuJ81CFYtABZ3bAzL2EdFUaEwJOBOgENyFj3R7oTzDyyw==

string-length@^4.0.1:
version "4.0.2"
resolved "https://registry.yarnpkg.com/string-length/-/string-length-4.0.2.tgz#a8a8dc7bd5c1a82b9b3c8b87e125f66871b6e57a"
Expand Down

0 comments on commit 2a23f49

Please sign in to comment.