derv-net/routes/init.js

152 lines
3.7 KiB
JavaScript
Raw Permalink Normal View History

2025-02-25 19:47:28 -05:00
import { importRouters } from "../lib.js";
import multer from "multer";
2025-02-25 22:53:22 -05:00
import cookieParser from "cookie-parser";
2025-02-25 19:47:28 -05:00
import { initDb } from "../db.js";
2025-02-26 16:37:48 -05:00
import express from "express";
2025-02-26 20:17:33 -05:00
import { randomBytes } from 'node:crypto';
2025-02-25 19:47:28 -05:00
let db = await initDb();
2025-03-02 15:28:47 -05:00
const mimes = {
'image/gif': 'gif',
'image/jpeg': 'jpg',
'text/plain': 'txt',
'application/json': 'json',
'video/mp4': 'mp4',
'video/webm': 'webm',
'image/png': 'png',
'audio/mpeg': 'mp3',
'audio/wav': 'wav',
};
2025-02-26 20:17:33 -05:00
const upload = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './uploads/')
},
filename: function (req, file, cb) {
const uniqueSuffix = randomBytes(8).toString('hex');
2025-03-02 15:28:47 -05:00
const type = mimes[file.mimetype] || 'txt';
cb(null, uniqueSuffix + '.' + type)
2025-02-26 20:17:33 -05:00
}
});
const upload2 = multer({ storage: upload });
2025-02-25 19:47:28 -05:00
const aliases = {
2025-02-26 01:10:09 -05:00
'/': '/walls/get/hub/main/0'
2025-02-25 19:47:28 -05:00
};
const routers = {
'/you': './routes/you.js',
2025-02-26 01:10:09 -05:00
'/api/form/you': './routes/youApi.js',
2025-02-26 22:12:46 -05:00
'/api/file/you': './routes/youApi.js',
2025-02-26 01:40:27 -05:00
'/api/file/comment': './routes/commenter.js',
2025-02-26 01:10:09 -05:00
'/walls': './routes/walls.js',
2025-02-26 22:12:46 -05:00
'/comment': './routes/comment.js',
2025-03-02 02:45:13 -05:00
'/users': './routes/user.js',
'/info': './routes/info.js'
2025-02-25 19:47:28 -05:00
}
function doAliases(app) {
for (let alias in aliases) {
2025-02-25 22:53:22 -05:00
let aliasV = aliases[alias];
2025-02-25 19:47:28 -05:00
app.all(alias, (req, res, next) => {
2025-02-25 22:53:22 -05:00
res.redirect(aliasV)
2025-02-25 19:47:28 -05:00
})
}
}
async function auther(req, res, next) {
2025-02-25 22:53:22 -05:00
let { token } = req.cookies;
2025-02-25 19:47:28 -05:00
2025-02-25 22:53:22 -05:00
if (!token) {
res.auth = {};
2025-02-25 22:59:18 -05:00
next();
2025-02-25 22:53:22 -05:00
return;
}
2025-02-25 19:47:28 -05:00
let match = await db.all('SELECT * FROM token WHERE token = ?', [
token || 'blah'
]);
let username = match[0] ? match[0].username : '!nobody';
2025-02-25 22:53:22 -05:00
let valid = false;
if (username != '!nobody') {
valid = await db.all('SELECT * FROM auth WHERE username = ?', [
username
]);
}
2025-02-25 19:47:28 -05:00
res.auth = {
username,
2025-02-25 22:53:22 -05:00
valid: valid[0]
2025-02-25 19:47:28 -05:00
};
next();
}
2025-03-03 13:51:06 -05:00
async function initr(req, res, next) {
let msgCount = 0;
if (res.auth.valid) {
let messages = await db.all('SELECT count(*) FROM messages WHERE username = ? AND status = ?', [
res.auth.username,
'unread'
]);
msgCount = messages[0]['count(*)'];
}
2025-02-25 19:47:28 -05:00
let headerCtx = [
2025-02-26 22:12:46 -05:00
{ link: '/walls/get/hub/main/0', icon: '/icon.svg', name: 'DervNet' },
2025-02-25 19:47:28 -05:00
{ link: '/walls/list', icon: '/walls.svg', name: 'Explore' },
2025-02-27 11:51:22 -05:00
{ link: '/users/' + (res.auth ? res.auth.username : ''), icon: '/login.svg', name: 'You' },
2025-03-03 13:51:06 -05:00
{ link: '/you/messages', icon: '/mail.svg', name: `Messages [${msgCount}]` },
2025-03-02 15:28:47 -05:00
{ link: '/you/logout', icon: '/logout.svg', name: 'Leave' }
2025-02-25 19:47:28 -05:00
];
if (!res.auth || !res.auth.valid) {
headerCtx = [
2025-02-26 22:12:46 -05:00
{ link: '/walls/get/hub/main/0', icon: '/icon.svg', name: 'DervNet' },
2025-02-25 22:59:18 -05:00
{ link: '/walls/list', icon: '/walls.svg', name: 'Explore' },
{ link: '/you/login', icon: '/login.svg', name: 'Log in' },
{ link: '/you/new', icon: '/join.svg', name: 'Join' }
2025-02-25 19:47:28 -05:00
];
}
res.ctx = {
mainPage: '404.ejs',
mainCtx: {},
headerCtx
};
next();
}
async function doInit(app) {
app.use(cookieParser());
2025-02-25 22:53:22 -05:00
doAliases(app);
2025-02-26 16:37:48 -05:00
app.use(express.static('public'));
app.use('/uploads', express.static('uploads'));
2025-02-25 19:47:28 -05:00
app.use(auther)
app.use(initr)
2025-02-26 20:17:33 -05:00
app.use('/api/form', upload2.none());
app.use('/api/file', upload2.single('file'));
2025-02-25 19:47:28 -05:00
await importRouters(app, routers);
app.use('/api', (req, res, next) => {
res.send(res.api);
})
app.use((req, res, next) => {
res.render('root.ejs', res.ctx)
})
}
export { doInit };