From edb7baf1c46d95b17676efc1b403851c259d8ddc Mon Sep 17 00:00:00 2001 From: Radek Stepan Date: Sat, 17 Aug 2013 13:54:48 +0100 Subject: [PATCH] a simple proxy --- package.json | 7 +++--- proxy.coffee | 44 +++++++++++++++++++++++++++++++++++++- src/modules/graph.coffee | 26 +--------------------- src/modules/repos.coffee | 6 +++--- src/modules/request.coffee | 6 +++--- 5 files changed, 54 insertions(+), 35 deletions(-) diff --git a/package.json b/package.json index 1379889..8beaa4d 100644 --- a/package.json +++ b/package.json @@ -9,11 +9,12 @@ "coffee-script": "~1.6.3", "async": "~0.2.9", "proxyquire": "~0.4.1", - "lodash": "~1.3.1" + "lodash": "~1.3.1", + "connect": "~2.8.5", + "request": "~2.27.0" }, "devDependencies": { - "mocha": "~1.12.0", - "moment": "~2.1.0" + "mocha": "~1.12.0" }, "scripts": { "test": "make test" diff --git a/proxy.coffee b/proxy.coffee index 706739d..c6b9ee7 100644 --- a/proxy.coffee +++ b/proxy.coffee @@ -1 +1,43 @@ -#!/usr/bin/env coffee \ No newline at end of file +#!/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 \ No newline at end of file diff --git a/src/modules/graph.coffee b/src/modules/graph.coffee index 8d34c35..970d6b9 100644 --- a/src/modules/graph.coffee +++ b/src/modules/graph.coffee @@ -18,35 +18,11 @@ module.exports = # Swap? [ b, a ] = [ a, b ] if b < a - return cb null, [ + cb null, [ { date: new Date(a), points: total } { 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) -> # Get available space. { height, width } = document.querySelector('#graph').getBoundingClientRect() diff --git a/src/modules/repos.coffee b/src/modules/repos.coffee index 8a45826..f0fd154 100644 --- a/src/modules/repos.coffee +++ b/src/modules/repos.coffee @@ -18,9 +18,9 @@ class exports.Repos fetch: (cb) -> self = @ - req.config (err, config) -> - return cb err if err - self.models = ( new Repo(entry) for entry in config ) + req.config (err, { protocol, host, token, repos }) -> + protocol ?= 'https' ; host ?= 'api.github.com' + self.models = ( new Repo({ protocol, host, token, repo: r }) for r in repos ) cb null at: (index) -> diff --git a/src/modules/request.coffee b/src/modules/request.coffee index 5d27863..ddb51a5 100644 --- a/src/modules/request.coffee +++ b/src/modules/request.coffee @@ -13,7 +13,7 @@ module.exports = _.extend query, { per_page: '100' } request repo, query, 'issues', cb - # Get config from our domain always. + # Get config from our host always. 'config': (cb) -> sa .get("http://#{window.location.host}/config.json") @@ -22,12 +22,12 @@ module.exports = cb err, data?.body # 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. q = ( "#{k}=#{v}" for k, v of query ).join('&') req = sa - .get("https://#{domain}/repos/#{user}/#{repo}/#{path}?#{q}") + .get("#{protocol}://#{host}/repos/#{repo}/#{path}?#{q}") .set('Content-Type', 'application/json') .set('Accept', 'application/vnd.github.raw')