Skip to content

Commit

Permalink
chore: merge api back into base server (#46)
Browse files Browse the repository at this point in the history
The base server has issues connecting to the API within the
integration environment. By merging the two back together,
the service can stick to only using the DT probability setting
to connect to any DT service.
  • Loading branch information
Qard authored and watson committed Nov 6, 2018
1 parent eef41d3 commit e31c239
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 88 deletions.
66 changes: 0 additions & 66 deletions api.js

This file was deleted.

4 changes: 0 additions & 4 deletions ecosystem-workload.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,5 @@ module.exports = {
name: 'server',
script: './server.js',
instances: 1
}, {
name: 'api',
script: './api.js',
instances: 1
}]
}
4 changes: 0 additions & 4 deletions ecosystem.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,5 @@ module.exports = {
name: 'server',
script: './server.js',
instances: 1
}, {
name: 'api',
script: './api.js',
instances: 1
}]
}
26 changes: 14 additions & 12 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,19 @@ app.use(function (req, res, next) {
app.use(require('./server/coffee'))

var http = require('http')
app.use('/api', function (req, res) {
const opts = { method: req.method }

if (Math.random() < opbeansRedirectProbability) {
const service = opbeansServiceUrls[Math.floor(Math.random() * opbeansServiceUrls.length)]
opts.hostname = service.hostname
opts.port = service.port
opts.path = req.originalUrl
} else {
opts.hostname = conf.server.hostname
opts.port = conf.server.port2
opts.path = req.url // req.url will just contain the sub-path without the /api
app.use('/api', function (req, res, next) {
if (Math.random() > opbeansRedirectProbability) {
return next()
}

const service = opbeansServiceUrls[Math.floor(Math.random() * opbeansServiceUrls.length)]
const opts = {
method: req.method,
hostname: service.hostname,
port: service.port,
path: req.originalUrl
}

req.log.debug('proxying request: %s => %s:%s', req.originalUrl, opts.hostname, opts.port + opts.path)

var clientReq = http.request(opts)
Expand All @@ -122,6 +122,8 @@ app.use('/api', function (req, res) {
req.pipe(clientReq)
})

app.use('/api', require('./server/routes'))

app.get('*', function (req, res) {
res.sendFile(path.resolve(__dirname, 'client/build', 'index.html'))
})
Expand Down
3 changes: 1 addition & 2 deletions server/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ var conf = module.exports = {
protocol: process.env.OPBEANS_SERVER_PROTOCOL || 'http:',
auth: process.env.OPBEANS_SERVER_AUTH || '',
hostname: process.env.OPBEANS_SERVER_HOSTNAME || 'localhost',
port: process.env.OPBEANS_SERVER_PORT || process.env.PORT || 3000,
port2: process.env.OPBEANS_SERVER_PORT2 || 3001
port: process.env.OPBEANS_SERVER_PORT || process.env.PORT || 3000
},
pg: {
database: process.env.PGDATABASE || 'opbeans'
Expand Down

0 comments on commit e31c239

Please sign in to comment.