index html only on real routes otherwise redirect + better logging
This commit is contained in:
@@ -8,16 +8,16 @@ async function withDatabase({ path, initialValue }, fn) {
|
||||
try {
|
||||
await access(path, constants.R_OK)
|
||||
} catch (e) {
|
||||
console.log(`Creating empty database into "${path}"`)
|
||||
console.log(`[Database] Creating empty database into "${path}"`)
|
||||
await writeFile(path, JSON.stringify(initialValue, null, 2))
|
||||
}
|
||||
|
||||
console.log(`Loading database from "${path}"`)
|
||||
console.log(`[Database] Loading database from "${path}"`)
|
||||
const state = JSON.parse(await readFile(path, 'utf-8'))
|
||||
|
||||
const result = await fn(state)
|
||||
|
||||
console.log(`Saving database to "${path}"`)
|
||||
console.log(`[Database] Saving database to "${path}"`)
|
||||
await writeFile(path, JSON.stringify(state, null, 2))
|
||||
|
||||
return result
|
||||
|
||||
@@ -28,20 +28,6 @@ export class PingRouter extends Router {
|
||||
}
|
||||
}
|
||||
|
||||
export const loggingMiddleware = (req, res, next) => {
|
||||
next()
|
||||
|
||||
const coloredStatusCode = [
|
||||
chalk.gray,
|
||||
chalk.green,
|
||||
chalk.blueBright,
|
||||
chalk.red,
|
||||
chalk.redBright,
|
||||
][Math.floor(res.statusCode / 100) - 1](res.statusCode)
|
||||
|
||||
console.log(`${toLocalISO(new Date())} | ${req.method} ${req.originalUrl} ${coloredStatusCode}`)
|
||||
}
|
||||
|
||||
export const authMiddleware = getUserForSession => async (req, res, next) => {
|
||||
if (req.cookies.sid) {
|
||||
req.user = await getUserForSession(req.cookies.sid)
|
||||
|
||||
@@ -3,11 +3,11 @@ import crypto from 'crypto'
|
||||
import bodyParser from 'body-parser'
|
||||
import cookieParser from 'cookie-parser'
|
||||
|
||||
import { authMiddleware, loggingMiddleware, PingRouter, StatusRouter } from './middlewares.js'
|
||||
import { createDatabase, getUser, updateUser } from './db/database.js'
|
||||
import express from 'express'
|
||||
|
||||
import { authMiddleware, PingRouter, StatusRouter } from './middlewares.js'
|
||||
import { createDatabase, getUser, updateUser } from './db/database.js'
|
||||
import { initialDatabaseValue } from './db/example-data.js'
|
||||
import { useId } from 'preact/hooks'
|
||||
|
||||
export async function createApiRouter() {
|
||||
const sessions = {
|
||||
@@ -29,7 +29,6 @@ export async function createApiRouter() {
|
||||
r.use(bodyParser.json())
|
||||
r.use(cookieParser())
|
||||
|
||||
r.use(loggingMiddleware)
|
||||
r.use(authMiddleware(sid => sessions.getUserForSession(sid)))
|
||||
|
||||
r.use('/api/status', new StatusRouter())
|
||||
@@ -78,9 +77,5 @@ export async function createApiRouter() {
|
||||
res.sendStatus(200)
|
||||
})
|
||||
|
||||
// r.all('*', (_req, res) => {
|
||||
// res.sendStatus(404)
|
||||
// })
|
||||
|
||||
return r
|
||||
}
|
||||
Reference in New Issue
Block a user