Files: fa8fe35c744d44ee51b0e57b2ea3af01a64dc51f / server.js
2803 bytesRaw
1 | |
2 | var http = require('http') |
3 | var Stack = require('stack') |
4 | var route = require('tiny-route') |
5 | var db = require('./db') |
6 | var pull = require('pull-stream') |
7 | var toPull = require('stream-to-pull-stream') |
8 | var pl = require('pull-level') |
9 | var stringify = require('pull-stringify') |
10 | var urls = require('./urls') |
11 | var marked = require('marked') |
12 | |
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 | |
55 | http.createServer(Stack( |
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) { |
60 | db.get(req.params[0], function (err, value) { |
61 | if(err) return next(err) |
62 | get(value, req.params[1], res, next) |
63 | }) |
64 | }), |
65 | route(/^\/ls\/([a-z\-]+)(\.\w+)?$/, function (req, res, next) { |
66 | var place = req.params[0] |
67 | pull( |
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 | }) |
77 | ) |
78 | }), |
79 | route(/^\/_update$/, function (req, res, next) { |
80 | db.update(function (err, data) { |
81 | if(err) return next(err) |
82 | return res.end(JSON.stringify(data, null, 2)) |
83 | }) |
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 | })) |
91 | }) |
92 | )).listen(8000) |
93 | // |
94 | //db.update(function (err, data) { |
95 | // console.log(data) |
96 | //}) |
97 | |
98 | setInterval(function () { |
99 | db.update(console.log) |
100 | }, 1000*60*60) |
101 | |
102 | |
103 | |
104 | |
105 | |
106 | |
107 | |
108 | |
109 | |
110 | |
111 | |
112 |
Built with git-ssb-web