fix: refine type definition for transferList
[poolifier.git] / examples / typescript / http-server-pool / fastify-worker_threads / src / main.ts
... / ...
CommitLineData
1import { dirname, extname, join } from 'node:path'
2import { exit } from 'node:process'
3import { fileURLToPath } from 'node:url'
4
5import Fastify from 'fastify'
6
7import { fastifyPoolifier } from './fastify-poolifier.js'
8
9/**
10 * The fastify server is still a single-threaded application, but the request handling can be multi-threaded.
11 */
12
13const port = 8080
14const fastify = Fastify({
15 logger: true
16})
17
18const workerFile = join(
19 dirname(fileURLToPath(import.meta.url)),
20 `worker${extname(fileURLToPath(import.meta.url))}`
21)
22
23await fastify.register(fastifyPoolifier, {
24 workerFile,
25 enableTasksQueue: true,
26 tasksQueueOptions: {
27 concurrency: 8
28 },
29 errorHandler: (e: Error) => {
30 fastify.log.error('Thread worker error:', e)
31 }
32})
33
34fastify.all('/api/echo', async request => {
35 return (await fastify.execute({ body: request.body }, 'echo')).body
36})
37
38fastify.get<{
39 Params: { number: number }
40}>('/api/factorial/:number', async request => {
41 const { number } = request.params
42 return (await fastify.execute({ body: { number } }, 'factorial')).body
43})
44
45try {
46 await fastify.listen({ port })
47} catch (err) {
48 fastify.log.error(err)
49 exit(1)
50}