X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=examples%2Ftypescript%2Fhttp-server-pool%2Ffastify-cluster%2Fsrc%2Fworker.ts;h=2f6c8f6dec1fcf405c8ce5bdb78a51c84c7c1915;hb=54ada4e841457cce94576b2b3870cbd1329503d3;hp=eb546ef0a9d82c189d2ef5b799395321c7d8df62;hpb=5daad283d56413b7287cca894637a35e51a1222c;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 eb546ef0..2f6c8f6d 100644 --- a/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts +++ b/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts @@ -3,39 +3,40 @@ import { ClusterWorker } from 'poolifier' import Fastify, { type FastifyInstance } from 'fastify' import type { WorkerData, WorkerResponse } from './types.js' -const factorial: (n: number) => number = (n) => { - if (n === 0) { - return 1 - } - return factorial(n - 1) * n -} - class FastifyWorker extends ClusterWorker { private static fastify: FastifyInstance + private static readonly factorial = (n: number): number => { + if (n === 0) { + return 1 + } + return FastifyWorker.factorial(n - 1) * n + } + private static readonly startFastify = async ( workerData?: WorkerData ): Promise => { const { port } = workerData as WorkerData + FastifyWorker.fastify = Fastify({ logger: true }) - FastifyWorker.fastify.all('/api/echo', (request) => { + FastifyWorker.fastify.all('/api/echo', request => { return request.body }) FastifyWorker.fastify.get<{ Params: { number: number } - }>('/api/factorial/:number', (request) => { + }>('/api/factorial/:number', request => { const { number } = request.params - return { number: factorial(number) } + return { number: FastifyWorker.factorial(number) } }) await FastifyWorker.fastify.listen({ port }) return { status: true, - port: (FastifyWorker.fastify.server.address() as AddressInfo).port + port: (FastifyWorker.fastify.server.address() as AddressInfo)?.port } }