Skip to content

Commit

Permalink
refactor: application start up (#986)
Browse files Browse the repository at this point in the history
  • Loading branch information
lautarodragan authored Aug 17, 2019
1 parent 59f1c3d commit adcc327
Show file tree
Hide file tree
Showing 20 changed files with 229 additions and 229 deletions.
109 changes: 56 additions & 53 deletions src/app.ts → src/Frost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { getVerifiableClaimSigner } from '@po.et/poet-js'
import { MongoClient } from 'mongodb'
import * as Pino from 'pino'

import { API } from './api/API'
import { RestServer } from './api/RestServer'
import { Router } from './api/Router'
import { Configuration } from './configuration'
import { AccountController } from './controllers/AccountController'
import { ArchiveController } from './controllers/ArchiveController'
Expand All @@ -15,11 +16,7 @@ import { loggingConfigurationToPinoConfiguration } from './utils/Logging/Logging
import { SendEmail } from './utils/SendEmail'
import { Vault } from './utils/Vault/Vault'

import './extensions/Error'

require('dotenv').config({ path: '.env' })

export async function app(localVars: any = {}) {
export async function Frost(localVars: any = {}) {
const configuration: Configuration = loadConfigurationWithDefaults(localVars)
const logger = Pino(loggingConfigurationToPinoConfiguration(configuration))

Expand All @@ -41,14 +38,29 @@ export async function app(localVars: any = {}) {
logger.error(e, 'Error with Vault')
}

const configurationFrostAPI = configurationToFrostAPI(configuration)

const mongoClient = await MongoClient.connect(configuration.mongodbUrl)
const dbConnection = await mongoClient.db()
const accountCollection = dbConnection.collection('accounts')
const accountDao = AccountDao(accountCollection)

const sendEmail = SendEmail(configurationFrostAPI.sendEmail)
const sendEmail = SendEmail({
nodemailer: {
mandrill: {
apiKey: configuration.transactionalMandrill,
},
maildev: {
host: configuration.maildevPortTcpAddr,
port: configuration.maildevPortTcpPort,
ignoreTLS: configuration.maildevIgnoreTLS,
},
sendEmailDisabled: configuration.sendEmailDisabled,
emailTransportMailDev: configuration.emailTransportMailDev,
},
emailFrom: configuration.emailFrom,
emailReply: configuration.emailReply,
frostChangePassword: configuration.frostChangePassword,
frostVerifiedAccount: configuration.frostVerifiedAccount,
})

const mainnetNode = PoetNode(configuration.poetUrl)
const testnetNode = PoetNode(configuration.testPoetUrl)
Expand Down Expand Up @@ -90,57 +102,48 @@ export async function app(localVars: any = {}) {
},
})

const frostAPI = await API(accountController, archiveController, workController)(configurationFrostAPI).start()
const router = Router({
configuration: {
passwordComplexity: {
min: configuration.passwordComplexMin,
max: configuration.passwordComplexMax,
lowerCase: configuration.passwordComplexLowerCase,
upperCase: configuration.passwordComplexUpperCase,
numeric: configuration.passwordComplexNumeric,
symbol: configuration.passwordComplexSymbol,
},
maxApiTokens: configuration.maxApiTokens,
},
dependencies: {
accountController,
archiveController,
workController,
},
})

const restServer = await RestServer({
configuration: {
host: configuration.frostHost,
port: configuration.frostPort,
maxApiRequestLimitForm: configuration.maxApiRequestLimitForm,
maxApiRequestLimitJson: configuration.maxApiRequestLimitJson,
loggingConfiguration: {
loggingLevel: configuration.loggingLevel,
loggingPretty: configuration.loggingPretty,
},
},
dependencies: {
router,
},
})

await accountDao.createIndices()

return {
stop: async () => {
await frostAPI.stop()
await restServer.stop()
await mongoClient.close()
return true
},
}
}

