-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: application start up (#986)
- Loading branch information
1 parent
59f1c3d
commit adcc327
Showing
20 changed files
with
229 additions
and
229 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 was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
src/api/PasswordComplexConfiguration.ts → src/api/PasswordComplexityConfiguration.ts
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 |
---|---|---|
@@ -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, | ||
}), | ||
) |
Oops, something went wrong.