From 1bfd931515f42ae7ef7c903c71d89aeccd832f42 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Wed, 28 Aug 2024 11:09:13 +0100 Subject: [PATCH] Sperate out build router section of buildApplication (#5) Also introduce new type alias AppRequestContext --- Sources/App/Application+build.swift | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/Sources/App/Application+build.swift b/Sources/App/Application+build.swift index 868699a..e88a1b5 100644 --- a/Sources/App/Application+build.swift +++ b/Sources/App/Application+build.swift @@ -11,6 +11,11 @@ public protocol AppArguments { var logLevel: Logger.Level? { get } } +// Request context used by application +typealias AppRequestContext = BasicRequestContext + +/// Build application +/// - Parameter arguments: application arguments public func buildApplication(_ arguments: some AppArguments) async throws -> some ApplicationProtocol { let environment = Environment() let logger = { @@ -21,13 +26,7 @@ public func buildApplication(_ arguments: some AppArguments) async throws -> som .info return logger }() - let router = Router() - // Add logging - router.add(middleware: LogRequestsMiddleware(.info)) - // Add health endpoint - router.get("/health") { _,_ -> HTTPResponse.Status in - return .ok - } + let router = buildRouter() let app = Application( router: router, configuration: .init( @@ -38,3 +37,18 @@ public func buildApplication(_ arguments: some AppArguments) async throws -> som ) return app } + +/// Build router +func buildRouter() -> Router { + let router = Router(context: AppRequestContext.self) + // Add middleware + router.add { + // logging middleware + LogRequestsMiddleware(.info) + } + // Add health endpoint + router.get("/health") { _,_ -> HTTPResponse.Status in + return .ok + } + return router +} \ No newline at end of file