Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion Sources/App/Core/CFRayRouteLoggingMiddleware.swift
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down
7 changes: 7 additions & 0 deletions Sources/App/Core/Dependencies/EnvironmentClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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>) -> Double = { XCTFail("random"); return Double.random(in: $0) }

Expand Down Expand Up @@ -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:))
Expand Down Expand Up @@ -145,6 +151,7 @@ extension EnvironmentClient: TestDependencyKey {
var mock = Self()
mock.appVersion = { "test" }
mock.current = { .development }
mock.enableCFRayLogging = { true }
return mock
}
}
Expand Down
20 changes: 12 additions & 8 deletions Sources/App/configure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())
Expand Down
1 change: 1 addition & 0 deletions app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
Loading