Intermediate refactoring...
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import { readFile, writeFile, access, constants } from 'fs/promises'
|
||||
|
||||
export function createDatabase(path, initialValue) {
|
||||
return { path, initialValue }
|
||||
}
|
||||
|
||||
async function withDatabase({ path, initialValue }, fn) {
|
||||
try {
|
||||
await access(path, constants.R_OK)
|
||||
} catch (e) {
|
||||
console.log(`Creating empty database into "${path}"`)
|
||||
await writeFile(path, JSON.stringify(initialValue, null, 2))
|
||||
}
|
||||
|
||||
console.log(`Loading database from "${path}"`)
|
||||
const state = JSON.parse(await readFile(path, 'utf-8'))
|
||||
|
||||
const result = await fn(state)
|
||||
|
||||
console.log(`Saving database to "${path}"`)
|
||||
await writeFile(path, JSON.stringify(state, null, 2))
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
function createTable(tableName) {
|
||||
return {
|
||||
create() {},
|
||||
get() {},
|
||||
update() {},
|
||||
delete() {},
|
||||
}
|
||||
}
|
||||
|
||||
export const User = createTable('users')
|
||||
export const Problem = createTable('problems')
|
||||
|
||||
export const getUser = (db, id) =>
|
||||
withDatabase(db, state => {
|
||||
return state.users[id]
|
||||
})
|
||||
|
||||
export const createUser = (db, { email, username, password }) =>
|
||||
withDatabase(db, state => {
|
||||
state.users[email] = {
|
||||
username,
|
||||
password,
|
||||
}
|
||||
})
|
||||
|
||||
export const updateUser = (db, username, { email, password }) =>
|
||||
withDatabase(db, state => {
|
||||
state.users[username] = {
|
||||
email,
|
||||
password,
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,79 @@
|
||||
import express from 'express'
|
||||
|
||||
import crypto from 'crypto'
|
||||
|
||||
// import serveStatic from 'serve-static'
|
||||
import bodyParser from 'body-parser'
|
||||
import cookieParser from 'cookie-parser'
|
||||
|
||||
import { authMiddleware, loggingMiddleware, PingRouter, StatusRouter } from './server/routes.js'
|
||||
import { createDatabase, getUser, updateUser } from './server/db/database.js'
|
||||
|
||||
const app = express()
|
||||
|
||||
const sessions = {
|
||||
store: {},
|
||||
createSession(username) {
|
||||
const sid = crypto.randomBytes(10).toString('hex')
|
||||
this.store[sid] = username
|
||||
return sid
|
||||
},
|
||||
getUserForSession(sid) {
|
||||
return this.store[sid] ?? null
|
||||
},
|
||||
}
|
||||
|
||||
const db = createDatabase('./db.local.json', {
|
||||
users: {
|
||||
['BachoSeven']: {},
|
||||
['aziis98']: {},
|
||||
},
|
||||
problems: {},
|
||||
})
|
||||
|
||||
app.use(bodyParser.json())
|
||||
app.use(cookieParser())
|
||||
|
||||
app.use(loggingMiddleware)
|
||||
app.use(authMiddleware(sid => sessions.getUserForSession(sid)))
|
||||
|
||||
app.use('/api/status', new StatusRouter())
|
||||
app.use('/api/ping', new PingRouter())
|
||||
|
||||
app.get('/api/current-user', (req, res) => {
|
||||
res.json(sessions.getUserForSession(req.cookies.sid))
|
||||
})
|
||||
|
||||
app.post('/api/login', (req, res) => {
|
||||
const { username } = req.body
|
||||
res.cookie('sid', sessions.createSession(username), { maxAge: 1000 * 60 * 60 * 24 * 7 })
|
||||
res.json({ status: 'ok' })
|
||||
})
|
||||
|
||||
app.post('/api/logout', (req, res) => {
|
||||
res.cookie('sid', '', { expires: new Date() })
|
||||
res.json({ status: 'ok' })
|
||||
})
|
||||
|
||||
app.get('/api/user/:id', async (req, res) => {
|
||||
const user = await getUser(db, req.params.id)
|
||||
|
||||
if (user) {
|
||||
res.json(user)
|
||||
} else {
|
||||
res.sendStatus(404)
|
||||
}
|
||||
})
|
||||
|
||||
app.post('/api/user/:id', async (req, res) => {
|
||||
await updateUser(db, req.params.id, req.body)
|
||||
res.sendStatus(200)
|
||||
})
|
||||
|
||||
app.all('*', (_req, res) => {
|
||||
res.sendStatus(404)
|
||||
})
|
||||
|
||||
app.listen(4000, () => {
|
||||
console.log(`Started server on port 4000...`)
|
||||
})
|
||||
Reference in New Issue
Block a user