Search code examples
elixircowboy

Cowboy server exits on start without errors


I'm running a simple cowboy server. Here's my application file:

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                                                                                

And here is my endpoint:

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                                                                                

After running mix run, I see the start up log ("Starting..."), but my app exits immediately instead of listening for connections. How do I keep it listening indefinitely?


Solution

  • From the mix run docs:

    mix run can be used to start the current application dependencies, the application itself, and optionally run some code in its context. For long running systems, this is typically done with the --no-halt option:

    mix run --no-halt