From 121e6a4086944bb2c3b85004f840d8c47c5ecc53 Mon Sep 17 00:00:00 2001 From: David Rodrigues <david.ndh@gmail.com> Date: Mon, 4 Nov 2019 10:47:38 +0000 Subject: [PATCH] Use a custom middleware to log any incoming request (#34) * `RequestLoggerMiddleware` because we are not carpenters * Apply suggestions from code review Co-Authored-By: Yasuhiro Inami <inamiy@gmail.com> --- Sources/App/Logger/Logger.swift | 5 ++-- .../Middleware/RequestLoggerMiddleware.swift | 23 +++++++++++++++++++ Sources/App/configure.swift | 3 +++ Sources/App/routes.swift | 12 +--------- 4 files changed, 30 insertions(+), 13 deletions(-) create mode 100644 Sources/App/Middleware/RequestLoggerMiddleware.swift 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<Response> { + 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) - } -}