Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

wild untested stab at proxy layer

  • Loading branch information...
commit ca89870d494766b14f3bcebec5e7f2ee6d071d6a 1 parent cee940a
aeischeid authored
Showing with 32 additions and 2 deletions.
  1. +5 −1 examples/slug.json
  2. +2 −1  package.json
  3. +25 −0 src/hem.coffee
View
6 examples/slug.json
@@ -2,5 +2,9 @@
"libs": [
"./lib/custom.js"
],
- "dependencies": ["gfx"]
+ "dependencies": ["gfx"],
+ "useProxy": true,
+ "apiHost": "localhost",
+ "apiPort": 8080,
+ "proxyPort": 8001
}
View
3  package.json
@@ -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"
}
}
View
25 src/hem.coffee
@@ -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',
@@ -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'
@@ -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) =>
@@ -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)
Please sign in to comment.
Something went wrong with that request. Please try again.