Skip to content

Commit e652fcf

Browse files
Merge pull request #3577 from SwiftPackageIndex/suggested-changes
Suggested changes
2 parents 92fba58 + 5862cf5 commit e652fcf

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

Sources/App/Core/CFRayRouteLoggingMiddleware.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
1+
// Copyright Dave Verwer, Sven A. Schmidt, and other contributors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
115
import Vapor
216

17+
318
// Replica of the Vapor RouteLoggingMiddleware that's tweaked to explicitly expose the cf-ray header in the logger metadata for the request.
419
public final class CFRayRouteLoggingMiddleware: Middleware {
520
public let logLevel: Logger.Level
6-
21+
722
public init(logLevel: Logger.Level = .info) {
823
self.logLevel = logLevel
924
}

Sources/App/Core/Dependencies/EnvironmentClient.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ struct EnvironmentClient {
4141
var current: @Sendable () -> Environment = { XCTFail("current"); return .development }
4242
var currentReferenceCache: @Sendable () -> CurrentReferenceCache?
4343
var dbId: @Sendable () -> String?
44+
var enableCFRayLogging: @Sendable () -> Bool = { XCTFail("enableCFRayLogging"); return true }
4445
var mastodonCredentials: @Sendable () -> Mastodon.Credentials?
4546
var random: @Sendable (_ range: ClosedRange<Double>) -> Double = { XCTFail("random"); return Double.random(in: $0) }
4647

@@ -104,6 +105,11 @@ extension EnvironmentClient: DependencyKey {
104105
current: { (try? Environment.detect()) ?? .development },
105106
currentReferenceCache: { .live },
106107
dbId: { Environment.get("DATABASE_ID") },
108+
enableCFRayLogging: {
109+
Environment.get("ENABLE_CF_RAY_LOGGING")
110+
.flatMap(\.asBool)
111+
?? false
112+
},
107113
mastodonCredentials: {
108114
Environment.get("MASTODON_ACCESS_TOKEN")
109115
.map(Mastodon.Credentials.init(accessToken:))
@@ -145,6 +151,7 @@ extension EnvironmentClient: TestDependencyKey {
145151
var mock = Self()
146152
mock.appVersion = { "test" }
147153
mock.current = { .development }
154+
mock.enableCFRayLogging = { true }
148155
return mock
149156
}
150157
}

Sources/App/configure.swift

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
import Dependencies
1516
import Fluent
1617
import FluentPostgresDriver
1718
import Vapor
1819

20+
1921
@discardableResult
2022
public func configure(_ app: Application) async throws -> String {
2123
#if DEBUG && os(macOS)
@@ -36,14 +38,16 @@ public func configure(_ app: Application) async throws -> String {
3638
// app.http.server.configuration.responseCompression = .enabled
3739
// app.http.server.configuration.requestDecompression = .enabled
3840

39-
// This stanza replaces the default middleware from Vapor, specifically replacing the
40-
// default route logging middleware with the tweaked up version that reports on the cf-ray
41-
// header (if any) from CloudFlare.
42-
app.middleware = Middlewares()
43-
app.middleware.use(Vapor.ErrorMiddleware.default(environment: app.environment))
44-
app.middleware.use(CFRayRouteLoggingMiddleware())
45-
// disable the 3 lines above to return to the default middleware setup before we add SPI-specific
46-
// middleware.
41+
@Dependency(\.environment) var environment
42+
43+
if environment.enableCFRayLogging() {
44+
// This stanza replaces the default middleware from Vapor, specifically replacing the
45+
// default route logging middleware with the tweaked up version that reports on the cf-ray
46+
// header (if any) from CloudFlare.
47+
app.middleware = Middlewares()
48+
app.middleware.use(Vapor.ErrorMiddleware.default(environment: app.environment))
49+
app.middleware.use(CFRayRouteLoggingMiddleware())
50+
}
4751

4852
app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory))
4953
app.middleware.use(ErrorMiddleware())

app.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ x-shared: &shared
4646
DATABASE_USERNAME: ${DATABASE_USERNAME}
4747
DATABASE_PASSWORD: ${DATABASE_PASSWORD}
4848
DATABASE_USE_TLS: ${DATABASE_USE_TLS}
49+
ENABLE_CF_RAY_LOGGING: ${ENABLE_CF_RAY_LOGGING}
4950
FAILURE_MODE: ${FAILURE_MODE}
5051
GITHUB_TOKEN: ${GITHUB_TOKEN}
5152
GITLAB_API_TOKEN: ${GITLAB_API_TOKEN}

0 commit comments

Comments
 (0)