Commit 6d09dd8687c742866f2799345a876eaf0d9f002d
add proxy to work with dokku production deploy
Michael Williams committed on 2/14/2016, 4:14:32 AMParent: d33feed5f3b157fa00d79dd0c4297c6e10eaf539
Files changed
api.js | changed |
config/development.js | changed |
config/index.js | changed |
config/production.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 | }) |
config/development.js | ||
---|---|---|
@@ -1,7 +1,32 @@ | ||
1 | 1 | const join = require('path').join |
2 | 2 | |
3 | 3 | module.exports = { |
4 | + render: { | |
5 | + url: { | |
6 | + protocol: 'http:', | |
7 | + hostname: 'localhost', | |
8 | + port: 5000 | |
9 | + }, | |
10 | + port: 5000 | |
11 | + }, | |
12 | + static: { | |
13 | + url: { | |
14 | + protocol: 'http:', | |
15 | + hostname: 'localhost', | |
16 | + port: 5001 | |
17 | + }, | |
18 | + root: join(__dirname, '..', 'build'), | |
19 | + port: 5001 | |
20 | + }, | |
21 | + api: { | |
22 | + url: { | |
23 | + protocol: 'http:', | |
24 | + hostname: 'localhost', | |
25 | + port: 5002 | |
26 | + }, | |
27 | + port: 5002 | |
28 | + }, | |
4 | 29 | db: { |
5 | 30 | client: 'pg', |
6 | 31 | connection: { |
7 | 32 | 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 | } |
package.json | ||
---|---|---|
@@ -26,10 +26,11 @@ | ||
26 | 26 | "prod:render-node": "node render", |
27 | 27 | "prod:assets": "cpx \"app/assets/**/*\" build", |
28 | 28 | "prod:api": "node api", |
29 | 29 | "prod:static": "node static", |
30 | + "prod:proxy": "node proxy", | |
30 | 31 | "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" | |
32 | + "prod": "NODE_ENV=production npm-run-all prod:render-browser prod:assets -p prod:render-node prod:api prod:static prod:proxy" | |
32 | 33 | }, |
33 | 34 | "ava": { |
34 | 35 | "cache": false, |
35 | 36 | "files": [ |
@@ -116,8 +117,9 @@ | ||
116 | 117 | "feathers-hooks": "^1.0.0-pre.4", |
117 | 118 | "feathers-knex": "^2.1.0", |
118 | 119 | "feathers-rest": "^1.2.2", |
119 | 120 | "feathers-tcomb": "^1.0.0", |
121 | + "http-proxy": "^1.13.1", | |
120 | 122 | "isomorphic-fetch": "^2.2.1", |
121 | 123 | "knex": "^0.9.0", |
122 | 124 | "lnfs-cli": "^1.0.1", |
123 | 125 | "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,43 @@ | ||
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 | +const config = require('app/config') | |
8 | + | |
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 | +server.listen(config.proxy.port, function () { | |
25 | + console.log(`proxy server listening on port ${config.proxy.port}`) | |
26 | +}) | |
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