# Another Sinatra-like server

**URL:** <https://forum.elixirforum.com/t/another-sinatra-like-server/51705>\
**Category:** Discussions\
**Tags:** bandit\
**Created:** [November 10, 2022, 5:29pm UTC](https://forum.elixirforum.com/t/another-sinatra-like-server/51705 "2022-11-10T17:29:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndyL](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/andyl/32/3659_2.png) [@AndyL](https://forum.elixirforum.com/u/AndyL)\
**Post date:** [November 10, 2022, 5:29pm UTC](https://forum.elixirforum.com/t/another-sinatra-like-server/51705/1 "2022-11-10T17:29:42Z")

</div>

Ruby has [Sinatra](https://sinatrarb.com/) for simple web apps. In the Elixir world, there have been a few approaches to emulate Sinatra, using `Cowboy`, `Plug.Router`, or custom frameworks.

With Phoenix 1.7 I had a look at [Bandit](https://github.com/mtrudel/bandit) a new web server. With Bandit and Plug it’s easy to build a simple Sinatra-like server. Here’s an example written as an Elixir script:

```elixir
#!/usr/bin/env elixir

Mix.install([{:bandit, "~> 0.5"}])

defmodule Router do
  use Plug.Router
  plug :match
  plug :dispatch

  def run do
    Bandit.start_link(plug: Router)
    Process.sleep(:infinity)
  end

  get "/" do
    send_resp(conn, 200, "HELLO")
  end

  match _ do
    send_resp(conn, 404, "Not found")
  end
end

Router.run()

```

Has anyone else experimented with `Bandit` & `Plug.Router`? What is your experience pro/con?

---

<div class="post-metadata">

**Author:** ![tmecklem](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/tmecklem/32/6630_2.png) [@tmecklem](https://forum.elixirforum.com/u/tmecklem)\
**Post date:** [November 10, 2022, 5:38pm UTC](https://forum.elixirforum.com/t/another-sinatra-like-server/51705/2 "2022-11-10T17:38:29Z")

</div>

I love Sinatra in Ruby. My primary use case is to stand up fake servers for third party APIs instead of using things like VCR. I do the same in Elixir, but I just `use Plug.Router` and add `plug(:match)` in with cowboy as the implementation, and I get 90% of what Sinatra gives with the DSL path matching and all that. The rest is just plugs for auth, etc.

I haven’t checked out bandit too much yet.

I can say that it’s a huge pro for small things that don’t need a lot of orchestration and benefit from having the code in one place. I think I’d drop the straight Plug.Router for Phoenix if I knew my use case would grow over time to need its features and benefits.

---

<div class="post-metadata">

**Author:** ![LostKobrakai](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lostkobrakai/32/3072_2.png) [@LostKobrakai](https://forum.elixirforum.com/u/LostKobrakai)\
**Post date:** [November 10, 2022, 5:47pm UTC](https://forum.elixirforum.com/t/another-sinatra-like-server/51705/3 "2022-11-10T17:47:09Z")

</div>

Most often I’d argue what the reason is to not go for phoenix.

It has some useful additions on top of pure plug (e.g. conveniences like `:put_secure_browser_headers` or simpler CSRF handling) + websockets/channels. If you strip down phoenix it’s hardly less code than a plug based approach:

- [bandit.exs](https://github.com/wojtekmach/mix_install_examples/blob/main/bandit.exs) (28 loc)
- [phoenix.exs](https://github.com/wojtekmach/mix_install_examples/blob/main/phoenix.exs) (48 loc)

Adding LV in the mix makes it [84 loc](https://github.com/wojtekmach/mix_install_examples/blob/main/phoenix_live_view.exs), most of the additional lines being the LV itself.

So unless the “single module approach” is really the selling point – like e.g. the mentioned usecase of testservers – I’d always go straight for phoenix, because things are likely to grow to a point where it makes sense. You can still use `Plug.Router` as your router instead of a phoenix one.

---

<div class="post-metadata">

**Author:** ![kip](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kip/32/1440_2.png) [@kip](https://forum.elixirforum.com/u/kip)\
**Post date:** [November 10, 2022, 5:54pm UTC](https://forum.elixirforum.com/t/another-sinatra-like-server/51705/4 "2022-11-10T17:54:17Z")

</div>

I think of Bandit more as an alternative to Cowboy, not an alternative to Phoenix. Indeed with Phoenix 1.7 you can run Phoenix apps on either Cowboy or Bandit. The rest of it is just the boilerplate to get a plug-based app up and running on the HTTP server of choice.

---

<div class="post-metadata">

**Author:** ![mtrudel](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mtrudel/32/28190_2.png) [@mtrudel](https://forum.elixirforum.com/u/mtrudel)\
**Post date:** [November 10, 2022, 7:40pm UTC](https://forum.elixirforum.com/t/another-sinatra-like-server/51705/5 "2022-11-10T19:40:41Z")

</div>

(Bandit author here)

@kip is right on the money - Bandit is a an alternative to Cowboy (hence the lousy naming pun). The analog to Sinatra in the Elixir community would be `Plug.Router`, which is actually a really great little bit of tooling.

Plug.Router is just a Plug, so you can run it on top of any Plug-supporting webserver (Cowboy or Bandit, basically).

I talk about this in the ElixirConf talk where I introduced Bandit:

[![](https://img.youtube.com/vi/ZLjWyanLHuk/maxresdefault.jpg "ElixirConf 2021 - Mat Trudel - bandit on the loose! Networking in Elixir demystified") ](https://www.youtube.com/watch?v=ZLjWyanLHuk&t=1124)

---

<div class="post-metadata">

**Author:** ![derpycoder](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/derpycoder/32/31388_2.png) [@derpycoder](https://forum.elixirforum.com/u/derpycoder)\
**Post date:** [November 11, 2022, 4:59am UTC](https://forum.elixirforum.com/t/another-sinatra-like-server/51705/6 "2022-11-11T04:59:32Z")

</div>

It works flawlessly with Phoenix 1.7!!

> [@Phoenix 1.7.0-rc.0 is out!](https://forum.elixirforum.com/t/phoenix-1-7-0-rc-0-is-out/51625/30):
>
> I tried it, it’s awesome. I couldn’t make it work with Phoenix 1.6, however now it works flawlessly with Phoenix 1.7. Here everyone, if you are not familiar with Bandit, checkout a talk from Elixir Conf 2021: ElixirConf 2021 - Mat Trudel - bandit on the loose! Networking in Elixir demystified [https://www.youtube.com/watch?v=ZLjWyanLHuk](https://www.youtube.com/watch?v=ZLjWyanLHuk)

And it’s a 2 line change!!

[https://github.com/mtrudel/bandit#using-bandit-with-phoenix](https://github.com/mtrudel/bandit#using-bandit-with-phoenix)
