Got working preact SSR

This commit is contained in:
2022-11-07 02:02:23 +01:00
parent 5142d4c551
commit ddad7fe746
16 changed files with 229 additions and 200 deletions
+7
View File
@@ -0,0 +1,7 @@
export const initialDatabaseValue = {
users: {
['BachoSeven']: {},
['aziis98']: {},
},
problems: {},
}
-79
View File
@@ -1,79 +0,0 @@
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...`)
})
+86
View File
@@ -0,0 +1,86 @@
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 { initialDatabaseValue } from './db/example-data.js'
import { useId } from 'preact/hooks'
export async function createApiRouter() {
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', initialDatabaseValue)
const r = express.Router()
r.use(bodyParser.json())
r.use(cookieParser())
r.use(loggingMiddleware)
r.use(authMiddleware(sid => sessions.getUserForSession(sid)))
r.use('/api/status', new StatusRouter())
r.use('/api/ping', new PingRouter())
r.get('/api/current-user', async (req, res) => {
const userId = sessions.getUserForSession(req.cookies.sid)
if (!userId) {
res.cookie('sid', '', { expires: new Date() })
res.status(400)
res.end('Invalid session token')
return
}
const user = await getUser(db, userId)
res.json({
username: userId,
...user,
})
})
r.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' })
})
r.post('/api/logout', (req, res) => {
res.cookie('sid', '', { expires: new Date() })
res.json({ status: 'ok' })
})
r.get('/api/user/:id', async (req, res) => {
const user = await getUser(db, req.params.id)
if (user) {
res.json(user)
} else {
res.sendStatus(404)
}
})
r.post('/api/user/:id', async (req, res) => {
await updateUser(db, req.params.id, req.body)
res.sendStatus(200)
})
// r.all('*', (_req, res) => {
// res.sendStatus(404)
// })
return r
}