derv-net/init.js

55 lines
1.2 KiB
JavaScript
Raw Normal View History

2025-02-25 16:35:22 -05:00
import { importRouters } from "./lib.js";
2025-02-25 16:02:31 -05:00
const aliases = {
'/': '/walls/get/home'
};
2025-02-25 16:35:22 -05:00
const routers = {
'/you': './you.js'
}
2025-02-25 16:02:31 -05:00
function doAliases(app) {
for (let alias in aliases) {
2025-02-25 16:35:22 -05:00
app.all(alias, (req, res, next) => {
2025-02-25 16:02:31 -05:00
res.redirect(aliases[alias])
})
}
}
2025-02-25 16:35:22 -05:00
async function doInit(app) {
2025-02-25 16:02:31 -05:00
app.use((req, res, next) => {
2025-02-25 16:35:22 -05:00
let headerCtx = [
{ link: '/walls/get/home', icon: '/icon.svg', name: 'DervNet' },
{ link: '/walls/list', icon: '/walls.svg', name: 'Explore' },
{ link: '/you/logout', icon: '/logout.svg', name: 'Leave' } // fix icon
];
if (!res.auth || !res.auth.valid) {
headerCtx = [
{ link: '/walls/get/home', icon: '/icon.svg', name: 'DervNet' },
{ link: '/you/login', icon: '/walls.svg', name: 'Log in' }
];
}
2025-02-25 16:02:31 -05:00
res.ctx = {
mainPage: '404.ejs',
mainCtx: {},
2025-02-25 16:35:22 -05:00
headerCtx
2025-02-25 16:02:31 -05:00
};
next();
})
doAliases(app);
2025-02-25 16:35:22 -05:00
await importRouters(app,routers);
2025-02-25 16:02:31 -05:00
app.use('/api', (req, res, next) => {
res.send(res.ctx);
})
app.use((req, res, next) => {
res.render('root.ejs', res.ctx)
})
}
export { doInit };