Commit e1b55c0cca1102ca828c984773c303b09b965ff0
update proxy to work in dev and tests
Michael Williams committed on 2/15/2016, 3:17:26 AMParent: 6d09dd8687c742866f2799345a876eaf0d9f002d
Files changed
app/proxy.js | added |
config/development.js | changed |
config/test.js | changed |
features/support/hooks.js | changed |
features/support/world.js | changed |
package.json | changed |
proxy.js | changed |
app/proxy.js | ||
---|---|---|
@@ -1,0 +1,44 @@ | ||
1 | +const http = require('http') | |
2 | +const httpProxy = require('http-proxy') | |
3 | +const Url = require('url') | |
4 | +const startsWith = require('lodash').startsWith | |
5 | +const assign = require('lodash').assign | |
6 | + | |
7 | +module.exports = { createServer } | |
8 | + | |
9 | +function createServer (config) { | |
10 | + const proxy = httpProxy.createProxyServer({ | |
11 | + ignorePath: true | |
12 | + }) | |
13 | + | |
14 | + const server = http.createServer(function (req, res) { | |
15 | + const url = Url.parse(req.url) | |
16 | + | |
17 | + if (matches(config.api, url.pathname)) { | |
18 | + proxy.web(req, res, { target: targetUrl(config.api, url.pathname) }) | |
19 | + } else if (matches(config.static, url.pathname)) { | |
20 | + proxy.web(req, res, { target: targetUrl(config.static, url.pathname) }) | |
21 | + } else { | |
22 | + proxy.web(req, res, { target: targetUrl(config.render, url.pathname) }) | |
23 | + } | |
24 | + }) | |
25 | + | |
26 | + return server | |
27 | +} | |
28 | + | |
29 | +function targetUrl (service, pathname) { | |
30 | + const url = assign( | |
31 | + {}, service.url, | |
32 | + { port: service.port }, | |
33 | + { pathname: targetPath(service, pathname) } | |
34 | + ) | |
35 | + return Url.format(url) | |
36 | +} | |
37 | + | |
38 | +function targetPath (service, pathname) { | |
39 | + return pathname.slice(service.url.pathname.length - 1) | |
40 | +} | |
41 | + | |
42 | +function matches (service, pathname) { | |
43 | + return startsWith(pathname, service.url.pathname) | |
44 | +} |
config/development.js | ||
---|---|---|
@@ -1,31 +1,37 @@ | ||
1 | 1 | const join = require('path').join |
2 | 2 | |
3 | 3 | module.exports = { |
4 | + proxy: { | |
5 | + port: 5000 | |
6 | + }, | |
4 | 7 | render: { |
5 | 8 | url: { |
6 | 9 | protocol: 'http:', |
7 | 10 | hostname: 'localhost', |
11 | + pathname: '/', | |
8 | 12 | port: 5000 |
9 | 13 | }, |
10 | - port: 5000 | |
14 | + port: 6000 | |
11 | 15 | }, |
12 | 16 | static: { |
13 | 17 | url: { |
14 | 18 | protocol: 'http:', |
15 | 19 | hostname: 'localhost', |
16 | - port: 5001 | |
20 | + pathname: '/static/', | |
21 | + port: 5000 | |
17 | 22 | }, |
18 | 23 | root: join(__dirname, '..', 'build'), |
19 | - port: 5001 | |
24 | + port: 6001 | |
20 | 25 | }, |
21 | 26 | api: { |
22 | 27 | url: { |
23 | 28 | protocol: 'http:', |
24 | 29 | hostname: 'localhost', |
25 | - port: 5002 | |
30 | + pathname: '/api/', | |
31 | + port: 5000 | |
26 | 32 | }, |
27 | - port: 5002 | |
33 | + port: 6002 | |
28 | 34 | }, |
29 | 35 | db: { |
30 | 36 | client: 'pg', |
31 | 37 | connection: { |
config/test.js | ||
---|---|---|
@@ -1,21 +1,45 @@ | ||
1 | -const join = require('path').join | |
2 | -const env = process.env | |
3 | -const nodeEnv = env.NODE_ENV | |
4 | - | |
5 | 1 | module.exports = { |
2 | + proxy: { | |
3 | + port: 5050 | |
4 | + }, | |
6 | 5 | render: { |
7 | 6 | url: { |
8 | - port: 6000 | |
9 | - } | |
7 | + protocol: 'http:', | |
8 | + hostname: 'localhost', | |
9 | + pathname: '/', | |
10 | + port: 5050 | |
11 | + }, | |
12 | + port: 6050 | |
10 | 13 | }, |
11 | 14 | static: { |
12 | 15 | url: { |
13 | - port: 6001 | |
14 | - } | |
16 | + protocol: 'http:', | |
17 | + hostname: 'localhost', | |
18 | + pathname: '/static/', | |
19 | + port: 5050 | |
20 | + }, | |
21 | + port: 6051 | |
15 | 22 | }, |
16 | 23 | api: { |
17 | 24 | url: { |
18 | - port: 6002 | |
25 | + protocol: 'http:', | |
26 | + hostname: 'localhost', | |
27 | + pathname: '/api/', | |
28 | + port: 5050 | |
29 | + }, | |
30 | + port: 6052 | |
31 | + }, | |
32 | + db: { | |
33 | + client: 'pg', | |
34 | + connection: { | |
35 | + host : 'localhost', | |
36 | + user : 'postgres', | |
37 | + //password : 'postgres', | |
38 | + database : 'postgres' | |
39 | + }, | |
40 | + pool: { | |
41 | + min: 0, | |
42 | + max: 1 | |
19 | 43 | } |
20 | 44 | } |
21 | 45 | } |
features/support/hooks.js | ||
---|---|---|
@@ -9,18 +9,19 @@ | ||
9 | 9 | module.exports = function () { |
10 | 10 | const servers = _.mapValues({ |
11 | 11 | static: require('app/static'), |
12 | 12 | api: require('app/api'), |
13 | - render: require('app/render') | |
13 | + render: require('app/render'), | |
14 | + proxy: require('app/proxy') | |
14 | 15 | }, function (module) { |
15 | 16 | return module.createServer(config) |
16 | 17 | }) |
17 | 18 | |
18 | 19 | function start (cb) { |
19 | 20 | parallel( |
20 | 21 | _.map(servers, function (server, name) { |
21 | 22 | return function (callback) { |
22 | - server.listen(config[name].url.port, callback) | |
23 | + server.listen(config[name].port, callback) | |
23 | 24 | } |
24 | 25 | }), |
25 | 26 | cb |
26 | 27 | ) |
features/support/world.js | ||
---|---|---|
@@ -1,9 +1,9 @@ | ||
1 | 1 | const Browser = require('zombie') |
2 | 2 | |
3 | 3 | const config = require('app/config') |
4 | 4 | |
5 | -Browser.localhost(config.render.url.hostname, config.render.url.port) | |
5 | +Browser.localhost('localhost', config.proxy.port) | |
6 | 6 | |
7 | 7 | function World () { |
8 | 8 | // this.browser will be available in step definitions |
9 | 9 | this.browser = new Browser() |
package.json | ||
---|---|---|
@@ -21,8 +21,9 @@ | ||
21 | 21 | "dev:assets": "cpx \"app/assets/**/*\" build -w", |
22 | 22 | "dev:livereload": "wtch -d build -e html,css,png,gif,jpg | garnish --level debug", |
23 | 23 | "dev:api": "node-dev api", |
24 | 24 | "dev:static": "node-dev static", |
25 | + "dev:proxy": "node-dev proxy", | |
25 | 26 | "prod:render-browser": "browserify app/render -o build/bundle.js -g envify -g uglifyify", |
26 | 27 | "prod:render-node": "node render", |
27 | 28 | "prod:assets": "cpx \"app/assets/**/*\" build", |
28 | 29 | "prod:api": "node api", |
proxy.js | ||
---|---|---|
@@ -1,43 +1,9 @@ | ||
1 | -const http = require('http') | |
2 | -const httpProxy = require('http-proxy') | |
1 | +const config = require('app/config') | |
2 | +const createServer = require('app/proxy').createServer | |
3 | 3 | const Url = require('url') |
4 | -const startsWith = require('lodash').startsWith | |
5 | -const assign = require('lodash').assign | |
6 | 4 | |
7 | -const config = require('app/config') | |
5 | +const server = createServer(config) | |
8 | 6 | |
9 | -const proxy = httpProxy.createProxyServer({ | |
10 | - ignorePath: true | |
11 | -}) | |
12 | - | |
13 | -const server = http.createServer(function (req, res) { | |
14 | - const url = Url.parse(req.url) | |
15 | - if (matches(config.api, url.pathname)) { | |
16 | - proxy.web(req, res, { target: targetUrl(config.api, url.pathname) }) | |
17 | - } else if (matches(config.static, url.pathname)) { | |
18 | - proxy.web(req, res, { target: targetUrl(config.static, url.pathname) }) | |
19 | - } else { | |
20 | - proxy.web(req, res, { target: targetUrl(config.render, url.pathname) }) | |
21 | - } | |
22 | -}) | |
23 | - | |
24 | 7 | server.listen(config.proxy.port, function () { |
25 | 8 | console.log(`proxy server listening on port ${config.proxy.port}`) |
26 | 9 | }) |
27 | - | |
28 | -function targetUrl (service, pathname) { | |
29 | - const url = assign( | |
30 | - {}, service.url, | |
31 | - { port: service.port }, | |
32 | - { pathname: targetPath(service, pathname) } | |
33 | - ) | |
34 | - return Url.format(url) | |
35 | -} | |
36 | - | |
37 | -function targetPath (service, pathname) { | |
38 | - return pathname.slice(service.url.pathname.length - 1) | |
39 | -} | |
40 | - | |
41 | -function matches (service, pathname) { | |
42 | - return startsWith(pathname, service.url.pathname) | |
43 | -} |
Built with git-ssb-web