new backend
This commit is contained in:
@ -1,8 +1,9 @@
|
||||
import cfg from "../config.json";
|
||||
import cfg from "./inc/config.mjs";
|
||||
import sql from "./inc/sql.mjs";
|
||||
import lib from "./inc/lib.mjs";
|
||||
import cuffeo from "cuffeo";
|
||||
import { promises as fs } from "fs";
|
||||
|
||||
import "./websrv.mjs";
|
||||
import flummpress from "flummpress";
|
||||
|
||||
(async () => {
|
||||
const self = {
|
||||
@ -49,4 +50,64 @@ import "./websrv.mjs";
|
||||
|
||||
console.timeEnd("loading");
|
||||
|
||||
|
||||
// websrv
|
||||
const app = new flummpress();
|
||||
const router = app.router;
|
||||
const tpl = app.tpl;
|
||||
|
||||
app.use(async (req, res) => {
|
||||
// sessionhandler
|
||||
req.session = false;
|
||||
if(req.url.pathname.match(/^\/(s|b|t|ca)/))
|
||||
return;
|
||||
req.theme = req.cookies.theme ?? 'f0ck';
|
||||
|
||||
if(req.cookies.session) {
|
||||
const user = await sql("user_sessions") // get user
|
||||
.select("user.id", "user.login", "user.user", "user.level", "user_sessions.id as sess_id", "user_options.mode", "user_options.theme")
|
||||
.where("user_sessions.session", lib.md5(req.cookies.session))
|
||||
.leftJoin("user", "user.id", "user_sessions.user_id")
|
||||
.leftJoin("user_options", "user_options.user_id", "user_sessions.user_id")
|
||||
.limit(1);
|
||||
|
||||
if(user.length === 0) {
|
||||
return res.writeHead(307, { // delete session
|
||||
"Cache-Control": "no-cache, public",
|
||||
"Set-Cookie": "session=; Path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT",
|
||||
"Location": req.url.pathname
|
||||
}).end();
|
||||
}
|
||||
|
||||
req.session = user[0];
|
||||
await sql("user_sessions") // log last action
|
||||
.update("last_used", (Date.now() / 1e3))
|
||||
.update("last_action", req.url.pathname)
|
||||
.where("id", user[0].sess_id);
|
||||
|
||||
// update userprofile
|
||||
await sql("user_options")
|
||||
.insert({
|
||||
user_id: user[0].id,
|
||||
mode: user[0].mode ?? 0,
|
||||
theme: req.session.theme ?? "f0ck"
|
||||
})
|
||||
.onConflict("user_id")
|
||||
.merge();
|
||||
}
|
||||
});
|
||||
|
||||
tpl.views = "views";
|
||||
tpl.debug = true;
|
||||
tpl.cache = false;
|
||||
tpl.globals = {
|
||||
lul: cfg.websrv.lul,
|
||||
themes: cfg.websrv.themes,
|
||||
modes: cfg.allowedModes
|
||||
};
|
||||
router.use(tpl);
|
||||
|
||||
await router.importRoutesFromPath("src/inc/routes", tpl);
|
||||
|
||||
app.listen(cfg.websrv.port);
|
||||
})();
|
||||
|
Reference in New Issue
Block a user