61 lines
1.4 KiB
JavaScript
61 lines
1.4 KiB
JavaScript
import { performance } from 'perf_hooks'
|
|
import { Flaska, QueryHandler } from 'flaska'
|
|
|
|
import TestRoutes from './test/routes.mjs'
|
|
import MediaRoutes from './media/routes.mjs'
|
|
|
|
import config from './config.mjs'
|
|
import log from './log.mjs'
|
|
|
|
const app = new Flaska({
|
|
log: log,
|
|
})
|
|
|
|
app.before(function(ctx) {
|
|
ctx.__started = performance.now()
|
|
})
|
|
|
|
app.after(function(ctx) {
|
|
let ended = performance.now() - ctx.__started
|
|
let logger = ctx.log.info
|
|
if (ctx.status >= 400) {
|
|
logger = ctx.log.warn
|
|
}
|
|
console.log('')
|
|
console.log({
|
|
path: ctx.url,
|
|
status: ctx.status,
|
|
ms: Math.round(ended),
|
|
}, 'Request finished')
|
|
logger({
|
|
path: ctx.url,
|
|
status: ctx.status,
|
|
ms: Math.round(ended),
|
|
}, 'Request finished')
|
|
})
|
|
|
|
app.onerror(function(err, ctx) {
|
|
if (err.status && err.status >= 400 && err.status < 500) {
|
|
ctx.log.warn(err.message)
|
|
} else {
|
|
ctx.log.error(err)
|
|
}
|
|
ctx.status = err.status || 500
|
|
ctx.body = {
|
|
status: ctx.status,
|
|
message: err.message,
|
|
}
|
|
})
|
|
|
|
const test = new TestRoutes()
|
|
app.get('/', test.static.bind(test))
|
|
app.get('/error', test.error.bind(test))
|
|
|
|
const media = new MediaRoutes()
|
|
app.post('/media', [QueryHandler()], media.upload.bind(media))
|
|
|
|
app.listen(config.get('server:port'), function(a,b) {
|
|
log.info(`Server listening at ${config.get('server:port')}`)
|
|
})
|
|
|
|
export default app
|