feat: Working on hummingbird app
This commit is contained in:
80
Sources/HApp/App+build.swift
Normal file
80
Sources/HApp/App+build.swift
Normal 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
|
||||
// }
|
||||
29
Sources/HApp/App.swift
Normal file
29
Sources/HApp/App.swift
Normal file
@@ -0,0 +1,29 @@
|
||||
import ArgumentParser
|
||||
import Hummingbird
|
||||
import Logging
|
||||
|
||||
@main
|
||||
struct App: AsyncParsableCommand {
|
||||
|
||||
@Option(name: .shortAndLong)
|
||||
var hostname: String = "127.0.0.1"
|
||||
|
||||
@Option(name: .shortAndLong)
|
||||
var port: Int = 8080
|
||||
|
||||
@Option(name: .shortAndLong)
|
||||
var logLevel: Logger.Level?
|
||||
|
||||
func run() async throws {
|
||||
let app = try await buildApplication(.init(hostname: hostname, port: port, logLevel: logLevel))
|
||||
try await app.runService()
|
||||
}
|
||||
}
|
||||
|
||||
/// Extend `Logger.Level` so it can be used as an argument
|
||||
|
||||
#if hasFeature(RetroactiveAttribute)
|
||||
extension Logger.Level: @retroactive ExpressibleByArgument {}
|
||||
#else
|
||||
extension Logger.Level: ExpressibleByArgument {}
|
||||
#endif
|
||||
Reference in New Issue
Block a user