const configurationToFrostAPI = (configuration: Configuration) => ({
host: configuration.frostHost,
port: configuration.frostPort,
maxApiRequestLimitForm: configuration.maxApiRequestLimitForm,
maxApiRequestLimitJson: configuration.maxApiRequestLimitJson,
passwordComplex: {
min: configuration.passwordComplexMin,
max: configuration.passwordComplexMax,
lowerCase: configuration.passwordComplexLowerCase,
upperCase: configuration.passwordComplexUpperCase,
numeric: configuration.passwordComplexNumeric,
symbol: configuration.passwordComplexSymbol,
},
sendEmail: {
nodemailer: {
mandrill: {
apiKey: configuration.transactionalMandrill,
},
maildev: {
host: configuration.maildevPortTcpAddr,
port: configuration.maildevPortTcpPort,
ignoreTLS: configuration.maildevIgnoreTLS,
},
sendEmailDisabled: configuration.sendEmailDisabled,
emailTransportMailDev: configuration.emailTransportMailDev,
},
emailFrom: configuration.emailFrom,
emailReply: configuration.emailReply,
frostChangePassword: configuration.frostChangePassword,
frostVerifiedAccount: configuration.frostVerifiedAccount,
},
poetUrl: configuration.poetUrl,
testPoetUrl: configuration.testPoetUrl,
maxApiTokens: configuration.maxApiTokens,
verifiedAccount: configuration.verifiedAccount,
pwnedCheckerRoot: configuration.pwnedCheckerRoot,
loggingConfiguration: {
loggingLevel: configuration.loggingLevel,
loggingPretty: configuration.loggingPretty,
},
})
117 changes: 0 additions & 117 deletions src/api/API.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface PasswordComplexConfiguration {
export interface PasswordComplexityConfiguration {
readonly min: number
readonly max: number
readonly lowerCase: number
Expand Down
92 changes: 92 additions & 0 deletions src/api/RestServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import * as Koa from 'koa'
import * as bodyParser from 'koa-bodyparser'
import * as helmet from 'koa-helmet'
import * as KoaRouter from 'koa-router'
import * as cors from 'koa2-cors'

import { securityHeaders } from '../securityHeaders'
import { createModuleLogger, LoggingConfiguration } from '../utils/Logging/Logging'

import { errorHandling } from '../middlewares/errorHandling'
import { logger } from '../middlewares/logger'

interface Configuration {
readonly host: string
readonly port: number
readonly maxApiRequestLimitForm: string
readonly maxApiRequestLimitJson: string
readonly loggingConfiguration: LoggingConfiguration
}

interface Dependencies {
readonly router: KoaRouter
}

interface Arguments {
readonly configuration: Configuration
readonly dependencies: Dependencies
}

interface APIMethods {
readonly stop: () => Promise<void>
}

export const RestServer = async ({
configuration: {
port,
host,
maxApiRequestLimitForm,
maxApiRequestLimitJson,
loggingConfiguration,
},
dependencies: {
router,
},
}: Arguments): Promise<APIMethods> => {
const logger = createModuleLogger(loggingConfiguration)(__dirname)

const koa = ConfiguredKoa({
maxApiRequestLimitForm,
maxApiRequestLimitJson,
loggingConfiguration,
})

koa
.use(router.routes())
.use(router.allowedMethods())

logger.info('Starting HTTP Server...')
const server = await koa.listen(port, host)
logger.info('Started HTTP Server.')

const stop = async () => {
logger.info('Stopping HTTP Server...')
await server.close()
logger.info('Stopped HTTP Server.')
}

return {
stop,
}
}

const ConfiguredKoa = ({
maxApiRequestLimitForm,
maxApiRequestLimitJson,
loggingConfiguration,
}: Partial<Configuration>) =>
new Koa()
.use(errorHandling())
.use(logger(createModuleLogger(loggingConfiguration)))
.use(helmet(securityHeaders))
.use(
cors({
origin: (ctx: any, next: any) => '*',
}),
)
.use(
bodyParser({
formLimit: maxApiRequestLimitForm,
jsonLimit: maxApiRequestLimitJson,
}),
)
Loading

0 comments on commit adcc327

Please sign in to comment.