-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
80 lines (61 loc) · 1.75 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
const express = require("express");
const {Watcher} = require("watch-fs");
const {exec} = require("child_process");
const jetpack = require("fs-jetpack");
const {URL} = require("url");
const md = require("markdown-it")();
const posts = require("./lib/posts");
const app = express();
const postPath = `${__dirname}/posts`;
const watcher = new Watcher({
paths: [postPath],
filters: {
includeFile: ((name) => name.endsWith(".md"))
}
});
app.set("view engine", "pug");
app.set("server name", process.env["SERVER_NAME"] || "http://localhost:1440");
app.use("/static", express.static(`${__dirname}/static`));
app.use((req, res, next) => {
res.locals.pageCount = posts.countPages();
let baseUrl = app.get("server name");
res.locals.asset_url = (path) =>
new URL("/static/" + path, baseUrl).toString();
res.locals.url_for = (post) =>
new URL("/p/" + post.slug, baseUrl).toString();
res.locals.baseUrl = baseUrl;
next(null);
});
app.get("/", (req, res) =>
res.render("index.pug", { posts: posts.fetch(0), page: 0 })
);
app.get("/:page", (req, res) => {
res.locals.page = parseInt(req.params.page);
res.render("index.pug", { posts: posts.fetch(res.locals.page) });
});
app.get("/p/:slug", (req, res) =>
res.render("post.pug", { post: posts.fetchBySlug(req.params.slug) })
);
app.post("/_/webhook/github", (req, res, next) =>
exec("git pull", { cwd: postPath }, (err) => {
if (err) next(err)
res.sendStatus(200);
})
);
watcher.on("create", (path) =>
posts.load(path)
);
watcher.on("change", (path) =>
posts.reload(path)
);
watcher.on("delete", (path) =>
posts.remove(path)
);
watcher.start((err, failed) => {
jetpack.listAsync(postPath).then((files) =>
files.forEach((filepath) =>
posts.load(`${postPath}/${filepath}`)
)
);
app.listen(1440);
});