mirror of
https://github.com/tcgdex/cards-database.git
synced 2025-04-23 19:32:11 +00:00
Merge branch 'master' into feat/add-openapi-explorer
This commit is contained in:
commit
8f288dfc08
@ -2,17 +2,38 @@ import express, { type Response } from 'express'
|
|||||||
import jsonEndpoints from './V2/endpoints/jsonEndpoints'
|
import jsonEndpoints from './V2/endpoints/jsonEndpoints'
|
||||||
import openapi from './V2/endpoints/openapi'
|
import openapi from './V2/endpoints/openapi'
|
||||||
import graphql from './V2/graphql'
|
import graphql from './V2/graphql'
|
||||||
|
import cluster from 'node:cluster'
|
||||||
|
import { availableParallelism } from "node:os"
|
||||||
import { Errors, sendError } from './libs/Errors'
|
import { Errors, sendError } from './libs/Errors'
|
||||||
import status from './status'
|
import status from './status'
|
||||||
|
|
||||||
// Current API version
|
if (cluster.isPrimary) {
|
||||||
const VERSION = 2
|
console.log(`Primary ${process.pid} is running`);
|
||||||
|
|
||||||
// Init Express server
|
const parallelism = availableParallelism()
|
||||||
const server = express()
|
console.log(`creating ${parallelism} workers...`)
|
||||||
|
for (let i = 0; i < parallelism; i++) {
|
||||||
|
cluster.fork();
|
||||||
|
}
|
||||||
|
|
||||||
// Route logging / Error logging for debugging
|
cluster.on('online', (worker) => {
|
||||||
server.use((req, res, next) => {
|
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()
|
const now = new Date()
|
||||||
// Date of request User-Agent 32 first chars request Method
|
// 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)}`
|
const prefix = `\x1b[2m${now.toISOString()}\x1b[22m ${req.headers['user-agent']?.slice(0, 32).padEnd(32)} ${req.method.toUpperCase().padEnd(7)}`
|
||||||
@ -42,10 +63,10 @@ server.use((req, res, next) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
next()
|
next()
|
||||||
})
|
})
|
||||||
|
|
||||||
// Set CORS global headers
|
// Set CORS global headers
|
||||||
server.use((req, res, next) => {
|
server.use((req, res, next) => {
|
||||||
res
|
res
|
||||||
.setHeader('Access-Control-Allow-Origin', '*')
|
.setHeader('Access-Control-Allow-Origin', '*')
|
||||||
.setHeader('Access-Control-Allow-Methods', 'GET,POST,OPTIONS')
|
.setHeader('Access-Control-Allow-Methods', 'GET,POST,OPTIONS')
|
||||||
@ -57,31 +78,31 @@ server.use((req, res, next) => {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
next()
|
next()
|
||||||
})
|
})
|
||||||
|
|
||||||
server.get('/', (_, res) => {
|
server.get('/', (_, res) => {
|
||||||
res.redirect('https://www.tcgdex.dev/?ref=api.tcgdex.net')
|
res.redirect('https://www.tcgdex.dev/?ref=api.tcgdex.net')
|
||||||
})
|
})
|
||||||
|
|
||||||
server.use(express.static('./public'))
|
server.use(express.static('./public'))
|
||||||
|
|
||||||
// Setup GraphQL
|
// Setup GraphQL
|
||||||
server.use(`/v${VERSION}/graphql`, graphql)
|
server.use(`/v${VERSION}/graphql`, graphql)
|
||||||
server.use(`/v${VERSION}/openapi`, openapi)
|
server.use(`/v${VERSION}/openapi`, openapi)
|
||||||
|
|
||||||
// Setup JSON endpoints
|
// Setup JSON endpoints
|
||||||
server.use(`/v${VERSION}`, jsonEndpoints)
|
server.use(`/v${VERSION}`, jsonEndpoints)
|
||||||
|
|
||||||
// Status page
|
// Status page
|
||||||
server.use('/status', status)
|
server.use('/status', status)
|
||||||
|
|
||||||
// handle 404 errors
|
// handle 404 errors
|
||||||
server.use((_, res) => {
|
server.use((_, res) => {
|
||||||
sendError(Errors.NOT_FOUND, res)
|
sendError(Errors.NOT_FOUND, res)
|
||||||
})
|
})
|
||||||
|
|
||||||
// General error handler
|
// General error handler
|
||||||
server.use((err: Error, _1: unknown, res: Response, _2: unknown) => {
|
server.use((err: Error, _1: unknown, res: Response, _2: unknown) => {
|
||||||
// add a full line dash to not miss it
|
// add a full line dash to not miss it
|
||||||
const columns = (process?.stdout?.columns ?? 32) - 7
|
const columns = (process?.stdout?.columns ?? 32) - 7
|
||||||
const dashes = ''.padEnd(columns / 2, '-')
|
const dashes = ''.padEnd(columns / 2, '-')
|
||||||
@ -92,8 +113,8 @@ server.use((err: Error, _1: unknown, res: Response, _2: unknown) => {
|
|||||||
console.error(`\x1b[91m${dashes} ERROR ${dashes}\x1b[0m`)
|
console.error(`\x1b[91m${dashes} ERROR ${dashes}\x1b[0m`)
|
||||||
|
|
||||||
sendError(Errors.GENERAL, res, { err })
|
sendError(Errors.GENERAL, res, { err })
|
||||||
})
|
})
|
||||||
|
|
||||||
// Start server
|
// Start server
|
||||||
server.listen(3000)
|
server.listen(3000)
|
||||||
console.log('🚀 Server ready at localhost:3000');
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user