git ssb

1+

dinoworm ๐Ÿ› / catstack



Commit 6d09dd8687c742866f2799345a876eaf0d9f002d

add proxy to work with dokku production deploy

Michael Williams committed on 2/14/2016, 4:14:32 AM
Parent: d33feed5f3b157fa00d79dd0c4297c6e10eaf539

Files changed

api.jschanged
config/development.jschanged
config/index.jschanged
config/production.jschanged
package.jsonchanged
render.jschanged
static.jschanged
proxy.jsadded
api.jsView
@@ -5,8 +5,8 @@
55 const Url = require('url')
66
77 const server = createServer(config)
88
9-server.listen(config.api.url.port, function () {
9+server.listen(config.api.port, function () {
1010 const apiUrl = Url.format(config.api.url)
11- console.log(`api server listening at ${apiUrl}`)
11+ console.log(`api server at ${apiUrl}`)
1212 })
config/development.jsView
@@ -1,7 +1,32 @@
11 const join = require('path').join
22
33 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+ },
429 db: {
530 client: 'pg',
631 connection: {
732 host : 'localhost',
config/index.jsView
@@ -3,26 +3,11 @@
33 const nodeEnv = env.NODE_ENV
44
55 module.exports = {
66 render: {
7- url: {
8- protocol: 'http:',
9- hostname: 'localhost',
10- port: 5000
11- }
127 },
138 static: {
14- url: {
15- protocol: 'http:',
16- hostname: 'localhost',
17- port: 5001
18- },
199 root: join(__dirname, '..', 'build')
2010 },
2111 api: {
22- url: {
23- protocol: 'http:',
24- hostname: 'localhost',
25- port: 5002
26- }
2712 }
2813 }
config/production.jsView
@@ -1,3 +1,38 @@
11 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+ }
338 }
package.jsonView
@@ -26,10 +26,11 @@
2626 "prod:render-node": "node render",
2727 "prod:assets": "cpx \"app/assets/**/*\" build",
2828 "prod:api": "node api",
2929 "prod:static": "node static",
30+ "prod:proxy": "node proxy",
3031 "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"
3233 },
3334 "ava": {
3435 "cache": false,
3536 "files": [
@@ -116,8 +117,9 @@
116117 "feathers-hooks": "^1.0.0-pre.4",
117118 "feathers-knex": "^2.1.0",
118119 "feathers-rest": "^1.2.2",
119120 "feathers-tcomb": "^1.0.0",
121+ "http-proxy": "^1.13.1",
120122 "isomorphic-fetch": "^2.2.1",
121123 "knex": "^0.9.0",
122124 "lnfs-cli": "^1.0.1",
123125 "lodash": "^4.3.0",
render.jsView
@@ -6,8 +6,8 @@
66 const Url = require('url')
77
88 const server = createServer(config)
99
10-server.listen(config.render.url.port, function () {
10+server.listen(config.render.port, function () {
1111 const renderUrl = Url.format(config.render.url)
12- console.log(`render server listening at ${renderUrl}`)
12+ console.log(`render server at ${renderUrl}`)
1313 })
static.jsView
@@ -5,8 +5,8 @@
55 const Url = require('url')
66
77 const server = createServer(config)
88
9-server.listen(config.static.url.port, function () {
9+server.listen(config.static.port, function () {
1010 const staticUrl = Url.format(config.static.url)
11- console.log(`static server listening at ${staticUrl}`)
11+ console.log(`static server at ${staticUrl}`)
1212 })
proxy.jsView
@@ -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