使用Vapor 3更改主机名和端口

Ada*_*ung 9 docker swift vapor

Vapor 3似乎没有读取Config/server.json文件,因此我无法配置Vapor 3应用程序绑定的主机名和端口.

Vapor 3有不同的方法吗?

小智 21

您可以使用NIOServerConfig.

let serverConfiure = NIOServerConfig.default(hostname: "0.0.0.0", port: 9090)
services.register(serverConfiure)
Run Code Online (Sandbox Code Playgroud)

蒸汽版本是3.0.3

  • 这是官方批准的:我问维护者。 (2认同)

tob*_*fin 16

目前,您可以在运行服务器时设置端口和主机名:

swift run Run --hostname 0.0.0.0 --port 9000

似乎有基于结构的配置,EngineServer但我不认为它在运行时是可配置的.上一次Vapor开发人员回答了这个问题(在他们的Slack上),命令行参数方法是建议的.


yas*_*bas 10

在蒸气 4

app.http.server.configuration.hostname = "127.0.0.1"
app.http.server.configuration.port = 8000
Run Code Online (Sandbox Code Playgroud)

在蒸气 3

services.register(NIOServerConfig.default(hostname: "127.0.0.1", port: 8000))
Run Code Online (Sandbox Code Playgroud)

  • 这是 Vapor 4 的正确答案,如其[文档](https://docs.vapor.codes/4.0/server/#port)中所示。在“public func configure(_ app: Application) throws”中执行此操作。 (2认同)

Bru*_*ela 6

确保使用的是Vapor 3版本,然后使用以下版本:

vapor run --hostname=0.0.0.0 --port=8080
Run Code Online (Sandbox Code Playgroud)

如果您 =在参数后添加,则会收到以下投诉:

CommandError: Unknown command 8080
Run Code Online (Sandbox Code Playgroud)

如果您按照我上面的建议进行操作,您将收到:

[Deprecated] --option=value syntax is deprecated.
Run Code Online (Sandbox Code Playgroud)

请改用--option值(不带=),但该命令将运行并正常运行。

没有=参数,我无法找到一种运行此命令的方法。


imi*_*ike 5

我的 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
}
Run Code Online (Sandbox Code Playgroud)

它在 Vapor 3.0.0 RC 2.4.1 上完美运行


gfl*_*flo 5

编辑运行方案的“启动时传递的参数”也对我有用

在此处输入图片说明在此处输入图片说明