X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=examples%2Ftypescript%2Fhttp-server-pool%2Ffastify-hybrid%2Fsrc%2Ffastify-worker.ts;h=8e4713cfae2df7e3a158c6191a3ec57fc3d8060f;hb=e1ba9765afbf97436250d19f28c818221ebc10b9;hp=3956c8ce2a6794eb31ef5c269ffec1f620a2740e;hpb=503bda5baced9515d2fc20b85d4deb3da2dab93a;p=poolifier.git diff --git a/examples/typescript/http-server-pool/fastify-hybrid/src/fastify-worker.ts b/examples/typescript/http-server-pool/fastify-hybrid/src/fastify-worker.ts index 3956c8ce..8e4713cf 100644 --- a/examples/typescript/http-server-pool/fastify-hybrid/src/fastify-worker.ts +++ b/examples/typescript/http-server-pool/fastify-hybrid/src/fastify-worker.ts @@ -1,43 +1,60 @@ import type { AddressInfo } from 'node:net' + +import Fastify, { type FastifyInstance } from 'fastify' import { ClusterWorker } from 'poolifier' -import Fastify from 'fastify' -import type { ClusterWorkerData, ClusterWorkerResponse } from './types.js' -import { fastifyPoolifier } from './fastify-poolifier.js' -const startFastify = async ( - workerData?: ClusterWorkerData -): Promise => { - const { port } = workerData as ClusterWorkerData - const fastify = Fastify({ - logger: true - }) - - await fastify.register(fastifyPoolifier, workerData) - - fastify.all('/api/echo', async request => { - return (await fastify.execute({ body: request.body }, 'echo')).body - }) - - fastify.get<{ - Params: { number: number } - }>('/api/factorial/:number', async request => { - const { number } = request.params - return (await fastify.execute({ body: { number } }, 'factorial')).body - }) - - await fastify.listen({ port }) - return { - status: true, - port: (fastify.server.address() as AddressInfo).port - } -} +import { fastifyPoolifier } from './fastify-poolifier.js' +import type { ClusterWorkerData, ClusterWorkerResponse } from './types.js' class FastifyWorker extends ClusterWorker< ClusterWorkerData, ClusterWorkerResponse > { + private static fastify: FastifyInstance + + private static readonly startFastify = async ( + workerData?: ClusterWorkerData + ): Promise => { + const { port, ...fastifyPoolifierOptions } = workerData! + + FastifyWorker.fastify = Fastify({ + logger: true + }) + + await FastifyWorker.fastify.register( + fastifyPoolifier, + fastifyPoolifierOptions + ) + + FastifyWorker.fastify.all('/api/echo', async request => { + return ( + await FastifyWorker.fastify.execute({ data: request.body }, 'echo') + ).data + }) + + FastifyWorker.fastify.get<{ + Params: { number: number } + }>('/api/factorial/:number', async request => { + const { number } = request.params + return ( + await FastifyWorker.fastify.execute({ data: { number } }, 'factorial') + ).data + }) + + await FastifyWorker.fastify.listen({ port }) + return { + status: true, + port: (FastifyWorker.fastify.server.address() as AddressInfo).port + } + } + public constructor () { - super(startFastify) + super(FastifyWorker.startFastify, { + killHandler: async () => { + await FastifyWorker.fastify.pool.destroy() + await FastifyWorker.fastify.close() + } + }) } }