Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Bandit 最小サンプル

bandit_sample.livemd

Bandit 最小サンプル

# Mix.install([
#   {:bandit, path: "/home/hisaway/github/bandit"}
# ])
Mix.install([
  {:bandit, "~> 1.0"}
])

Section

defmodule MyPlug do
  use Plug.Router

  plug(:match)
  plug(:dispatch)

  get "/" do
    send_resp(conn, 200, "Hello from Bandit on Livebook!")
  end
  
  match _ do
    send_resp(conn, 404, "Not found")
  end
end
# 再評価でエラーにならないようにする
pid = Process.whereis(:my_bandit_server)
unless is_nil(pid), do: Supervisor.stop(pid)
{:ok, new_pid} = Bandit.start_link(plug: MyPlug, port: 4000)
Process.register(new_pid, :my_bandit_server)

http://localhost:4000/ https://localhost:4000/