forked from radekstepan/burnchart
-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxy.coffee
46 lines (40 loc) · 1.31 KB
/
proxy.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
#!/usr/bin/env coffee
{ _ } = require 'lodash'
http = require 'http'
fs = require 'fs'
connect = require 'connect'
request = require 'request'
# Read the original config.
config = JSON.parse fs.readFileSync './config.json', 'utf-8'
# Some defaults.
config.host ?= 'api.github.com'
# This is the scrubbed version.
_.extend scrubbed = {}, config, { 'protocol': 'http', 'token': null }
proxy = (req, res, next) ->
write = (code, body) ->
res.writeHead code, {'Content-Type': 'application/json; charset=utf-8'}
res.end body
# Config?
if req.url is '/config.json'
# Refer to us like so.
scrubbed.host = req.headers.host
return write 200, JSON.stringify scrubbed, null, 4
# API request?
if req.url.match /^\/repos/
# The new headers.
headers = 'Accept': 'application/vnd.github.raw'
# Add a token?
headers.Authorization = 'token ' + config.token if config.token
# Make the HTTPS request.
return request {
'uri': 'https://' + config.host + req.url
headers
}, (_err, _res, body) ->
return write(500) if _err
write _res.statusCode, body
# Get handled by Connect.
next()
app = connect()
.use(proxy)
.use(connect.static(__dirname + '/public'))
.listen process.env.PORT