-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
79 lines (69 loc) · 2.3 KB
/
app.js
File metadata and controls
79 lines (69 loc) · 2.3 KB
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
const express = require('express')
const cp = require('child_process')
const os = require('os')
const fs = require('fs')
const app = express()
const port = 3000
const blog_repo = 'https://github.com/zju-lambda/hexo-blog.git';
const slides_repo = 'https://github.com/zju-lambda/slides.git';
function exec(cmd) {
console.log('[Exec] ' + cmd)
cp.execSync(cmd);
}
function init() {
exec(`git clone ${slides_repo}`);
exec(`git clone ${blog_repo}`);
exec('cd hexo-blog && npm install && npm uninstall hexo-renderer-marked --save && npm install hexo-renderer-kramed --save && cp ../patch/inline.js node_modules/kramed/lib/rules && node node_modules/hexo/bin/hexo generate');
}
function update() {
exec('cd slides && git pull');
exec('cd hexo-blog && git pull && npm install && && node node_modules/hexo/bin/hexo generate');
}
index = {};
function build_index() {
index = {};
let posts = fs.readdirSync('hexo-blog/source/_posts');
posts.forEach(post => {
let filename = `hexo-blog/source/_posts/${post}`;
console.log(`[Scan] ${filename}`);
let data = fs.readFileSync(filename).toString();
let lines = data.split("\n");
for (i = 1; i < lines.length; i++) {
let line = lines[i].trim();
if (line.startsWith("---")) {
break;
} else {
let s = line.split(":");
let key = s[0].trim();
if (key == "redirect") {
let value = s.slice(1).join(":").trim();
index[post.split('.')[0]] = value;
}
}
}
});
console.log(index);
}
if (!fs.existsSync('hexo-blog')) {
init();
}
build_index();
//app.get('/', (req, res) => res.send('Hello World!'))
app.get('/[0-9]*/[0-9]*/[0-9]*/', function (req, res, next) {
let name = req.path.match("/[0-9]*/[0-9]*/[0-9]*/(.*)/$")[1];
console.log(`[Get] ${name}`);
if (index[name]) {
let url = index[name];
res.redirect(302, url);
} else {
next();
}
});
app.use(express.static('hexo-blog/public'));
app.use('/slides', express.static('slides'));
app.post('/github', function (req, res) {
update();
build_index();
res.status(201).end();
});
app.listen(port, () => console.log(`[Log] listening on port ${port}`))