Skip to content

Commit

Permalink
Add healthcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlgo11 committed May 20, 2024
1 parent 4c90334 commit fb58c1e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
15 changes: 13 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Use Alpine Linux as base image
FROM node:slim
# Use Debian as build OS (https://github.com/nodejs/docker-node/issues/1973)
FROM node:slim AS BUILD

# Set default env variable values
ENV PORT=3000
Expand All @@ -21,11 +21,22 @@ COPY package*.json ./
# Install dependencies
RUN npm i --verbose

# Use Alpine Linux as base image
FROM node:alpine

WORKDIR /app

# Copy from the build stage
COPY --from=0 . .

# Copy the rest of the application code
COPY . .

# Expose the port on which the Node.js application will run
EXPOSE 3000

# Configure healthcheck
HEALTHCHECK --interval=1m --timeout=5s CMD node healtcheck.js || exit 1

# Command to run the Node.js application
CMD ["node", "server.js"]
7 changes: 7 additions & 0 deletions healthcheck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require('dotenv').config();

fetch(`http://localhost:${process.env.PORT}/healthcheck`).then(res => {
return process.exit(res.status === 204 ? 0:1);
}).catch(() => {
process.exit(1);
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"main": "server.js",
"private": true,
"scripts": {
"start": "node server.js"
"start": "node server.js",
"health": "node healthcheck.js"
},
"keywords": [
"unifi",
Expand Down
4 changes: 4 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ app.get('/img/bg.jpg', async (req, res) => {
});
});

app.get('/healthcheck', (req,res)=> {
res.sendStatus(204)
})

// Serve static files
app.use(express.static('./node_modules/bootstrap-icons/font/'));
app.use(express.static('./node_modules/@fontsource/'));
Expand Down

0 comments on commit fb58c1e

Please sign in to comment.