-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.coffee
60 lines (44 loc) · 1.47 KB
/
server.coffee
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
express = require('express')
app = express()
request = require('request')
markdown = require('markdown').markdown;
fs = require('fs')
app.set('view engine', 'jade')
app.set('views', __dirname + '/views')
# use nginx for proxy
app.enable('trust proxy')
# serve static files and log
app.use(express.logger())
app.use('/static', express.static(__dirname + '/static'))
APP_ID = null
if fs.existsSync "config.json"
configJson = JSON.parse fs.readFileSync "config.json", 'utf8'
APP_ID = configJson.appid # optional openweathermap api key
serveMarkdownRoute = (path, mdfilename, title) ->
mdSrc = fs.readFileSync "markdown/#{mdfilename}", 'utf8'
mdContent = markdown.toHTML(mdSrc)
app.get path, (req, res) ->
res.render "markdowntemplate",
title: title
mdContent: mdContent
serveMarkdownRoute "/about", "about.md", 'About'
app.get '/', (req, res) ->
res.render 'main',
title: "Seriously though how long."
app.get "/why", (req, res) ->
res.render "why",
title: "Why?"
app.get '/weather', (req, res) ->
if req.query.lat and req.query.lon
app_key_stuff = ""
if APP_ID?
app_key_stuff = "&APPID=#{APP_ID}"
weatherUrl = "http://api.openweathermap.org/data/2.5/weather?lat=#{req.query.lat}&lon=#{req.query.lon}#{app_key_stuff}&units=metric"
request weatherUrl, (err, resp, body) ->
result = JSON.parse body
res.send
currentTemp: result.main.temp
else
res.send 400,
error: "That's no good."
app.listen(9000)