git ssb

0+

Dominic / weather



Commit fa8fe35c744d44ee51b0e57b2ea3af01a64dc51f

server

Dominic Tarr committed on 1/4/2016, 5:27:26 AM
Parent: eabac7df86b55848d9dd78f8f1690fbb1f0fa2a8

Files changed

server.jschanged
server.jsView
@@ -6,31 +6,106 @@
66 var pull = require('pull-stream')
77 var toPull = require('stream-to-pull-stream')
88 var pl = require('pull-level')
99 var stringify = require('pull-stringify')
10 +var urls = require('./urls')
11 +var marked = require('marked')
1012
13 +function render(data, format, md) {
14 + if(format === '.json')
15 + return JSON.stringify(data, null, 2) + '\n\n'
16 + if(format === '.md') return md(data)
17 + return '<div id=content>'+marked(md(data))+'</div>'
18 +}
19 +
20 +function capitalize (n) {
21 + return n[0].toUpperCase() + n.substring(1)
22 +}
23 +function log (e) {
24 + console.log(e)
25 + return e
26 +}
27 +function sections (text) {
28 + return log(text.split(/([\w\s]+):/).map(function (e, i) {
29 + if(!(i%2)) return e + '\n\n'
30 + if(/\s/.test(e.trim())) return '### ' + e
31 + else return '#### ' + e
32 + })).join('\n')
33 +}
34 +
35 +function get(key, format, res, next) {
36 + db.sublevel('raw').get(key, function (err, value) {
37 + if(err) return next(err)
38 + res.end(render(value, format, function (e) {
39 + return [
40 + '# ' + capitalize(value.name),
41 + 'issued at: ' + value.issued,
42 + '## Situation',
43 + e.situation,
44 + '## Forecast',
45 + sections(e.forecast),
46 + '## Outlook',
47 + sections(e.outlook),
48 + '## Swell',
49 + e.swell
50 + ].join('\n')
51 + }))
52 + })
53 +}
54 +
1155 http.createServer(Stack(
12- route(/^\/place\/([a-z\-]+)$/, function (req, res, next) {
56 + route(/^\/([0-9a-f]{64})(\.\w+)?$/, function (req, res, next) {
57 + get(req.params[0], req.params[1], res, next)
58 + }),
59 + route(/^\/([a-z\-]+)(\.\w+)?$/, function (req, res, next) {
1360 db.get(req.params[0], function (err, value) {
1461 if(err) return next(err)
15- db.sublevel('raw').get(value, function (err, value) {
16- if(err) return next(err)
17- res.end(JSON.stringify(value, null, 2))
18- })
62 + get(value, req.params[1], res, next)
1963 })
2064 }),
21- route(/^\/ls\/([a-z\-]+)$/, function (req, res, next) {
65 + route(/^\/ls\/([a-z\-]+)(\.\w+)?$/, function (req, res, next) {
2266 var place = req.params[0]
2367 pull(
24- pl.read(db.sublevel('sample'), {gte: [place, null], lt: [place, undefined]}),
25- stringify(),
26- toPull.sink(res)
68 + pl.read(db.sublevel('sample'), {gte: [place, null], lt: [place, undefined], reverse: true}),
69 + pull.collect(function (err, data) {
70 + res.end(render(data, req.params[1], function (e) {
71 + return '# history for:' + capitalize(e[0].key[0]) + '\n\n' +
72 + e.map(function (e) {
73 + return '* [' + new Date(e.key[1]) + '](/'+e.value+')'
74 + }).join('\n')
75 + }))
76 + })
2777 )
2878 }),
29- route(/^\/update$/, function (req, res, next) {
79 + route(/^\/_update$/, function (req, res, next) {
3080 db.update(function (err, data) {
3181 if(err) return next(err)
3282 return res.end(JSON.stringify(data, null, 2))
3383 })
84 + }),
85 + route(/^\/(\.\w+)?$/, function (req, res) {
86 + res.end(render(Object.keys(urls), req.params[0], function (data) {
87 + return data.map(function (e) {
88 + return '* ['+e+'](/'+e+')'
89 + }).join('\n')
90 + }))
3491 })
3592 )).listen(8000)
93 +//
94 +//db.update(function (err, data) {
95 +// console.log(data)
96 +//})
3697
98 +setInterval(function () {
99 + db.update(console.log)
100 +}, 1000*60*60)
101 +
102 +
103 +
104 +
105 +
106 +
107 +
108 +
109 +
110 +
111 +

Built with git-ssb-web