How to start Bandit HTTP Server test? Tutorials not working

I am trying to start a basic HTTP Server to test (using a throwaway supervisor) as per the following two tutorials:

https://hexdocs.pm/plug/readme.html
https://hexdocs.pm/bandit/Bandit.html

I have an Elixir project named rust_test where in my lib/rust_test.ex I have:

defmodule MyPlug do
  import Plug.Conn

  def init(options) do
    # initialize options
    options
  end

  def call(conn, _opts) do
    conn
    |> put_resp_content_type("text/plain")
    |> send_resp(200, "Hello world")
  end
end

#//MAIN MODULE
defmodule RustTest do

  def startHttpServer do
    IO.puts("Try to Start HTTP Server");
    webserverOpts = [port: 4000]
    webserver = {Bandit, plug: MyPlug, scheme: :http, options: webserverOpts}
    
    supervisorOpts = [strategy: :one_for_one ]
    IO.puts("About to start link");
   
    {:ok, _} = Supervisor.start_link([webserver], supervisorOpts)
    
    IO.puts("Plug now running on localhost:4000");
    Process.sleep(:infinity)
  end
end

To run the project I am doing iex -S mix then entering the command RustTest.startHttpServer().

But I am not getting past the Supervisor.start_link line (not getting the “Plug now running” line outputted). I am also getting no errors. Why no errors? And why is this not succeeding?

For dependencies I have:

      {:bandit, "~> 1.5"},
      {:websock_adapter, "~> 0.5.7"},

Thanks for any help.

Oops. I got it wrong. Code for setting webserver should be:

webserver = {Bandit, plug: MyPlug, scheme: :http, port: 4000 }
    
    supervisorOpts = [strategy: :one_for_one ]
    IO.puts("About to start link");
    
    {:ok, _} = Supervisor.start_link([webserver], supervisorOpts)

This works.

1 Like

Good thing that you fixed your issue. One nit, variable names in Elixir are usually written in snake case. It’s preferable to follow the conventions.