Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 使用Vapor 3更改主机名和端口_Swift_Docker_Vapor - Fatal编程技术网

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

Swift 使用Vapor 3更改主机名和端口,swift,docker,vapor,Swift,Docker,Vapor,Config/server.json文件似乎没有被Vapor 3读取,因此我无法配置Vapor 3应用程序绑定到的主机名和端口 Vapor 3有不同的方法吗?目前,您可以在运行服务器时设置端口和主机名: swift运行--主机名0.0.0.0--端口9000 对于EngineServer有基于结构的配置,但我认为它在运行时还不可配置。Vapor开发人员上次回答此问题时(在空闲时间),建议使用命令行参数方法。您可以使用命令行标志设置主机名和端口: --hostname localhost--por

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


Vapor 3有不同的方法吗?

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

swift运行--主机名0.0.0.0--端口9000


对于
EngineServer
有基于结构的配置,但我认为它在运行时还不可配置。Vapor开发人员上次回答此问题时(在空闲时间),建议使用命令行参数方法。

您可以使用命令行标志设置主机名和端口:


--hostname localhost--port 8080

您还可以在
服务中注册
EngineServerConfig

configure.swift
中,插入以下代码:

let myServerConfig = try EngineServerConfig.detect(from: &env, port: 8081)
services.register(myServerConfig)
这应该适用于
3.0.0-rc.2.2

我的$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
}
它在Vapor 3.0.0 RC 2.4.1

官方解决方案(经维护人员批准)上运行良好 您可以使用NIOServerConfig

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

Vapor版本是3.0.3

请确保您使用的是Vapor 3版本,然后使用此版本:

vapor run --hostname=0.0.0.0 --port=8080
如果您没有在参数后添加
=
,您将收到以下投诉:

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

[Deprecated] --option=value syntax is deprecated.
请改用--option值(不带=),但该命令将正常运行

在参数后面没有
=
的情况下,我无法找到运行此命令的方法。

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


在蒸汽中:稳定3.1.10

打开:configure.swift

在:public func configure()中

添加以下内容:

// Define Hostname & Port to listen to ...
let myServerConfig = NIOServerConfig.default(hostname: "servers-hostname.local", port: 8080)
services.register(myServerConfig)

iOS Guy编写的内容需要对Vapor 3.3.1进行一些修改
//定义要侦听的主机名和端口。。。
让myServerConfig=NIOServerConfig.default(主机名:“localhost”,端口:8081)
services.register(myServerConfig)


因此,
NIOServerConfig.default
只能与两个参数hostname&port一起使用,如果您只想更改端口号,就可以使用它。

在Vapor 4中

app.http.server.configuration.hostname = "127.0.0.1"
app.http.server.configuration.port = 8000
在蒸汽3中

services.register(NIOServerConfig.default(hostname: "127.0.0.1", port: 8000))

我们确实使用基于结构的配置,尽管这些配置仍然可以通过服务进行配置。我们可以使用我们的ip地址替换0.0.0。这是官方认可的配置:我问过维护人员。这非常有效。我把它包装在开发中。。。。如果env=.development{let serverconfure=NIOServerConfig.default(主机名:“0.0.0.0”,端口:9090)服务注册(serverconfure)}对于Vapor 3.3.0,“EngineServerConfig”已弃用:重命名为“NIOServerConfig”;而且,“EngineServer”已被弃用:重命名为“NIOServer”。这是Vapor 4的正确答案,如图所示。在
public func configure(uu.app:Application)抛出的
中执行此操作。此功能对vapor 4非常有用。谢谢你,伙计!