Skip to content

Commit

Permalink
Add the web server to the process manager
Browse files Browse the repository at this point in the history
  • Loading branch information
rslucena committed May 2, 2024
1 parent 217fd42 commit c1f60db
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
11 changes: 5 additions & 6 deletions src/infrastructure/server/webserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,17 @@ import { convertRequestTypes, err } from './request'
async function webserver(): Promise<server> {
const instance = fastify({
logger: Logs.provider,
caseSensitive: true,
caseSensitive: false,
pluginTimeout: 20000,
requestTimeout: 20000,
disableRequestLogging: true,
}).withTypeProvider<ZodTypeProvider>()
instance.removeContentTypeParser('text/plain')
instance.addHook('onRequest', convertRequestTypes)
instance.setValidatorCompiler(validatorCompiler)
instance.setSerializerCompiler(serializerCompiler)
instance.register(fastifyCors, cors)
instance.register(fastifyHelmet, helmet)
instance.setNotFoundHandler((_request, reply) => reply.code(418).send())
instance.setNotFoundHandler((_request, reply) => reply.code(510).send())
instance.register(fastifySwagger, SettingOptions)
instance.register(fastifySwaggerUi, SettingOptionsUI)
instance.setErrorHandler(function (error, request, reply) {
Expand Down Expand Up @@ -56,12 +55,12 @@ async function webserver(): Promise<server> {
return instance
}

async function start(instance: server): Promise<void> {
async function start(instance: server, port: number): Promise<void> {
instance.ready((err) => {
if (err) return Logs.console.error(err.message, err, true)
instance.swagger()
})
instance.listen({ port: Number(process.env.PROCESS_PORT), host: '0.0.0.0' }, (err, address) => {
instance.listen({ port, host: '0.0.0.0' }, (err, address) => {
if (err) Logs.file.error(err.message, err, true)
Logs.console.info(`Server listening on ${address}`)
Logs.console.info(`${instance.printRoutes({ commonPrefix: false })}`)
Expand All @@ -70,5 +69,5 @@ async function start(instance: server): Promise<void> {

export default {
create: () => webserver(),
start: (instance: server) => start(instance),
start: (instance: server, port: number) => start(instance, port),
}
2 changes: 1 addition & 1 deletion src/index.ts → src/providers/http-webserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import webserver from '@infrastructure/server/webserver'
;(async () => {
const server = await webserver.create()
server.register(userRoutes, { prefix: '/api/v1/users' })
webserver.start(server)
webserver.start(server, Number(process.env.PROCESS_PORT))
})()

0 comments on commit c1f60db

Please sign in to comment.