feat: Working on hummingbird app

This commit is contained in:
2025-01-14 07:51:13 -05:00
parent 6225c32007
commit 4f47f1aed8
10 changed files with 223 additions and 86 deletions

View File

@@ -0,0 +1,80 @@
import Hummingbird
import Logging
public struct AppConfiguration {
public let hostname: String
public let port: Int
public let logLevel: Logger.Level?
public init(hostname: String, port: Int, logLevel: Logger.Level? = nil) {
self.hostname = hostname
self.port = port
self.logLevel = logLevel
}
}
/// Build application
/// - Parameter arguments: application arguments
public func buildApplication(_ arguments: AppConfiguration) async throws -> some ApplicationProtocol {
let environment = Environment()
let logger = {
var logger = Logger(label: "Todos")
logger.logLevel = arguments.logLevel ??
environment.get("LOG_LEVEL").map { Logger.Level(rawValue: $0) ?? .info } ??
.info
return logger
}()
let router = Router()
// Add middleware
router.addMiddleware {
// logging middleware
LogRequestsMiddleware(.info)
}
// Add health endpoint
router.get("/health") { _, _ -> HTTPResponse.Status in
return .ok
}
// let router = buildRouter()
//
let app = Application(
router: router,
configuration: .init(
address: .hostname(arguments.hostname, port: arguments.port),
serverName: "Todos"
),
logger: logger
)
return app
}
/// Build router
// func buildRouter() -> Router<AppRequestContext> {
// let router = Router()
//
// // Add middleware
//
// router.addMiddleware {
// // logging middleware
// LogRequestsMiddleware(.info)
// }
//
// // Add health endpoint
//
// router.get("/health") { _, _ -> HTTPResponse.Status in
// return .ok
// }
//
// return router
// }