Last active
January 29, 2017 15:21
-
-
Save dtinth/fb640c5bb6301f257ecaf72fd68f5813 to your computer and use it in GitHub Desktop.
I think I need 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 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 port = +process.env.PORT || 8008 | |
const app = createApiServer({ | |
logger: log4js.getLogger('HTTP'), | |
legacyUserApiKey: requiredEnv('LEGACY_USER_API_KEY'), | |
legacyUserRepository: factory.createLegacyUserRepository(), | |
rankingEntryRepository: factory.createRankingEntryRepository(), | |
playerRepository: factory.createPlayerRepository(), | |
tokenValidator: createTokenValidator() | |
}) | |
runApiServer(app, port) | |
})() | |
} | |
function createApiServer ({ | |
logger, | |
rankingEntryRepository, | |
playerRepository, | |
legacyUserApiKey, | |
legacyUserRepository, | |
tokenValidator | |
} = { }) { | |
const app = express() | |
// Logging | |
if (logger) { | |
app.use(log4js.connectLogger(logger, { level: log4js.levels.INFO })) | |
} | |
// Legacy user | |
app.use('/legacyusers', createLegacyUserApi({ | |
legacyUserApiKey, | |
legacyUserRepository, | |
playerRepository | |
})) | |
// GraphQL | |
if (rankingEntryRepository) { | |
const rootValue = createRoot({ | |
rankingEntryRepository, | |
legacyUserRepository, | |
playerRepository, | |
tokenValidator | |
}) | |
app.use(cors()) | |
app.use(graphqlHTTP({ schema, rootValue, graphiql: true })) | |
} | |
return app | |
} | |
function createLegacyUserApi ({ | |
legacyUserApiKey: apiKey, | |
legacyUserRepository, | |
playerRepository | |
}) { | |
const router = express.Router() | |
... | |
return router | |
} | |
function createRoot ({ | |
rankingEntryRepository, | |
legacyUserRepository, | |
playerRepository, | |
tokenValidator | |
}) { | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment