diff --git a/Sources/App/Core/CFRayRouteLoggingMiddleware.swift b/Sources/App/Core/CFRayRouteLoggingMiddleware.swift index 7b9bbfd62..ca5d5449e 100644 --- a/Sources/App/Core/CFRayRouteLoggingMiddleware.swift +++ b/Sources/App/Core/CFRayRouteLoggingMiddleware.swift @@ -1,9 +1,24 @@ +// Copyright Dave Verwer, Sven A. Schmidt, and other contributors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + import Vapor + // Replica of the Vapor RouteLoggingMiddleware that's tweaked to explicitly expose the cf-ray header in the logger metadata for the request. public final class CFRayRouteLoggingMiddleware: Middleware { public let logLevel: Logger.Level - + public init(logLevel: Logger.Level = .info) { self.logLevel = logLevel } diff --git a/Sources/App/Core/Dependencies/EnvironmentClient.swift b/Sources/App/Core/Dependencies/EnvironmentClient.swift index 67ee40016..2d1bb0efe 100644 --- a/Sources/App/Core/Dependencies/EnvironmentClient.swift +++ b/Sources/App/Core/Dependencies/EnvironmentClient.swift @@ -41,6 +41,7 @@ struct EnvironmentClient { var current: @Sendable () -> Environment = { XCTFail("current"); return .development } var currentReferenceCache: @Sendable () -> CurrentReferenceCache? var dbId: @Sendable () -> String? + var enableCFRayLogging: @Sendable () -> Bool = { XCTFail("enableCFRayLogging"); return true } var mastodonCredentials: @Sendable () -> Mastodon.Credentials? var random: @Sendable (_ range: ClosedRange) -> Double = { XCTFail("random"); return Double.random(in: $0) } @@ -104,6 +105,11 @@ extension EnvironmentClient: DependencyKey { current: { (try? Environment.detect()) ?? .development }, currentReferenceCache: { .live }, dbId: { Environment.get("DATABASE_ID") }, + enableCFRayLogging: { + Environment.get("ENABLE_CF_RAY_LOGGING") + .flatMap(\.asBool) + ?? false + }, mastodonCredentials: { Environment.get("MASTODON_ACCESS_TOKEN") .map(Mastodon.Credentials.init(accessToken:)) @@ -145,6 +151,7 @@ extension EnvironmentClient: TestDependencyKey { var mock = Self() mock.appVersion = { "test" } mock.current = { .development } + mock.enableCFRayLogging = { true } return mock } } diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index 504f9e98d..64b5f1c70 100644 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -12,10 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. +import Dependencies import Fluent import FluentPostgresDriver import Vapor + @discardableResult public func configure(_ app: Application) async throws -> String { #if DEBUG && os(macOS) @@ -36,14 +38,16 @@ public func configure(_ app: Application) async throws -> String { // app.http.server.configuration.responseCompression = .enabled // app.http.server.configuration.requestDecompression = .enabled - // This stanza replaces the default middleware from Vapor, specifically replacing the - // default route logging middleware with the tweaked up version that reports on the cf-ray - // header (if any) from CloudFlare. - app.middleware = Middlewares() - app.middleware.use(Vapor.ErrorMiddleware.default(environment: app.environment)) - app.middleware.use(CFRayRouteLoggingMiddleware()) - // disable the 3 lines above to return to the default middleware setup before we add SPI-specific - // middleware. + @Dependency(\.environment) var environment + + if environment.enableCFRayLogging() { + // This stanza replaces the default middleware from Vapor, specifically replacing the + // default route logging middleware with the tweaked up version that reports on the cf-ray + // header (if any) from CloudFlare. + app.middleware = Middlewares() + app.middleware.use(Vapor.ErrorMiddleware.default(environment: app.environment)) + app.middleware.use(CFRayRouteLoggingMiddleware()) + } app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory)) app.middleware.use(ErrorMiddleware()) diff --git a/app.yml b/app.yml index e2306bc2b..bda21adf7 100644 --- a/app.yml +++ b/app.yml @@ -46,6 +46,7 @@ x-shared: &shared DATABASE_USERNAME: ${DATABASE_USERNAME} DATABASE_PASSWORD: ${DATABASE_PASSWORD} DATABASE_USE_TLS: ${DATABASE_USE_TLS} + ENABLE_CF_RAY_LOGGING: ${ENABLE_CF_RAY_LOGGING} FAILURE_MODE: ${FAILURE_MODE} GITHUB_TOKEN: ${GITHUB_TOKEN} GITLAB_API_TOKEN: ${GITLAB_API_TOKEN}