Skip to content

Commit

Permalink
Sperate out build router section of buildApplication (#5)
Browse files Browse the repository at this point in the history
Also introduce new type alias AppRequestContext
  • Loading branch information
adam-fowler authored Aug 28, 2024
1 parent 9479a80 commit 1bfd931
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions Sources/App/Application+build.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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(
Expand All @@ -38,3 +37,18 @@ public func buildApplication(_ arguments: some AppArguments) async throws -> som
)
return app
}

/// Build router
func buildRouter() -> Router<AppRequestContext> {
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
}

0 comments on commit 1bfd931

Please sign in to comment.