2026-06-29 23:08:36 +00:00
|
|
|
import Hummingbird
|
2026-07-23 01:04:37 +00:00
|
|
|
import Infrastructure
|
|
|
|
|
import NIOCore
|
2026-06-29 23:08:36 +00:00
|
|
|
|
|
|
|
|
/// The website's request context.
|
|
|
|
|
///
|
|
|
|
|
/// Extends the core request storage with the negotiated language, defaulting to the default
|
2026-07-23 01:04:37 +00:00
|
|
|
/// supported language until ``LocalizationMiddleware`` resolves it from the request, and with
|
|
|
|
|
/// the connected client's address, so ``RateLimitMiddleware`` can key its budgets per client.
|
|
|
|
|
public struct WebsiteRequestContext: LocalizedRequestContext, RemoteAddressRequestContext {
|
2026-06-29 23:08:36 +00:00
|
|
|
|
|
|
|
|
// MARK: Properties
|
|
|
|
|
|
|
|
|
|
/// The core request context storage Hummingbird requires.
|
|
|
|
|
public var coreContext: CoreRequestContextStorage
|
|
|
|
|
/// The language identifier negotiated for the request.
|
|
|
|
|
public var language: String
|
2026-07-23 01:04:37 +00:00
|
|
|
/// The address of the connected client, captured from the source channel.
|
|
|
|
|
public let remoteAddress: SocketAddress?
|
2026-06-29 23:08:36 +00:00
|
|
|
|
|
|
|
|
// MARK: Initializers
|
|
|
|
|
|
|
|
|
|
/// Creates a request context for the given source.
|
|
|
|
|
/// - Parameter source: the source the context is initialized from.
|
2026-07-03 03:48:25 +00:00
|
|
|
public init(
|
|
|
|
|
source: Source,
|
|
|
|
|
) {
|
2026-06-29 23:08:36 +00:00
|
|
|
self.coreContext = .init(source: source)
|
2026-07-03 03:48:25 +00:00
|
|
|
self.language = .empty
|
2026-07-23 01:04:37 +00:00
|
|
|
self.remoteAddress = source.channel.remoteAddress
|
2026-06-29 23:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2026-07-03 03:48:25 +00:00
|
|
|
|
|
|
|
|
// MARK: - Constants
|
|
|
|
|
|
|
|
|
|
private extension String {
|
|
|
|
|
static let empty = ""
|
|
|
|
|
}
|