X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=examples%2Ftypescript%2Fhttp-server-pool%2Ffastify-cluster%2Fsrc%2Fworker.ts;h=4084bf71fb7b6461311e5a2490ed6ab6bbe9c7a3;hb=3d49c6d2f943532974cefa71f13af2c313555d95;hp=f679b4a6a9e3883db25909ec52f780259d319fa7;hpb=abdb851534195a6ec554bbb2c38501044d27f6b7;p=poolifier.git diff --git a/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts b/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts index f679b4a6..4084bf71 100644 --- a/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts +++ b/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts @@ -1,6 +1,6 @@ import type { AddressInfo } from 'node:net' import { ClusterWorker } from 'poolifier' -import Fastify from 'fastify' +import Fastify, { type FastifyInstance } from 'fastify' import type { WorkerData, WorkerResponse } from './types.js' const factorial: (n: number) => number = n => { @@ -10,35 +10,37 @@ const factorial: (n: number) => number = n => { return factorial(n - 1) * n } -const startFastify = async ( - workerData?: WorkerData -): Promise => { - const { port } = workerData as WorkerData - const fastify = Fastify({ - logger: true - }) - - fastify.all('/api/echo', request => { - return request.body - }) - - fastify.get<{ - Params: { number: number } - }>('/api/factorial/:number', request => { - const { number } = request.params - return { number: factorial(number) } - }) - - await fastify.listen({ port }) - return { - status: true, - port: (fastify.server.address() as AddressInfo).port +class FastifyWorker extends ClusterWorker { + private static fastify: FastifyInstance + + private static readonly startFastify = async ( + workerData?: WorkerData + ): Promise => { + const { port } = workerData as WorkerData + FastifyWorker.fastify = Fastify({ + logger: true + }) + + FastifyWorker.fastify.all('/api/echo', request => { + return request.body + }) + + FastifyWorker.fastify.get<{ + Params: { number: number } + }>('/api/factorial/:number', request => { + const { number } = request.params + return { number: factorial(number) } + }) + + await FastifyWorker.fastify.listen({ port }) + return { + status: true, + port: (FastifyWorker.fastify.server.address() as AddressInfo).port + } } -} -class FastifyWorker extends ClusterWorker { public constructor () { - super(startFastify) + super(FastifyWorker.startFastify) } }