-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.coffee
78 lines (67 loc) · 1.79 KB
/
index.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
require 'newrelic' if process.env.ENVIRONMENT == 'production'
Hapi = require 'hapi'
Nipple = require 'nipple'
querystring = require 'querystring'
{parseString} = require 'xml2js'
{log} = console
cache_cfg = if process.env.REDISTOGO_URL
rtg = require("url").parse(process.env.REDISTOGO_URL)
{
engine: 'catbox-redis'
host: rtg.hostname
port: rtg.port
password: rtg.auth.split(":")[1]
}
else
'catbox-redis'
port = process.env.PORT || 5000
log "Listening on " + port
server = Hapi.createServer '0.0.0.0', port,
cache: cache_cfg
cors: true
getBggEndpoint = (endpoint, params, next)->
query = querystring.stringify params
url = "http://boardgamegeek.com/xmlapi2/#{endpoint}?#{query}"
opts = {}
log "making request to: #{url}"
Nipple.get url, opts, (err, res, payload)->
return next err if err
unless /xml/.test(res.headers['content-type'])
bggError = Hapi.error.notFound('BGG did not return XML. Most likely: Invalid object or user')
return next(bggError)
parseString payload, (err, result) ->
return next err if err
if result.error
next Hapi.error.notFound(result.error.$.message)
else
next null, result
MINUTE = 60 * 1000
HOUR = 60 * MINUTE
server.method 'getBggEndpoint', getBggEndpoint,
cache:
expiresIn: 24 * HOUR
staleIn: 6 * HOUR
staleTimeout: 500
generateKey: (endpoint, params)->
endpoint + JSON.stringify(params)
bggRoute = (name)->
server.route
method: 'GET'
path: "/api/v1/#{name}"
handler: (request, reply)->
server.methods.getBggEndpoint name, request.query, (err, result)->
reply if err then err else result
bggRoute route for route in [
'thing'
'family'
'forumlist'
'forum'
'thread'
'user'
'guild'
'plays'
'collection'
'hot'
'search'
]
server.start()