-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
89 lines (68 loc) · 2.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const express = require("express");
const mds = require("markdown-serve");
const fs = require("fs");
const autoprefixer = require('express-autoprefixer');
const lessMiddleware = require('less-middleware');
const app = express();
const PORT = process.env.PORT || 3000;
const dataDir = "./data";
const dataPageDir = `${dataDir}/pages`;
app.set("view engine", "ejs");
app.set("view cache", "true");
const staticPath = __dirname + '/static';
app.use(lessMiddleware(staticPath));
app.use(autoprefixer({browsers: ["last 3 versions", "> 1%"], cascade: false}));
app.use(express.static(staticPath));
const validate = str => str.match(/^[0-9a-zA-Z-_]+$/);
const indexConfig = require(`${dataDir}/index.json`);
app.get("/", (req, res) => {
return res.render("index", indexConfig);
});
const render = (res, club, page) => {
const configPath = `${dataPageDir}/${club}.json`;
if(fs.existsSync(configPath)) {
const config = require(configPath);
const pagePath = `${club}/pages/${page}`;
const currentPage = config.pages.find(({name}) => name === page);
if(currentPage !== undefined) {
config.currentPage = currentPage;
if(fs.existsSync(`./views/${pagePath}.ejs`)) {
res.render(pagePath, config);
} else {
if(fs.existsSync(`./views/${club}/pages/index.ejs`)) {
res.render(`${club}/pages/index`, config);
} else {
res.render(`default/pages/index`, config);
}
}
return true;
}
}
return false;
}
app.get("/:club", (req, res, next) => {
if(!req.url.endsWith("/")) return res.redirect(req.url + "/");
const {club} = req.params;
if(!validate(club)) return next();
const configPath = `${dataPageDir}/${club}.json`;
if(fs.existsSync(configPath)) {
if(render(res, club, "index")) return;
}
next();
});
app.get("/:club/:page", (req, res, next) => {
const {club, page} = req.params;
if(!validate(club) || !validate(page)) return next();
if(page === "index") return res.redirect(`/${club}/`);
if(!render(res, club, page)) next();
});
const redirects = indexConfig.redirects;
app.get("/redirect/:page", (req, res) => {
if(Object.keys(redirects).includes(req.params["page"])) return res.redirect(redirects[req.params["page"]]);
return res.send("Invalid url");
});
app.use(mds.middleware({
rootDirectory: staticPath,
view: 'markdown'
}));
app.listen(PORT, () => console.log(`Started server at port ${PORT}`));