-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor towards three-layer architecture
Create app container Pass services down into lower layers (lower layers don't require singletons from src/services) TODO: - Move common utils (e.g., services/log) to `common` - BalanceCache not used by default (i.e., caching is behind feature flag) and not a singleton.
- Loading branch information
Alan Cohen
committed
Mar 23, 2016
1 parent
e5b5c21
commit dceb9c1
Showing
11 changed files
with
138 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,102 @@ | ||
'use strict' | ||
|
||
const _ = require('lodash') | ||
const co = require('co') | ||
const metadata = require('./controllers/metadata') | ||
const health = require('./controllers/health') | ||
const pairs = require('./controllers/pairs') | ||
const quote = require('./controllers/quote') | ||
const notifications = require('./controllers/notifications') | ||
const subscriptions = require('./models/subscriptions') | ||
const compress = require('koa-compress') | ||
const serve = require('koa-static') | ||
const route = require('koa-route') | ||
const errorHandler = require('five-bells-shared/middlewares/error-handler') | ||
const koa = require('koa') | ||
const path = require('path') | ||
const log = require('./services/log') | ||
const logger = require('koa-mag') | ||
const passport = require('koa-passport') | ||
const app = module.exports = koa() | ||
const Passport = require('koa-passport').KoaPassport | ||
const log = require('./services/log') | ||
|
||
function listen (koaApp, config, backend, ledgers) { | ||
if (config.getIn(['server', 'secure'])) { | ||
const spdy = require('spdy') | ||
const tls = config.get('tls') | ||
|
||
const options = { | ||
port: config.getIn(['server', 'port']), | ||
host: config.getIn(['server', 'bind_ip']), | ||
key: tls.key, | ||
cert: tls.cert, | ||
ca: tls.ca, | ||
crl: tls.crl, | ||
requestCert: config.getIn(['auth', 'client_certificates_enabled']), | ||
|
||
// Certificates are checked in the passport-client-cert middleware | ||
// Authorization check is disabled here to allow clients to connect | ||
// to some endpoints without presenting client certificates, or using a | ||
// different authentication method (e.g., Basic Auth) | ||
rejectUnauthorized: false | ||
} | ||
|
||
spdy.createServer( | ||
options, koaApp.callback()).listen(config.getIn(['server', 'port'])) | ||
} else { | ||
koaApp.listen(config.getIn(['server', 'port'])) | ||
} | ||
|
||
log('app').info('connector listening on ' + config.getIn(['server', 'bind_ip']) + ':' + | ||
config.getIn(['server', 'port'])) | ||
log('app').info('public at ' + config.getIn(['server', 'base_uri'])) | ||
for (let pair of config.get('tradingPairs')) { | ||
log('app').info('pair', pair) | ||
} | ||
|
||
// Start a coroutine that connects to the backend and | ||
// subscribes to all the ledgers in the background | ||
co(function * () { | ||
yield backend.connect() | ||
|
||
yield subscriptions.subscribePairs(config.get('tradingPairs'), ledgers, config) | ||
}).catch(function (err) { | ||
log('app').error(typeof err === 'object' && err.stack || err) | ||
}) | ||
} | ||
|
||
function createApp (config, backend, ledgers) { | ||
const koaApp = koa() | ||
|
||
koaApp.context.config = config | ||
koaApp.context.backend = backend | ||
koaApp.context.ledgers = ledgers | ||
|
||
// Configure passport | ||
const passport = new Passport() | ||
require('./services/auth')(passport, config) | ||
|
||
// Logger | ||
koaApp.use(logger()) | ||
koaApp.use(errorHandler({log: log('error-handler')})) | ||
koaApp.use(passport.initialize()) | ||
|
||
// Configure passport | ||
require('./services/auth') | ||
koaApp.use(route.get('/', metadata.getResource)) | ||
koaApp.use(route.get('/health', health.getResource)) | ||
koaApp.use(route.get('/pairs', pairs.getCollection)) | ||
|
||
// Logger | ||
app.use(logger()) | ||
app.use(errorHandler({log: log('error-handler')})) | ||
app.use(passport.initialize()) | ||
koaApp.use(route.get('/quote', quote.get)) | ||
|
||
app.use(route.get('/', metadata.getResource)) | ||
app.use(route.get('/health', health.getResource)) | ||
app.use(route.get('/pairs', pairs.getCollection)) | ||
koaApp.use(route.post('/notifications', notifications.post)) | ||
|
||
app.use(route.get('/quote', quote.get)) | ||
// Serve static files | ||
koaApp.use(serve(path.join(__dirname, 'public'))) | ||
|
||
app.use(route.post('/notifications', notifications.post)) | ||
// Compress | ||
koaApp.use(compress()) | ||
|
||
// Serve static files | ||
app.use(serve(path.join(__dirname, 'public'))) | ||
return { | ||
koaApp: koaApp, | ||
listen: _.partial(listen, koaApp, config, backend, ledgers) | ||
} | ||
} | ||
|
||
// Compress | ||
app.use(compress()) | ||
module.exports = createApp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.