1
0
mirror of https://github.com/tcgdex/cards-database.git synced 2025-04-22 10:52:10 +00:00

feat: setup clustering (#530)

This commit is contained in:
Florian Bouillon 2024-08-30 12:10:05 +02:00 committed by GitHub
parent 5928a1dd25
commit ae6ed3cdaa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,17 +1,38 @@
import express, { type Response } from 'express'
import jsonEndpoints from './V2/endpoints/jsonEndpoints'
import graphql from './V2/graphql'
import cluster from 'node:cluster'
import { availableParallelism } from "node:os"
import { Errors, sendError } from './libs/Errors'
import status from './status'
import jsonEndpoints from './V2/endpoints/jsonEndpoints'
import graphql from './V2/graphql'
// Current API version
const VERSION = 2
if (cluster.isPrimary) {
console.log(`Primary ${process.pid} is running`);
// Init Express server
const server = express()
const parallelism = availableParallelism()
console.log(`creating ${parallelism} workers...`)
for (let i = 0; i < parallelism; i++) {
cluster.fork();
}
// Route logging / Error logging for debugging
server.use((req, res, next) => {
cluster.on('online', (worker) => {
console.log('Worker', worker.id, 'online')
})
cluster.on("exit", (worker, code, _signal) => {
console.log(`Worker ${worker.id} exited with code ${code}`);
})
console.log('🚀 Server ready at localhost:3000');
} else {
// Current API version
const VERSION = 2
// Init Express server
const server = express()
// Route logging / Error logging for debugging
server.use((req, res, next) => {
const now = new Date()
// Date of request User-Agent 32 first chars request Method
const prefix = `\x1b[2m${now.toISOString()}\x1b[22m ${req.headers['user-agent']?.slice(0, 32).padEnd(32)} ${req.method.toUpperCase().padEnd(7)}`
@ -41,10 +62,10 @@ server.use((req, res, next) => {
})
next()
})
})
// Set CORS global headers
server.use((req, res, next) => {
// Set CORS global headers
server.use((req, res, next) => {
res
.setHeader('Access-Control-Allow-Origin', '*')
.setHeader('Access-Control-Allow-Methods', 'GET,POST,OPTIONS')
@ -56,30 +77,30 @@ server.use((req, res, next) => {
return
}
next()
})
})
server.get('/', (_, res) => {
server.get('/', (_, res) => {
res.redirect('https://www.tcgdex.dev/?ref=api.tcgdex.net')
})
})
server.use(express.static('./public'))
server.use(express.static('./public'))
// Setup GraphQL
server.use(`/v${VERSION}/graphql`, graphql)
// Setup GraphQL
server.use(`/v${VERSION}/graphql`, graphql)
// Setup JSON endpoints
server.use(`/v${VERSION}`, jsonEndpoints)
// Setup JSON endpoints
server.use(`/v${VERSION}`, jsonEndpoints)
// Status page
server.use('/status', status)
// Status page
server.use('/status', status)
// handle 404 errors
server.use((_, res) => {
// handle 404 errors
server.use((_, res) => {
sendError(Errors.NOT_FOUND, res)
})
})
// General error handler
server.use((err: Error, _1: unknown, res: Response, _2: unknown) => {
// General error handler
server.use((err: Error, _1: unknown, res: Response, _2: unknown) => {
// add a full line dash to not miss it
const columns = (process?.stdout?.columns ?? 32) - 7
const dashes = ''.padEnd(columns / 2, '-')
@ -90,8 +111,8 @@ server.use((err: Error, _1: unknown, res: Response, _2: unknown) => {
console.error(`\x1b[91m${dashes} ERROR ${dashes}\x1b[0m`)
sendError(Errors.GENERAL, res, { err })
})
})
// Start server
server.listen(3000)
console.log('🚀 Server ready at localhost:3000');
// Start server
server.listen(3000)
}