Skip to content

Commit

Permalink
feat(web): refactor server to export run and return http.Server
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
suddenlyGiovanni committed Dec 17, 2024
1 parent 1d28190 commit 49bf194
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions apps/web/server/server.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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<void> {
export async function run(): Promise<http.Server> {
const {
NODE_ENV,
PORT: _port,
HOST,
buildPathArg,
} = Config.decodeUnknownSync({
...process.env,
buildPathArg: process.argv[2],
})

// biome-ignore lint/style/useNamingConvention: <explanation>
const PORT = await getPort({ port: _port ?? 5173 })

const app: express.Express = express()

app.disable('x-powered-by')
Expand Down Expand Up @@ -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()

0 comments on commit 49bf194

Please sign in to comment.