From 49bf194f137031251e58c95ce166032693b583bd Mon Sep 17 00:00:00 2001 From: suddenlyGiovanni <15946771+suddenlyGiovanni@users.noreply.github.com> Date: Tue, 17 Dec 2024 20:28:21 +0100 Subject: [PATCH] feat(web): refactor server to export `run` and return `http.Server` This change modifies the `run` function to export it and ensures it returns an `http.Server` instance. It also updates the configuration handling, simplifying input decoding and assigning a default port if not provided. These adjustments improve reusability and clarity. --- apps/web/server/server.ts | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/apps/web/server/server.ts b/apps/web/server/server.ts index 8a0c14a9..36fd9768 100644 --- a/apps/web/server/server.ts +++ b/apps/web/server/server.ts @@ -1,4 +1,5 @@ import fs from 'node:fs' +import type * as http from 'node:http' import os from 'node:os' import path from 'node:path/posix' import process from 'node:process' @@ -73,17 +74,20 @@ sourceMapSupport.install({ * server/main.ts ./build/server/index.js * ``` */ -async function run({ - NODE_ENV, - PORT, - HOST, - buildPathArg, -}: { - NODE_ENV: 'development' | 'production' - PORT: number - HOST: undefined | string - buildPathArg: string -}): Promise { +export async function run(): Promise { + const { + NODE_ENV, + PORT: _port, + HOST, + buildPathArg, + } = Config.decodeUnknownSync({ + ...process.env, + buildPathArg: process.argv[2], + }) + + // biome-ignore lint/style/useNamingConvention: + const PORT = await getPort({ port: _port ?? 5173 }) + const app: express.Express = express() app.disable('x-powered-by') @@ -153,13 +157,8 @@ async function run({ for (const signal of ['SIGTERM', 'SIGINT']) { process.once(signal, () => server?.close(console.error)) } -} -const config = Config.decodeUnknownSync({ ...process.env, buildPathArg: process.argv[2] }) + return server +} -run({ - NODE_ENV: config.NODE_ENV, - PORT: await getPort({ port: config.PORT ?? 5173 }), - HOST: config.HOST, - buildPathArg: config.buildPathArg, -}) +run()