Skip to content

Commit e5a978e

Browse files
committed
Read port from env
1 parent 1aeef26 commit e5a978e

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/app.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { type } from "arktype";
55
import credentialService from "./service/credentials.ts";
66
import { serve, type ServerType } from "@hono/node-server";
77

8+
const DEFAULT_PORT = 16281;
9+
810
function createApp() {
911
const app = new Hono();
1012

@@ -63,12 +65,24 @@ function createApp() {
6365
return app;
6466
}
6567

68+
function getPort(): number {
69+
const portFromEnv = process.env.CREDENTIAL_BRIDGE_PORT;
70+
if (portFromEnv) {
71+
const parsedPortFromEnv = parseInt(portFromEnv);
72+
// validate the port to be a valid port number
73+
if (isNaN(parsedPortFromEnv) || parsedPortFromEnv < 1 || parsedPortFromEnv > 65535) {
74+
return DEFAULT_PORT;
75+
}
76+
return parsedPortFromEnv;
77+
}
78+
return DEFAULT_PORT;
79+
}
80+
6681
export function startServer(): ServerType | undefined {
6782
try {
6883
const app = createApp();
69-
const port = 16281;
7084
const hostname = "0.0.0.0";
71-
85+
const port = getPort();
7286
const server = serve({
7387
...app,
7488
port,

0 commit comments

Comments
 (0)