Reputation: 1227
The Config/server.json file doesn't seem to be read by Vapor 3, and as such I can't configure the hostname and port that a Vapor 3 app binds to.
Does Vapor 3 have a different method for this?
Upvotes: 18
Views: 8963
Reputation: 236558
If you need to configure different hosts for production and development you can switch the app environment:
Vapor 4 • Swift 5.10
public func configure(_ app: Application) throws {
switch app.environment {
case .production:
app.http.server.configuration = .init(address: .hostname("222.111.111.222", port: nil))
case .development, .testing:
app.http.server.configuration = .init(address: .hostname("0.0.0.0", port: 8080))
default: break
}
// code ...
}
Upvotes: 1
Reputation: 335
in Vapor 4
app.http.server.configuration.hostname = "127.0.0.1"
app.http.server.configuration.port = 8000
in Vapor 3
services.register(NIOServerConfig.default(hostname: "127.0.0.1", port: 8000))
Upvotes: 19
Reputation: 389
You can use NIOServerConfig.
let serverConfiure = NIOServerConfig.default(hostname: "0.0.0.0", port: 9090)
services.register(serverConfiure)
Vapor version is 3.0.3
Upvotes: 27
Reputation: 271
What iOS Guy had written needs a bit of modification for Vapor 3.3.1
// Define Hostname & Port to listen to ...
let myServerConfig = NIOServerConfig.default(hostname: "localhost", port: 8081)
services.register(myServerConfig)
So NIOServerConfig.default
can be used only with two parameters hostname & port and it can be used if one just wants to change the port number.
Upvotes: 0
Reputation: 11
In vapor: stable 3.1.10
open: configure.swift
In: public func configure()
Add the following:
// Define Hostname & Port to listen to ...
let myServerConfig = NIOServerConfig.default(hostname: "servers-hostname.local", port: 8080)
services.register(myServerConfig)
Upvotes: 0
Reputation: 306
Editing the Run scheme's "Arguments Passed on Launch" also worked for me
Upvotes: 7
Reputation: 340
Be sure you are using the Vapor 3 version then use this:
vapor run --hostname=0.0.0.0 --port=8080
If you don't add =
after the parameter, you are going to receive the follow complaint:
CommandError: Unknown command 8080
If you do as I recommended above, you are going to receive:
[Deprecated] --option=value syntax is deprecated.
Please use --option value (with no =) instead but the command will run and works fine.
I was unable to find a way to run this command without =
after the parameter.
Upvotes: 7
Reputation: 5656
My $0.02
import Vapor
/// Called before your application initializes.
///
/// [Learn More →](https://docs.vapor.codes/3.0/getting-started/structure/#configureswift)
public func configure(
_ config: inout Config,
_ env: inout Environment,
_ services: inout Services
) throws {
if env == .development {
services.register(Server.self) { container -> EngineServer in
var serverConfig = try container.make() as EngineServerConfig
serverConfig.port = 8989
serverConfig.hostname = "192.168.31.215"
let server = EngineServer(
config: serverConfig,
container: container
)
return server
}
}
//Other configure code
}
It works perfectly on Vapor 3.0.0 RC 2.4.1
Upvotes: 5
Reputation: 1537
You could also register your EngineServerConfig
in services
.
In configure.swift
, insert the code below:
let myServerConfig = try EngineServerConfig.detect(from: &env, port: 8081)
services.register(myServerConfig)
This should work for 3.0.0-rc.2.2
Upvotes: 1
Reputation: 5421
Currently, you can set the port and hostname when running your server:
swift run Run --hostname 0.0.0.0 --port 9000
There appears to be struct-based configuration for EngineServer
but I don’t think it is configurable at run time just yet. The last time the Vapor developer answered this question (on their Slack) the command-line argument method was the suggested one.
Upvotes: 22
Reputation: 1759
You can set the hostname and port with command line flags:
--hostname localhost --port 8080
Upvotes: 3