28bf2a01f1cb0d15aff9b86d3bbbeed98c9bd808
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
, ...fastifyPoolifierOptions
} = workerData
!
18 FastifyWorker
.fastify
= Fastify({
22 await FastifyWorker
.fastify
.register(
24 fastifyPoolifierOptions
27 FastifyWorker
.fastify
.all('/api/echo', async request
=> {
29 await FastifyWorker
.fastify
.execute({ data
: request
.body
}, 'echo')
33 FastifyWorker
.fastify
.get
<{
34 Params
: { number: number }
35 }>('/api/factorial/:number', async request
=> {
36 const { number } = request
.params
38 await FastifyWorker
.fastify
.execute({ data
: { number } }, 'factorial')
42 await FastifyWorker
.fastify
.listen({ port
})
45 port
: (FastifyWorker
.fastify
.server
.address() as AddressInfo
)?.port
49 public constructor () {
50 super(FastifyWorker
.startFastify
, {
51 killHandler
: async () => {
52 await FastifyWorker
.fastify
.pool
.destroy()
53 await FastifyWorker
.fastify
.close()
59 export const fastifyWorker
= new FastifyWorker()