Created
January 29, 2017 17:43
-
-
Save dtinth/e8cc5149d636bafb3c823db3b4ce4b7a to your computer and use it in GitHub Desktop.
Use an IoC container
This file contains hidden or 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
function main () { | |
return Promise.coroutine(function * () { | |
- const logger = log4js.getLogger('main') | |
- | |
- logger.info('Loading certificate...') | |
- const JWT_CERTIFICATE_URL = requiredEnv('JWT_CERTIFICATE_URL') | |
- const certificate = (yield axios.get(JWT_CERTIFICATE_URL)).data | |
- | |
- logger.info('Connecting to MongoDB...') | |
- const DEFAULT_MONGO_URL = 'mongodb://127.0.0.1:27017/bemuse' | |
- const suppliedMongoUrl = process.env.MONGO_URL || process.env.MONGODB_URI | |
- const db = yield connectMongo(suppliedMongoUrl || DEFAULT_MONGO_URL) | |
- const factory = new MongoDBRepositoryFactory({ db }) | |
+ const services = Object.assign({ }, ...[ | |
+ configuration.logger.log4js, | |
+ configuration.config.env, | |
+ configuration.authentication.jwt, | |
+ configuration.database.mongodb, | |
+ configuration.repository.mongodb, | |
+ configuration.api | |
+ ]) | |
+ const container = createIoCContainer(services) | |
const port = +process.env.PORT || 8008 | |
- | |
- logger.info('Creating API...') | |
- const app = createApiServer({ | |
- logger: log4js.getLogger('HTTP'), | |
- legacyUserApiKey: requiredEnv('LEGACY_USER_API_KEY'), | |
- legacyUserRepository: factory.createLegacyUserRepository(), | |
- rankingEntryRepository: factory.createRankingEntryRepository(), | |
- playerRepository: factory.createPlayerRepository(), | |
- tokenValidator: createTokenValidator({ certificate }) | |
- }) | |
- | |
- logger.info('Let’s go!!!') | |
+ const app = yield container.get('api:app') | |
runApiServer(app, port) | |
})() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment