diff --git a/Sources/App/Logger/Logger.swift b/Sources/App/Logger/Logger.swift index a0b68db..cd7a46e 100644 --- a/Sources/App/Logger/Logger.swift +++ b/Sources/App/Logger/Logger.swift @@ -1,7 +1,8 @@ +import Vapor import Bot -public final class PrintLogger: LoggerProtocol { +public final class PrintLogger: LoggerProtocol, Service { public func log(_ message: String) { - print("[WALL-E] \(message)") + print("\(Date()) | [WALL-E] \(message)") } } diff --git a/Sources/App/Middleware/RequestLoggerMiddleware.swift b/Sources/App/Middleware/RequestLoggerMiddleware.swift new file mode 100644 index 0000000..87541de --- /dev/null +++ b/Sources/App/Middleware/RequestLoggerMiddleware.swift @@ -0,0 +1,23 @@ +import Vapor +import Bot + +final class RequestLoggerMiddleware: Middleware, ServiceType { + private let logger: LoggerProtocol + + init(logger: LoggerProtocol) { + self.logger = logger + } + + func respond(to request: Request, chainingTo next: Responder) throws -> Future { + logger.log(""" + šŸ“ Request logger šŸ“ + \(request) + =========================== + """) + return try next.respond(to: request) + } + + static func makeService(for container: Container) throws -> RequestLoggerMiddleware { + return RequestLoggerMiddleware(logger: try container.make(PrintLogger.self)) + } +} diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index ecc3915..70ec6f5 100644 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -20,6 +20,8 @@ public func configure(_ config: inout Config, _ env: inout Environment, _ servic let mergeService = try makeMergeService(with: logger, gitHubEventsService) services.register(mergeService) + services.register(logger, as: PrintLogger.self) + services.register(RequestLoggerMiddleware.self) // Register routes to the router let router = EngineRouter.default() @@ -30,6 +32,7 @@ public func configure(_ config: inout Config, _ env: inout Environment, _ servic var middlewares = MiddlewareConfig() // Create _empty_ middleware config // middlewares.use(FileMiddleware.self) // Serves files from `Public/` directory middlewares.use(ErrorMiddleware.self) // Catches errors and converts to HTTP response + middlewares.use(RequestLoggerMiddleware.self) services.register(middlewares) logger.log("šŸ Ready") diff --git a/Sources/App/routes.swift b/Sources/App/routes.swift index 3d72707..5c8f753 100644 --- a/Sources/App/routes.swift +++ b/Sources/App/routes.swift @@ -8,10 +8,7 @@ public func routes( gitHubEventsService: GitHubEventsService ) throws { - router.get("/", use: notFoundHandler(with: logger)) - router.get(all, use: notFoundHandler(with: logger)) - - router.get("healthcheck") { request -> HTTPResponse in + router.get("health") { request -> HTTPResponse in switch mergeService.healthcheck.status.value { case .ok: return HTTPResponse(status: .ok) default: return HTTPResponse(status: .serviceUnavailable) @@ -27,10 +24,3 @@ public func routes( } } } - -private func notFoundHandler(with logger: LoggerProtocol) -> (Request) -> HTTPResponse { - return { request in - logger.log("\nšŸšØšŸšØšŸšØ Got an unexpected request šŸšØšŸšØšŸšØ\n\(request.http)\nšŸšØšŸšØšŸšØ ------------------------- šŸšØšŸšØšŸšØ") - return HTTPResponse(status: .notFound) - } -}