git ssb

1+

dinoworm ๐Ÿ› / catstack



Commit e1b55c0cca1102ca828c984773c303b09b965ff0

update proxy to work in dev and tests

Michael Williams committed on 2/15/2016, 3:17:26 AM
Parent: 6d09dd8687c742866f2799345a876eaf0d9f002d

Files changed

app/proxy.jsadded
config/development.jschanged
config/test.jschanged
features/support/hooks.jschanged
features/support/world.jschanged
package.jsonchanged
proxy.jschanged
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,31 +1,37 @@
11 const join = require('path').join
22
33 module.exports = {
4+ proxy: {
5+ port: 5000
6+ },
47 render: {
58 url: {
69 protocol: 'http:',
710 hostname: 'localhost',
11+ pathname: '/',
812 port: 5000
913 },
10- port: 5000
14+ port: 6000
1115 },
1216 static: {
1317 url: {
1418 protocol: 'http:',
1519 hostname: 'localhost',
16- port: 5001
20+ pathname: '/static/',
21+ port: 5000
1722 },
1823 root: join(__dirname, '..', 'build'),
19- port: 5001
24+ port: 6001
2025 },
2126 api: {
2227 url: {
2328 protocol: 'http:',
2429 hostname: 'localhost',
25- port: 5002
30+ pathname: '/api/',
31+ port: 5000
2632 },
27- port: 5002
33+ port: 6002
2834 },
2935 db: {
3036 client: 'pg',
3137 connection: {
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,8 +21,9 @@
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",
proxy.jsView
@@ -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
33 const Url = require('url')
4-const startsWith = require('lodash').startsWith
5-const assign = require('lodash').assign
64
7-const config = require('app/config')
5+const server = createServer(config)
86
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-
247 server.listen(config.proxy.port, function () {
258 console.log(`proxy server listening on port ${config.proxy.port}`)
269 })
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