Files: 5eb47c64b87215ab4daa1b2e534ce2b82ac0a95f / server.js
3921 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).toLowerCase() |
22 | } |
23 | function log (e) { |
24 | console.log(e) |
25 | return e |
26 | } |
27 | |
28 | function sections (text) { |
29 | return log(text.split(/([\w,\s]+):/).map(function (e, i) { |
30 | if(!(i%2)) return e + '\n\n' |
31 | if(/\s/.test(e.trim())) return '### ' + e |
32 | else return '#### ' + e |
33 | })).join('\n') |
34 | } |
35 | |
36 | function renderTides(tides) { |
37 | return tides.map(function (e) { |
38 | return '### ' + capitalize(e.location) + '\n' + |
39 | e.tides.map(function (tide) { |
40 | return '* ' + tide.height + 'm at ' + tide.time |
41 | }).join('\n') |
42 | }).join('\n\n') |
43 | } |
44 | |
45 | function get(key, format, res, next) { |
46 | db.sublevel('raw').get(key, function (err, value) { |
47 | if(err) return next(err) |
48 | |
49 | if(value.forecast) //render a weather forecast |
50 | res.end(render(value, format, function (e) { |
51 | return [ |
52 | '# ' + capitalize(value.name), |
53 | 'issued at: [' + value.issued +'](/ls/'+value.name+')', |
54 | '## Situation', |
55 | e.situation, |
56 | '## Forecast', |
57 | sections(e.forecast), |
58 | '## Outlook', |
59 | sections(e.outlook), |
60 | e.swell ? '## Swell' + e.swell : '', |
61 | e.tides ? '## Tides' + renderTides(e.tides) : '', |
62 | //oceanic forecasts |
63 | e.warnings ? '## Warnings\n\n' + |
64 | e.warnings.map(function (e) { |
65 | return '### warning: '+e.name + ' (Analysis ' + e.analysis+')\n\n' + |
66 | e.warning.replace(/<br\/>/g, '\n\n') + '\n\n issued at:' + e.issued |
67 | }).join('\n\n') : '' |
68 | ].join('\n') |
69 | })) |
70 | else if(value.imageData) |
71 | res.end(render(value, format, function (e) { |
72 | return e.imageData.map(function (e) { |
73 | return '## ' + e.description + '\n\n' |
74 | + '![e.description](http://metservice.com' + e.url + ')\n' |
75 | + '\nissued at:' + e.issuedTime + ' valid from:' + e.validFromTime |
76 | }).join('\n\n\n') |
77 | })) |
78 | |
79 | }) |
80 | } |
81 | |
82 | http.createServer(Stack( |
83 | route(/^\/([0-9a-f]{64})(\.\w+)?$/, function (req, res, next) { |
84 | get(req.params[0], req.params[1], res, next) |
85 | }), |
86 | route(/^\/([a-z\-_]+)(\.\w+)?$/, function (req, res, next) { |
87 | db.get(req.params[0], function (err, value) { |
88 | if(err) return next(err) |
89 | get(value, req.params[1], res, next) |
90 | }) |
91 | }), |
92 | route(/^\/ls\/([a-z\-_]+)(\.\w+)?$/, function (req, res, next) { |
93 | var place = req.params[0] |
94 | pull( |
95 | pl.read(db.sublevel('sample'), {gte: [place, null], lt: [place, undefined], reverse: true}), |
96 | pull.collect(function (err, data) { |
97 | res.end(render(data, req.params[1], function (e) { |
98 | return '# history for:' + capitalize(e[0].key[0]) + '\n\n' + |
99 | e.map(function (e) { |
100 | return '* [' + new Date(e.key[1]) + '](/'+e.value+')' |
101 | }).join('\n') |
102 | })) |
103 | }) |
104 | ) |
105 | }), |
106 | route(/^\/_update$/, function (req, res, next) { |
107 | db.update(function (err, data) { |
108 | if(err) return next(err) |
109 | return res.end(JSON.stringify(data, null, 2)) |
110 | }) |
111 | }), |
112 | route(/^\/(\.\w+)?$/, function (req, res) { |
113 | res.end(render(Object.keys(urls), req.params[0], function (data) { |
114 | return data.map(function (e) { |
115 | return '* ['+e+'](/'+e+')' |
116 | }).join('\n') |
117 | })) |
118 | }) |
119 | )).listen(8000) |
120 | // |
121 | //db.update(function (err, data) { |
122 | // console.log(data) |
123 | //}) |
124 | |
125 | setInterval(function () { |
126 | db.update(console.log) |
127 | }, 1000*60*60) |
128 | |
129 | |
130 | |
131 | |
132 | |
133 | |
134 | |
135 | |
136 | |
137 | |
138 | |
139 |
Built with git-ssb-web