首页 > 解决方案 > 牛仔服务器在启动时退出而没有错误

问题描述

我正在运行一个简单的牛仔服务器。这是我的申请文件:

defmodule MyApp.Application do                                                      
  @moduledoc "Application file"                                                    
                                                                                   
  use Application                                                                  
                                                                                   
  def start(_type, _args) do                                                       
    children = [                                                                   
      Plug.Cowboy.child_spec(                                                      
        scheme: :http,                                                             
        plug: MyApp.Web.Endpoint,                                                   
        options: [port: 8000]                                                                                                                                          
      )                                                                            
    ]                                                                              
                                                                                   
    opts = [strategy: :one_for_one, name: MyApp.Supervisor]                         
    IO.puts("Starting...")                                                         
    Supervisor.start_link(children, opts) |> IO.inspect                            
  end                                                                              
end                                                                                

这是我的终点:

defmodule MyApp.Web.Endpoint do                                                     
  @moduledoc """                                                                   
  This is the module responsible for processing incoming requests                  
  """                                                                              
                                                                                   
  use Plug.Router                                                                  
  import Plug.Conn, only: [send_resp: 3]                                           
                                                                                   
  plug(Plug.Logger)                                                                
  plug(:match)                                                                     
  plug(:dispatch)                                                                  
                                                                                   
  get "/ping" do                                                                   
    send_resp(conn, 200, "pong")                                                                                                                                       
  end                                                                              
                                                                                   
end                                                                                

运行后mix run,我看到启动日志(“正在启动...”),但我的应用程序立即退出而不是监听连接。我如何让它无限期地听?

标签: elixircowboy

解决方案


混合运行文档

mix run可用于启动当前应用程序依赖项、应用程序本身,并可选择在其上下文中运行一些代码。对于长时间运行的系统,这通常通过以下--no-halt 选项完成:

mix run --no-halt

推荐阅读