Commit ed26f7c5c655a65d700ecac4cddac76d22aad40a
Merge pull request #49 from enspiral-craftworks/feature/proxy
add proxy to work with dokku production deployMikey committed on 2/15/2016, 3:38:22 AM
Parent: e51a1ed250d9a835a0a95e6e9c07fd3f450e7117
Parent: e1b55c0cca1102ca828c984773c303b09b965ff0
Files changed
api.js | changed |
app/proxy.js | added |
config/development.js | changed |
config/index.js | changed |
config/production.js | changed |
config/test.js | changed |
features/support/hooks.js | changed |
features/support/world.js | changed |
package.json | changed |
render.js | changed |
static.js | changed |
proxy.js | added |
api.js | ||
---|---|---|
@@ -5,8 +5,8 @@ | ||
5 | 5 | const Url = require('url') |
6 | 6 | |
7 | 7 | const server = createServer(config) |
8 | 8 | |
9 | -server.listen(config.api.url.port, function () { | |
9 | +server.listen(config.api.port, function () { | |
10 | 10 | const apiUrl = Url.format(config.api.url) |
11 | - console.log(`api server listening at ${apiUrl}`) | |
11 | + console.log(`api server at ${apiUrl}`) | |
12 | 12 | }) |
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,7 +1,38 @@ | ||
1 | 1 | const join = require('path').join |
2 | 2 | |
3 | 3 | module.exports = { |
4 | + proxy: { | |
5 | + port: 5000 | |
6 | + }, | |
7 | + render: { | |
8 | + url: { | |
9 | + protocol: 'http:', | |
10 | + hostname: 'localhost', | |
11 | + pathname: '/', | |
12 | + port: 5000 | |
13 | + }, | |
14 | + port: 6000 | |
15 | + }, | |
16 | + static: { | |
17 | + url: { | |
18 | + protocol: 'http:', | |
19 | + hostname: 'localhost', | |
20 | + pathname: '/static/', | |
21 | + port: 5000 | |
22 | + }, | |
23 | + root: join(__dirname, '..', 'build'), | |
24 | + port: 6001 | |
25 | + }, | |
26 | + api: { | |
27 | + url: { | |
28 | + protocol: 'http:', | |
29 | + hostname: 'localhost', | |
30 | + pathname: '/api/', | |
31 | + port: 5000 | |
32 | + }, | |
33 | + port: 6002 | |
34 | + }, | |
4 | 35 | db: { |
5 | 36 | client: 'pg', |
6 | 37 | connection: { |
7 | 38 | host : 'localhost', |
config/index.js | ||
---|---|---|
@@ -3,26 +3,11 @@ | ||
3 | 3 | const nodeEnv = env.NODE_ENV |
4 | 4 | |
5 | 5 | module.exports = { |
6 | 6 | render: { |
7 | - url: { | |
8 | - protocol: 'http:', | |
9 | - hostname: 'localhost', | |
10 | - port: 5000 | |
11 | - } | |
12 | 7 | }, |
13 | 8 | static: { |
14 | - url: { | |
15 | - protocol: 'http:', | |
16 | - hostname: 'localhost', | |
17 | - port: 5001 | |
18 | - }, | |
19 | 9 | root: join(__dirname, '..', 'build') |
20 | 10 | }, |
21 | 11 | api: { |
22 | - url: { | |
23 | - protocol: 'http:', | |
24 | - hostname: 'localhost', | |
25 | - port: 5002 | |
26 | - } | |
27 | 12 | } |
28 | 13 | } |
config/production.js | ||
---|---|---|
@@ -1,3 +1,38 @@ | ||
1 | 1 | module.exports = { |
2 | - | |
2 | + proxy: { | |
3 | + port: process.env.PORT || 80 | |
4 | + }, | |
5 | + render: { | |
6 | + url: { | |
7 | + protocol: 'http:', | |
8 | + hostname: 'localhost', | |
9 | + pathname: '/' | |
10 | + }, | |
11 | + port: 6000 | |
12 | + }, | |
13 | + static: { | |
14 | + url: { | |
15 | + protocol: 'http:', | |
16 | + hostname: 'localhost', | |
17 | + pathname: '/static/' | |
18 | + }, | |
19 | + port: 6001 | |
20 | + }, | |
21 | + api: { | |
22 | + url: { | |
23 | + protocol: 'http:', | |
24 | + hostname: 'localhost', | |
25 | + pathname: '/api/' | |
26 | + }, | |
27 | + port: 6002 | |
28 | + }, | |
29 | + db: { | |
30 | + client: 'pg', | |
31 | + connection: { | |
32 | + host : 'localhost', | |
33 | + user : 'postgres', | |
34 | + //password : 'postgres', | |
35 | + database : 'postgres' | |
36 | + } | |
37 | + } | |
3 | 38 | } |
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,15 +21,17 @@ | ||
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", |
29 | 30 | "prod:static": "node static", |
31 | + "prod:proxy": "node proxy", | |
30 | 32 | "dev": "NODE_ENV=development npm-run-all -p dev:*", |
31 | - "prod": "NODE_ENV=production npm-run-all prod:render-browser prod:assets -p prod:render-node prod:api prod:static" | |
33 | + "prod": "NODE_ENV=production npm-run-all prod:render-browser prod:assets -p prod:render-node prod:api prod:static prod:proxy" | |
32 | 34 | }, |
33 | 35 | "ava": { |
34 | 36 | "cache": false, |
35 | 37 | "files": [ |
@@ -116,8 +118,9 @@ | ||
116 | 118 | "feathers-hooks": "^1.0.0-pre.4", |
117 | 119 | "feathers-knex": "^2.1.0", |
118 | 120 | "feathers-rest": "^1.2.2", |
119 | 121 | "feathers-tcomb": "^1.0.0", |
122 | + "http-proxy": "^1.13.1", | |
120 | 123 | "isomorphic-fetch": "^2.2.1", |
121 | 124 | "knex": "^0.9.0", |
122 | 125 | "lnfs-cli": "^1.0.1", |
123 | 126 | "lodash": "^4.3.0", |
render.js | ||
---|---|---|
@@ -6,8 +6,8 @@ | ||
6 | 6 | const Url = require('url') |
7 | 7 | |
8 | 8 | const server = createServer(config) |
9 | 9 | |
10 | -server.listen(config.render.url.port, function () { | |
10 | +server.listen(config.render.port, function () { | |
11 | 11 | const renderUrl = Url.format(config.render.url) |
12 | - console.log(`render server listening at ${renderUrl}`) | |
12 | + console.log(`render server at ${renderUrl}`) | |
13 | 13 | }) |
static.js | ||
---|---|---|
@@ -5,8 +5,8 @@ | ||
5 | 5 | const Url = require('url') |
6 | 6 | |
7 | 7 | const server = createServer(config) |
8 | 8 | |
9 | -server.listen(config.static.url.port, function () { | |
9 | +server.listen(config.static.port, function () { | |
10 | 10 | const staticUrl = Url.format(config.static.url) |
11 | - console.log(`static server listening at ${staticUrl}`) | |
11 | + console.log(`static server at ${staticUrl}`) | |
12 | 12 | }) |
proxy.js | ||
---|---|---|
@@ -1,0 +1,9 @@ | ||
1 | +const config = require('app/config') | |
2 | +const createServer = require('app/proxy').createServer | |
3 | +const Url = require('url') | |
4 | + | |
5 | +const server = createServer(config) | |
6 | + | |
7 | +server.listen(config.proxy.port, function () { | |
8 | + console.log(`proxy server listening on port ${config.proxy.port}`) | |
9 | +}) |
Built with git-ssb-web