18

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?

Adam Young
  • 1,217
  • 10
  • 18

10 Answers10

27

Official Solution (as sanctioned by the maintainers)

You can use NIOServerConfig.

let serverConfiure = NIOServerConfig.default(hostname: "0.0.0.0", port: 9090)
services.register(serverConfiure)

Vapor version is 3.0.3

LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174
Passaction
  • 389
  • 3
  • 5
  • 3
    This is the officially sanctioned one: I asked maintainers. – mxcl Apr 04 '19 at 14:06
  • This works great. I wrapped it in development.... if env == .development { let serverConfiure = NIOServerConfig.default(hostname: "0.0.0.0", port: 9090) services.register(serverConfiure) } – zumzum Feb 09 '20 at 00:32
22

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.

tobygriffin
  • 5,339
  • 4
  • 36
  • 61
17

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))
yasinkbas
  • 315
  • 3
  • 10
  • 2
    This is the correct answer for Vapor 4, as shown in their [docs](https://docs.vapor.codes/4.0/server/#port). Do this in `public func configure(_ app: Application) throws`. – Rick Dec 18 '20 at 09:43
  • 1
    This thing works like a charm for vapor 4. Thanks man! – Fahim Rahman May 04 '21 at 21:30
7

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.

Grant Miller
  • 27,532
  • 16
  • 147
  • 165
7

Editing the Run scheme's "Arguments Passed on Launch" also worked for me

enter image description hereenter image description here

gflo
  • 186
  • 3
  • 13
5

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

imike
  • 5,515
  • 2
  • 37
  • 44
  • For Vapor 3.3.0, 'EngineServerConfig' is deprecated: renamed to 'NIOServerConfig'; and, 'EngineServer' is deprecated: renamed to 'NIOServer'. – marc-medley Apr 27 '19 at 01:51
3

You can set the hostname and port with command line flags:

--hostname localhost --port 8080

Daniel Seither
  • 1,647
  • 1
  • 13
  • 19
1

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

Honghao Z
  • 1,419
  • 22
  • 29
0

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)
iOS Guy
  • 11
0

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.

riskdoctor
  • 241
  • 3
  • 3