-
-
Save chrisza4/8dbbfa8a001f62b6f4fb8a08f8127a1c 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
| const getPlayerRankingsRepositories = () => { | |
| rankingEntryRepository, | |
| tokenValidator, | |
| ...getPlayersUserRepositories() | |
| } | |
| const getPlayersUserRepositories = () => { | |
| playerRepository, | |
| legacyUserRepository | |
| } | |
| 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'), | |
| ...getPlayerRankingsRepositories() | |
| }) | |
| 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(getPlayersUserRepositories())) | |
| // GraphQL | |
| if (rankingEntryRepository) { | |
| const rootValue = createRoot(getPlayerRankingsRepositories()) | |
| 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