-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
54 lines (50 loc) · 1.75 KB
/
routes.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
const url = require('url');
const {getGoogleDriveDocWithoutCode} = require('./google-utils');
const {commitBuild} = require('./github-utils.js');
module.exports = (app) => {
// webhook route
app.post('/', (req, res) => {
const {body} = req;
console.log(body);
res.redirect('/index');
});
// index route
app.get('/', (req, res) => {
res.redirect('/index');
});
// Handle build of codelab, docId
app.get('/index',
(req, res) => {
const queryParams = url.parse(req.url, true).query
if (Object.keys(queryParams).length > 0) {
// If they submit a url, get the data
if (queryParams.key){
const matches = /\/([\w-_]{15,})\/(.*?gid=(\d+))?/.exec(queryParams.key);
if (matches) {
const key = matches[1];
getGoogleDriveDocWithoutCode(key, async (output) => {
let baseDir = output.split('\t').pop();
baseDir = baseDir.replace(/[\n\r]/g, '');
try{
commitBuild(baseDir);
console.log('/codelabs/' + baseDir + '/index.html');
res.redirect('/codelabs/' + baseDir + '/index.html');
//res.sendFile(__dirname + '/views/success.html');
} catch(err){
console.log(err);
res.sendFile(__dirname + '/views/error.html');
}
});
} else {
res.sendFile(__dirname + '/views/error.html');
}
} else {
res.redirect('/index');
}
} else{
// If there is no key load the index page
res.sendFile(__dirname + '/views/index.html');
}
}
);
};