a simple proxy

This commit is contained in:
Radek Stepan 2013-08-17 13:54:48 +01:00
parent e4b91ee96b
commit edb7baf1c4
5 changed files with 54 additions and 35 deletions

View File

@ -9,11 +9,12 @@
"coffee-script": "~1.6.3", "coffee-script": "~1.6.3",
"async": "~0.2.9", "async": "~0.2.9",
"proxyquire": "~0.4.1", "proxyquire": "~0.4.1",
"lodash": "~1.3.1" "lodash": "~1.3.1",
"connect": "~2.8.5",
"request": "~2.27.0"
}, },
"devDependencies": { "devDependencies": {
"mocha": "~1.12.0", "mocha": "~1.12.0"
"moment": "~2.1.0"
}, },
"scripts": { "scripts": {
"test": "make test" "test": "make test"

View File

@ -1 +1,43 @@
#!/usr/bin/env coffee #!/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'
# 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 request.
return request {
uri: 'https://' + config.host + req.url
headers
}, (_err, _res, body) ->
write _res.statusCode, body
# Get handled by Connect.
next()
app = connect()
.use(proxy)
.use(connect.static(__dirname + '/public'))
.listen process.env.PORT

View File

@ -18,35 +18,11 @@ module.exports =
# Swap? # Swap?
[ b, a ] = [ a, b ] if b < a [ b, a ] = [ a, b ] if b < a
return cb null, [ cb null, [
{ date: new Date(a), points: total } { date: new Date(a), points: total }
{ date: new Date(b), points: 0 } { date: new Date(b), points: 0 }
] ]
# When do we start & end?
[ year, month, day ] = _.map(a.match(reg.datetime)[1].split('-'), (d) -> parseInt(d) )
# The head/tail are quite specific.
head = { date: new Date(a), points: total }
tail = { date: b = new Date(b.match(reg.datetime)[1]), points: 0 }
# The fillers...
days = []
do add = (i = 1) ->
# Add the time point at lunchtime.
days.push { date: c = new Date(year, month - 1, day + i, 12) }
# Moar?
add(i + 1) if c < b
# Daily velocity needed.
daily = total / (days.length + 1)
# Map points to days.
days = _.map days, (day) ->
day.points = total -= daily
day
cb null, [ head ].concat(days).concat([ tail ])
'render': ([ actual, ideal ], cb) -> 'render': ([ actual, ideal ], cb) ->
# Get available space. # Get available space.
{ height, width } = document.querySelector('#graph').getBoundingClientRect() { height, width } = document.querySelector('#graph').getBoundingClientRect()

View File

@ -18,9 +18,9 @@ class exports.Repos
fetch: (cb) -> fetch: (cb) ->
self = @ self = @
req.config (err, config) -> req.config (err, { protocol, host, token, repos }) ->
return cb err if err protocol ?= 'https' ; host ?= 'api.github.com'
self.models = ( new Repo(entry) for entry in config ) self.models = ( new Repo({ protocol, host, token, repo: r }) for r in repos )
cb null cb null
at: (index) -> at: (index) ->

View File

@ -13,7 +13,7 @@ module.exports =
_.extend query, { per_page: '100' } _.extend query, { per_page: '100' }
request repo, query, 'issues', cb request repo, query, 'issues', cb
# Get config from our domain always. # Get config from our host always.
'config': (cb) -> 'config': (cb) ->
sa sa
.get("http://#{window.location.host}/config.json") .get("http://#{window.location.host}/config.json")
@ -22,12 +22,12 @@ module.exports =
cb err, data?.body cb err, data?.body
# Make a request using SuperAgent. # Make a request using SuperAgent.
request = ({ domain, token, user, repo }, query, path, cb) -> request = ({ protocol, host, token, repo }, query, path, cb) ->
# Make the query params. # Make the query params.
q = ( "#{k}=#{v}" for k, v of query ).join('&') q = ( "#{k}=#{v}" for k, v of query ).join('&')
req = sa req = sa
.get("https://#{domain}/repos/#{user}/#{repo}/#{path}?#{q}") .get("#{protocol}://#{host}/repos/#{repo}/#{path}?#{q}")
.set('Content-Type', 'application/json') .set('Content-Type', 'application/json')
.set('Accept', 'application/vnd.github.raw') .set('Accept', 'application/vnd.github.raw')