Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing issue #2755: updating config storage inside HTTPServer #2882

Merged
merged 3 commits into from
Sep 14, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 17 additions & 2 deletions Sources/Vapor/HTTP/Server/HTTPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,23 @@ public final class HTTPServer: Server {
return connection.channel.closeFuture
}

public var configuration: Configuration {
get { _configuration }
set {
guard !didStart else {
_configuration.logger.warning("Cannot modify server configuration after server has been started.")
return
}
_configuration = newValue
}
}

private let responder: Responder
private var configuration: Configuration
private var _configuration: Configuration {
willSet {
self.application.storage[Application.HTTP.Server.ConfigurationKey.self] = newValue
}
}
private let eventLoopGroup: EventLoopGroup

private var connection: HTTPServerConnection?
Expand All @@ -250,7 +265,7 @@ public final class HTTPServer: Server {
) {
self.application = application
self.responder = responder
self.configuration = configuration
self._configuration = configuration
self.eventLoopGroup = eventLoopGroup
self.didStart = false
self.didShutdown = false
Expand Down
33 changes: 33 additions & 0 deletions Tests/VaporTests/ApplicationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,37 @@ final class ApplicationTests: XCTestCase {
XCTAssertEqual(returnedConfig.hostname, "0.0.0.0")
XCTAssertEqual(returnedConfig.port, 0)
}

func testConfigurationAddressDetailsReflectedWhenProvidedThroughServeCommand() throws {
let app = Application(.testing)
defer { app.shutdown() }

struct AddressConfig: Content {
let hostname: String
let port: Int
}

app.get("hello") { req -> AddressConfig in
let config = AddressConfig(hostname: req.application.http.server.configuration.hostname, port: req.application.http.server.configuration.port)
return config
}

app.environment.arguments = ["vapor", "serve", "--hostname", "0.0.0.0", "--port", "3000"]
XCTAssertNoThrow(try app.start())

XCTAssertNotNil(app.http.server.shared.localAddress)
XCTAssertEqual("0.0.0.0", app.http.server.configuration.hostname)
XCTAssertEqual(3000, app.http.server.configuration.port)

guard let localAddress = app.http.server.shared.localAddress,
localAddress.ipAddress != nil,
let port = localAddress.port else {
XCTFail("couldn't get ip/port from \(app.http.server.shared.localAddress.debugDescription)")
return
}
let response = try app.client.get("http://localhost:\(port)/hello").wait()
let returnedConfig = try response.content.decode(AddressConfig.self)
XCTAssertEqual(returnedConfig.hostname, "0.0.0.0")
XCTAssertEqual(returnedConfig.port, 3000)
}
}