【问题标题】:Changing hostname and port with Vapor 3使用 Vapor 3 更改主机名和端口
【发布时间】:2018-01-25 19:10:10
【问题描述】:

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

Vapor 3 有不同的方法吗?

【问题讨论】:

    标签: swift docker vapor


    【解决方案1】:

    在 Vapor 4 中

    app.http.server.configuration.hostname = "127.0.0.1"
    app.http.server.configuration.port = 8000
    

    在 Vapor 3 中

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

    【讨论】:

    • 这是 Vapor 4 的正确答案,如他们的docs 所示。在public func configure(_ app: Application) throws 中执行此操作。
    • 这玩意儿就像蒸汽 4 的魅力。谢谢老兄!
    【解决方案2】:

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

    swift run Run --hostname 0.0.0.0 --port 9000

    EngineServer 有基于结构的配置 appears to be,但我认为它还不能在运行时进行配置。上次 Vapor 开发人员(在他们的 Slack 上)回答这个问题时,建议使用命令行参数方法。

    【讨论】:

    • 我们确实使用基于结构的配置,尽管这些配置仍然可以通过服务进行配置。
    • 我们可以用我们的ip地址代替0.0.0.0
    【解决方案3】:

    在蒸气中:稳定 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)
    

    【讨论】:

      【解决方案4】:

      官方解决方案(由维护者批准)

      您可以使用 NIOServerConfig。

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

      Vapor 版本是 3.0.3

      【讨论】:

      • 这是官方认可的:我问过维护者。
      • 这很好用。我将它包装在开发中.... if env == .development { let serverConfigure = NIOServerConfig.default(hostname: "0.0.0.0", port: 9090) services.register(serverConfiure) }
      【解决方案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
      }
      

      它在 Vapor 3.0.0 RC 2.4.1 上完美运行

      【讨论】:

      • 对于 Vapor 3.3.0,'EngineServerConfig' 已弃用:重命名为'NIOServerConfig';并且,“EngineServer”已被弃用:重命名为“NIOServer”。
      【解决方案6】:

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

      【讨论】:

        【解决方案7】:

        确保您使用的是 Vapor 3 版本,然后使用这个:

        vapor run --hostname=0.0.0.0 --port=8080
        

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

        CommandError: Unknown command 8080
        

        如果你按照我上面的建议去做,你会收到:

        [Deprecated] --option=value syntax is deprecated.
        

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

        如果没有参数后的=,我无法找到运行此命令的方法。

        【讨论】:

          【解决方案8】:

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

          --hostname localhost --port 8080

          【讨论】:

            【解决方案9】:

            您也可以在services 中注册您的EngineServerConfig

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

            let myServerConfig = try EngineServerConfig.detect(from: &env, port: 8081)
            services.register(myServerConfig)
            

            这应该适用于3.0.0-rc.2.2

            【讨论】:

              【解决方案10】:

              iOS Guy 所写的内容需要对 Vapor 3.3.1 进行一些修改 // Define Hostname & Port to listen to ... let myServerConfig = NIOServerConfig.default(hostname: "localhost", port: 8081) services.register(myServerConfig)

              所以NIOServerConfig.default只能与两个参数主机名和端口一起使用,如果只想更改端口号也可以使用。

              【讨论】:

                猜你喜欢
                • 2013-03-27
                • 1970-01-01
                • 2011-10-11
                • 2017-06-15
                • 1970-01-01
                • 2014-04-23
                • 2019-04-10
                • 2023-04-06
                • 2011-08-01
                相关资源
                最近更新 更多