Skip to content

Commit

Permalink
feat: add server host for docker deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
jellydn committed Sep 24, 2023
1 parent d01ff53 commit 516478b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ COPY . .

# Set environment to production
ENV NODE_ENV production
ENV SERVER_HOST 0.0.0.0

# Expose port 8000
EXPOSE 8000
Expand Down
10 changes: 8 additions & 2 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import swagger from "@fastify/swagger";
import swaggerUI from "@fastify/swagger-ui";

const SERVER_PORT = Number(process.env.PORT ?? 3000);
const SERVER_HOST = process.env.SERVER_HOST ?? "localhost";

const server: FastifyInstance = fastify({
logger: process.env.NODE_DEVELOPMENT !== "test",
Expand All @@ -30,7 +31,7 @@ void server.register(swagger, {
url: "https://swagger.io",
description: "Find more info here",
},
host: "localhost:" + SERVER_PORT,
host: `${SERVER_HOST}:${SERVER_PORT}`,
schemes: ["http", "https"],
consumes: ["application/json"],
produces: ["application/json"],
Expand All @@ -46,6 +47,8 @@ void server.register(swaggerUI, {
staticCSP: true,
});

server.get("/", async (_request, _reply) => ({ status: "ok" }));

// Health check
server.get("/api", async (_request, _reply) => ({ ok: true }));

Expand Down Expand Up @@ -82,7 +85,10 @@ server.get(

async function startServer() {
try {
await server.listen({ port: SERVER_PORT });
await server.listen({
port: SERVER_PORT,
host: SERVER_HOST,
});

const address = server.server.address();
const port = typeof address === "string" ? address : address?.port;
Expand Down

0 comments on commit 516478b

Please sign in to comment.