git ssb

1+

dinoworm ๐Ÿ› / catstack



Commit ed26f7c5c655a65d700ecac4cddac76d22aad40a

Merge pull request #49 from enspiral-craftworks/feature/proxy

add proxy to work with dokku production deploy
Mikey committed on 2/15/2016, 3:38:22 AM
Parent: e51a1ed250d9a835a0a95e6e9c07fd3f450e7117
Parent: e1b55c0cca1102ca828c984773c303b09b965ff0

Files changed

api.jschanged
app/proxy.jsadded
config/development.jschanged
config/index.jschanged
config/production.jschanged
config/test.jschanged
features/support/hooks.jschanged
features/support/world.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 })
app/proxy.jsView
@@ -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.jsView
@@ -1,7 +1,38 @@
11 const join = require('path').join
22
33 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+ },
435 db: {
536 client: 'pg',
637 connection: {
738 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 }
config/test.jsView
@@ -1,21 +1,45 @@
1-const join = require('path').join
2-const env = process.env
3-const nodeEnv = env.NODE_ENV
4-
51 module.exports = {
2+ proxy: {
3+ port: 5050
4+ },
65 render: {
76 url: {
8- port: 6000
9- }
7+ protocol: 'http:',
8+ hostname: 'localhost',
9+ pathname: '/',
10+ port: 5050
11+ },
12+ port: 6050
1013 },
1114 static: {
1215 url: {
13- port: 6001
14- }
16+ protocol: 'http:',
17+ hostname: 'localhost',
18+ pathname: '/static/',
19+ port: 5050
20+ },
21+ port: 6051
1522 },
1623 api: {
1724 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
1943 }
2044 }
2145 }
features/support/hooks.jsView
@@ -9,18 +9,19 @@
99 module.exports = function () {
1010 const servers = _.mapValues({
1111 static: require('app/static'),
1212 api: require('app/api'),
13- render: require('app/render')
13+ render: require('app/render'),
14+ proxy: require('app/proxy')
1415 }, function (module) {
1516 return module.createServer(config)
1617 })
1718
1819 function start (cb) {
1920 parallel(
2021 _.map(servers, function (server, name) {
2122 return function (callback) {
22- server.listen(config[name].url.port, callback)
23+ server.listen(config[name].port, callback)
2324 }
2425 }),
2526 cb
2627 )
features/support/world.jsView
@@ -1,9 +1,9 @@
11 const Browser = require('zombie')
22
33 const config = require('app/config')
44
5-Browser.localhost(config.render.url.hostname, config.render.url.port)
5+Browser.localhost('localhost', config.proxy.port)
66
77 function World () {
88 // this.browser will be available in step definitions
99 this.browser = new Browser()
package.jsonView
@@ -21,15 +21,17 @@
2121 "dev:assets": "cpx \"app/assets/**/*\" build -w",
2222 "dev:livereload": "wtch -d build -e html,css,png,gif,jpg | garnish --level debug",
2323 "dev:api": "node-dev api",
2424 "dev:static": "node-dev static",
25+ "dev:proxy": "node-dev proxy",
2526 "prod:render-browser": "browserify app/render -o build/bundle.js -g envify -g uglifyify",
2627 "prod:render-node": "node render",
2728 "prod:assets": "cpx \"app/assets/**/*\" build",
2829 "prod:api": "node api",
2930 "prod:static": "node static",
31+ "prod:proxy": "node proxy",
3032 "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"
3234 },
3335 "ava": {
3436 "cache": false,
3537 "files": [
@@ -116,8 +118,9 @@
116118 "feathers-hooks": "^1.0.0-pre.4",
117119 "feathers-knex": "^2.1.0",
118120 "feathers-rest": "^1.2.2",
119121 "feathers-tcomb": "^1.0.0",
122+ "http-proxy": "^1.13.1",
120123 "isomorphic-fetch": "^2.2.1",
121124 "knex": "^0.9.0",
122125 "lnfs-cli": "^1.0.1",
123126 "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,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