Skip to content

Commit

Permalink
wild untested stab at proxy layer
Browse files Browse the repository at this point in the history
  • Loading branch information
aeischeid committed Sep 18, 2012
1 parent cee940a commit ca89870
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
6 changes: 5 additions & 1 deletion examples/slug.json
Expand Up @@ -2,5 +2,9 @@
"libs": [
"./lib/custom.js"
],
"dependencies": ["gfx"]
"dependencies": ["gfx"],
"useProxy": true,
"apiHost": "localhost",
"apiPort": 8080,
"proxyPort": 8001
}
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -23,6 +23,7 @@
"stylus" : "~0.29.0",
"coffee-script" : "~1.3.3",
"watch" : "~0.4.0",
"strata": "0.15.1"
"strata": "0.15.1",
"http-proxy": "~0.8.2"
}
}
25 changes: 25 additions & 0 deletions src/hem.coffee
Expand Up @@ -6,6 +6,8 @@ compilers = require('./compilers')
pkg = require('./package')
css = require('./css')
specs = require('./specs')
http = require('http'),
httpProxy = require('http-proxy')

argv = optimist.usage([
' usage: hem COMMAND',
Expand Down Expand Up @@ -39,6 +41,10 @@ class Hem
dependencies: []
port: process.env.PORT or argv.port or 9294
host: argv.host or 'localhost'
useProxy: argv.useProxy or false
apiHost: argv.apiHost or 'localhost'
apiPort: argv.apiPort or 8080
proxyPort: argv.proxyPort or 8001
cssPath: '/application.css'
jsPath: '/application.js'

Expand All @@ -53,6 +59,7 @@ class Hem
@options[key] = value for key, value of @readSlug()

server: ->
# setup the strata server to handle the spine app
strata.use(strata.contentLength)

strata.map @options.cssPath, (app) =>
Expand All @@ -72,6 +79,24 @@ class Hem
strata.use(strata.file, @options.public, ['index.html', 'index.htm'])

strata.run(port: @options.port, host: @options.host)

# Optionally setup the proxyServer to conditionally route requests.
# The spine app and the api need to appear to the browser to be coming from
# the same host and port to avoid crossDomain ajax issues
if @options.useProxy
proxy = new httpProxy.RoutingProxy()
http.createServer(function (req, res) {
if someSpinePath
proxy.proxyRequest(req, res, {
host: @options.host
port: @options.port
})
else
proxy.proxyRequest(req, res, {
host: @options.apiHost
port: @options.apiPort
})
}).listen(@options.proxyPort)

build: ->
source = @hemPackage().compile(not argv.debug)
Expand Down

0 comments on commit ca89870

Please sign in to comment.