Skip to content

Commit

Permalink
Use a custom middleware to log any incoming request (#34)
Browse files Browse the repository at this point in the history
* `RequestLoggerMiddleware` because we are not carpenters

* Apply suggestions from code review

Co-Authored-By: Yasuhiro Inami <[email protected]>
  • Loading branch information
dmcrodrigues and inamiy authored Nov 4, 2019
1 parent 05f731b commit 121e6a4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
5 changes: 3 additions & 2 deletions Sources/App/Logger/Logger.swift
Original file line number Diff line number Diff line change
@@ -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)")
}
}
23 changes: 23 additions & 0 deletions Sources/App/Middleware/RequestLoggerMiddleware.swift
Original file line number Diff line number Diff line change
@@ -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))
}
}
3 changes: 3 additions & 0 deletions Sources/App/configure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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")
Expand Down
12 changes: 1 addition & 11 deletions Sources/App/routes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
}

0 comments on commit 121e6a4

Please sign in to comment.