This repository has been archived by the owner on Apr 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
70 lines (66 loc) · 1.7 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
var db = require('./db')
var router = require('choreographer').router()
var settings = require('./settings')
var url = require('url')
var util = require('util')
router.get('/', function(req, res, next) {
var sites
db.get('checky:sites', function(err, sites) {
if (err) return next(err)
res.writeHead(200, {'Content-Type': 'application/json'})
res.end(sites)
})
})
router.post('/', function(req, res, next) {
var body = ''
var query = url.parse(req.url, true).query
if (!query.key) {
res.writeHead(400)
return res.end()
}
if (query.key != settings.secret) {
res.writeHead(403)
return res.end()
}
req.setEncoding('utf8')
req.on('data', function(chunk) {
body += chunk
})
req.on('end', function() {
var sites
try {
sites = JSON.parse(body)
} catch (e) {
res.writeHead(400)
return res.end()
}
for (var i=0; i<sites.length; i++) {
sites[i].ok = true
}
db.set('checky:sites', JSON.stringify(sites), function(err) {
if (err) return next(err)
res.writeHead(200, {'Content-Type': 'application/json'})
res.end('{"ok":true}')
})
})
})
router.get('/*', function(req, res, next, siteSlug) {
db.get('checky:sites', function(err, sitesVal) {
if (err) return next(err)
var sites = JSON.parse(sitesVal)
var found
for (var i=0; i<sites.length; i++) {
if (sites[i].slug == siteSlug) {
found = true
break
}
}
if (!found) return next()
db.lrange('checky:sites:'+siteSlug, 0, 299, function(err, log) {
if (err) return next(err)
res.writeHead(200, {'Content-Type': 'application/json'})
res.end(JSON.stringify(log))
})
})
})
module.exports = router