Not being able to run the most simple web server with Plug and Cowboy

I’m following the Plug docs to run a web server but I cant find what i’m missing
application.ex file

defmodule MyApp.Application do


  use Application

  def start(_type, _args) do
    children = [
      {Plug.Cowboy, scheme: :http, plug: MyApp.MyRouter, options: [port: 5000]}
    ]

    opts = [strategy: :one_for_one, name: MyApp.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

my_router.ex file

defmodule MyApp.MyRouter do
  use Plug.Router

  plug :match
  plug :dispatch

  get "/hello" do
    send_resp(conn, 200, "world")
  end

  match _ do
    send_resp(conn, 404, "oops")
  end
end

mix.exs file

defmodule Api.MixProject do
  use Mix.Project

  def project do
    [
      app: :api,
      version: "0.1.0",
      elixir: "~> 1.16",
      start_permanent: Mix.env() == :prod,
      deps: deps()
    ]
  end

  # Run "mix help compile.app" to learn about applications.
  def application do
    [
      extra_applications: [:logger]
    ]
  end

  # Run "mix help deps" to learn about dependencies.
  defp deps do
    [
      {:plug_cowboy, "~> 2.0"}
      # {:dep_from_hexpm, "~> 0.3.0"},
      # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
    ]
  end
end

both application and my_router files are under lib → my_app dir

Hi @henriquesati thanks for posting the code. It looks like when you generated the mix project you didn’t generate with --sup and instead added a supervisor later. This is fine, but you need to modify your mix.exs def application function to point to that supervisor:

  def application do
    [
      mod: {MyApp.Application, []},
      extra_applications: [:logger]
    ]
  end
1 Like

i’m glad I asked even that being silly, you saved from more headache, thanks :slight_smile: