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?
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?
You can use NIOServerConfig.
let serverConfiure = NIOServerConfig.default(hostname: "0.0.0.0", port: 9090)
services.register(serverConfiure)
Vapor version is 3.0.3
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.
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))
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.
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
You can set the hostname and port with command line flags:
--hostname localhost --port 8080
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
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)
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.