minor optimizations
This commit is contained in:
parent
d4d2303796
commit
cede9d3021
1 changed files with 18 additions and 11 deletions
|
@ -1,6 +1,6 @@
|
||||||
import { importRouters } from "../lib.js";
|
import { importRouters } from "../lib.js";
|
||||||
import multer from "multer";
|
import multer from "multer";
|
||||||
import cookieParser from "cookie-parser";
|
import cookieParser from "cookie-parser";
|
||||||
import { initDb } from "../db.js";
|
import { initDb } from "../db.js";
|
||||||
|
|
||||||
let db = await initDb();
|
let db = await initDb();
|
||||||
|
@ -18,17 +18,21 @@ const routers = {
|
||||||
|
|
||||||
function doAliases(app) {
|
function doAliases(app) {
|
||||||
for (let alias in aliases) {
|
for (let alias in aliases) {
|
||||||
|
let aliasV = aliases[alias];
|
||||||
app.all(alias, (req, res, next) => {
|
app.all(alias, (req, res, next) => {
|
||||||
res.redirect(aliases[alias])
|
res.redirect(aliasV)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function auther(req, res, next) {
|
async function auther(req, res, next) {
|
||||||
let body = { ...req.cookies, ...req.body };
|
let { token } = req.cookies;
|
||||||
|
|
||||||
let { token } = body;
|
if (!token) {
|
||||||
|
res.auth = {};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let match = await db.all('SELECT * FROM token WHERE token = ?', [
|
let match = await db.all('SELECT * FROM token WHERE token = ?', [
|
||||||
token || 'blah'
|
token || 'blah'
|
||||||
|
@ -36,14 +40,17 @@ async function auther(req, res, next) {
|
||||||
|
|
||||||
let username = match[0] ? match[0].username : '!nobody';
|
let username = match[0] ? match[0].username : '!nobody';
|
||||||
|
|
||||||
let valid = await db.all('SELECT * FROM auth WHERE username = ?', [
|
let valid = false;
|
||||||
username
|
|
||||||
]);
|
if (username != '!nobody') {
|
||||||
|
valid = await db.all('SELECT * FROM auth WHERE username = ?', [
|
||||||
|
username
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
res.auth = {
|
res.auth = {
|
||||||
username,
|
username,
|
||||||
valid: valid[0],
|
valid: valid[0]
|
||||||
rootUrl: process.env.URL,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
next();
|
next();
|
||||||
|
@ -75,6 +82,8 @@ function initr(req, res, next) {
|
||||||
async function doInit(app) {
|
async function doInit(app) {
|
||||||
app.use(cookieParser());
|
app.use(cookieParser());
|
||||||
|
|
||||||
|
doAliases(app);
|
||||||
|
|
||||||
app.use(auther)
|
app.use(auther)
|
||||||
|
|
||||||
app.use(initr)
|
app.use(initr)
|
||||||
|
@ -82,8 +91,6 @@ async function doInit(app) {
|
||||||
app.use('/api/form', upload.none());
|
app.use('/api/form', upload.none());
|
||||||
app.use('/api/file', upload.single('file'));
|
app.use('/api/file', upload.single('file'));
|
||||||
|
|
||||||
doAliases(app);
|
|
||||||
|
|
||||||
await importRouters(app, routers);
|
await importRouters(app, routers);
|
||||||
|
|
||||||
app.use('/api', (req, res, next) => {
|
app.use('/api', (req, res, next) => {
|
||||||
|
|
Loading…
Reference in a new issue