1
0
mirror of https://github.com/tcgdex/cards-database.git synced 2025-07-30 19:40:48 +00:00

feat: Add better sorting/filtering/pagination (#458)

This commit is contained in:
2024-01-03 02:27:56 +01:00
committed by GitHub
parent 034b7e2cec
commit 5c8ca20a41
13 changed files with 399 additions and 178 deletions

View File

@ -1,7 +1,7 @@
import express from 'express'
import status from './status'
import jsonEndpoints from './V2/endpoints/jsonEndpoints'
import graphql from './V2/graphql'
import status from './status'
// Current API version
const VERSION = 2
@ -20,19 +20,39 @@ server.use((_, res, next) => {
// 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
let prefix = `\x1b[2m${now.toISOString()}\x1b[22m ${req.headers['user-agent']?.slice(0, 32).padEnd(32)} ${req.method.padEnd(7)}`
const url = new URL(req.url, `http://${req.headers.host}`)
const fullURL = url.toString()
const path = fullURL.slice(fullURL.indexOf(url.pathname, fullURL.indexOf(url.host)))
// HTTP Status Code Time to run request path of request
console.log(`${prefix} ${''.padStart(5, ' ')} ${''.padStart(7, ' ')} ${path}`)
res.on('close', () => {
console.log(`[${new Date().toISOString()}] ${req.headers['user-agent']?.slice(0, 32).padEnd(32)} ${req.method.padEnd(7)} ${res.statusCode} ${(req.baseUrl ?? '') + req.url}`)
console.log(`${prefix} \x1b[34m[${'statusCode' in res ? res.statusCode : '???'}]\x1b[0m \x1b[2m${(new Date().getTime() - now.getTime()).toFixed(0).padStart(5, ' ')}ms\x1b[22m ${path}`)
})
res.on('error', (err) => {
console.error('Error:')
// log the request
console.log(`${prefix} \x1b[34m[500]\x1b[0m \x1b[2m${(new Date().getTime() - now.getTime()).toFixed(0).padStart(5, ' ')}ms\x1b[22m ${path}`)
// add a full line dash to not miss it
const columns = (process?.stdout?.columns ?? 32) - 7
const dashes = ''.padEnd(columns / 2, '-')
// colorize the lines to make sur to not miss it
console.error(`\x1b[91m${dashes} ERROR ${dashes}\x1b[0m`)
console.error(err)
console.error(`\x1b[91m${dashes} ERROR ${dashes}\x1b[0m`)
})
next()
})
server.get('/', (_, res) => {
res.redirect('https://www.tcgdex.dev/?ref=api.tcgdex.net')
res.redirect('https://www.tcgdex.dev/?ref=api.tccgdex.net')
})
server.use(express.static('./public'))