1 import type { AddressInfo
} from
'node:net'
2 import { ClusterWorker
} from
'poolifier'
3 import Fastify
, { type FastifyInstance
} from
'fastify'
4 import type { ClusterWorkerData
, ClusterWorkerResponse
} from
'./types.js'
5 import { fastifyPoolifier
} from
'./fastify-poolifier.js'
7 class FastifyWorker
extends ClusterWorker
<
11 private static fastify
: FastifyInstance
13 private static readonly startFastify
= async (
14 workerData
?: ClusterWorkerData
15 ): Promise
<ClusterWorkerResponse
> => {
16 const { port
} = workerData
as ClusterWorkerData
17 FastifyWorker
.fastify
= Fastify({
21 await FastifyWorker
.fastify
.register(fastifyPoolifier
, workerData
)
23 FastifyWorker
.fastify
.all('/api/echo', async (request
) => {
25 await FastifyWorker
.fastify
.execute({ body
: request
.body
}, 'echo')
29 FastifyWorker
.fastify
.get
<{
30 Params
: { number: number }
31 }>('/api/factorial/:number', async (request
) => {
32 const { number } = request
.params
34 await FastifyWorker
.fastify
.execute({ body
: { number } }, 'factorial')
38 await FastifyWorker
.fastify
.listen({ port
})
41 port
: (FastifyWorker
.fastify
.server
.address() as AddressInfo
).port
45 public constructor () {
46 super(FastifyWorker
.startFastify
, {
47 killHandler
: async () => {
48 await FastifyWorker
.fastify
.pool
.destroy()
49 await FastifyWorker
.fastify
.close()
55 export const fastifyWorker
= new FastifyWorker()