56 lines
1.8 KiB
JavaScript
56 lines
1.8 KiB
JavaScript
var express = require('express');
|
|
var path = require("path");
|
|
var fs = require("fs");
|
|
var app = express();
|
|
|
|
app.set('view engine', 'ejs');
|
|
app.use("/a", express.static(path.join(__dirname, 'assets')));
|
|
app.use("/assets", express.static(path.join(__dirname, 'assets', "Files")));
|
|
var templateIndex = (x, y) => ({"type": "page","pageName": x,"pageData": y,"allPages": pages});
|
|
var templateErrPage = (sc) => ({"tags": ["Meta",sc],"location": "system/" + sc});
|
|
var templateTag = (x, y) => ({"shortname": x,"color": y});
|
|
var pages;
|
|
var getPages = () => {
|
|
var __pg = JSON.parse(fs.readFileSync(path.join(__dirname, "views", "pages.json"), 'utf8'));
|
|
__pg.tags["Meta"] = templateTag("//","#808080");
|
|
__pg.tags["404"] = templateTag("404","#008000");
|
|
__pg.tags["500"] = templateTag("500","#FF0080");
|
|
__pg.wikiData.fullName = __pg.wikiData.name.join(" ");
|
|
pages = __pg;
|
|
}
|
|
setInterval(getPages, 10000);
|
|
getPages();
|
|
|
|
var getPage = (page) => {
|
|
if (!(pages.pages[page] || pages.redirects[page])) return {
|
|
type: "invaildrequest"
|
|
};
|
|
if (pages.redirects[page]) return {
|
|
type: "redirect",
|
|
page: pages.redirects[page]
|
|
};
|
|
return templateIndex(page, pages.pages[page]);
|
|
}
|
|
|
|
app.get('/', (_,r) => r.redirect("/wiki/Welcome"));
|
|
app.get('/wiki', (_,r) => r.redirect("/wiki/Welcome"));
|
|
|
|
app.get('/wiki/:page', function(req, res, next) {
|
|
var page = getPage(req.params.page);
|
|
switch (page.type) {
|
|
case "invaildrequest":
|
|
next();
|
|
break;
|
|
case "redirect":
|
|
res.redirect(page.page);
|
|
break;
|
|
case "page":
|
|
res.render('index', page, (err, html) => {
|
|
if (err) res.status(500).render('index', templateIndex("500_Internal_Server_Error", templateErrPage("500")));
|
|
else res.send(html);
|
|
});
|
|
}
|
|
});
|
|
|
|
app.use((_,r) => r.status(404).render('index', templateIndex("404_Not_Found", templateErrPage("404"))));
|
|
app.listen(80);
|