AndyL

AndyL

Another Sinatra-like server

Ruby has Sinatra 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 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:

#!/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?

Most Liked

mtrudel

mtrudel

Creator of Bandit

(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:

LostKobrakai

LostKobrakai

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:

Adding LV in the mix makes it 84 loc, 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.

kip

kip

ex_cldr Core Team

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.

derpycoder

derpycoder

It works flawlessly with Phoenix 1.7!!

And it’s a 2 line change!!

https://github.com/mtrudel/bandit#using-bandit-with-phoenix

tmecklem

tmecklem

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.

Where Next?

Popular in Discussions Top

jswny
I would like to better understand what the advantages/disadvantages of umbrella applications are compared to structuring your app as as s...
New
sashaafm
I’m trying to evaluate the best combo/stack for a BEAM Web app. Right now I’m exploring Yaws a bit, after having dealt with Phoenix for a...
New
jeramyRR
This is an interesting article to read. Elixir’s performance, like usual, is excellent. However, it seems like the high CPU usage is co...
New
laiboonh
Hi all, I am trying to convince my team to use liveview over the current react. What are some of the points where one should consider us...
New
axelson
Decided against including more info in the title, but the gist is that Plataformatec sponsored projects will continue with the assets bei...
New
arcanemachine
https://nitter.net/josevalim/status/1744395345872683471 https://twitter.com/josevalim/status/1744395345872683471
New
marciol
Please, let me know if this kind of discussion already took place in another topic . Hi all, how do you consider if is better to build ...
New
restack_oslo
Hello, Please pardon me for any faux paux. I am 46 and this is my first time on a forum of any kind. I wanted to to get answers from tho...
New
AstonJ
Can you believe the first professionally published Elixir book was published just 8 years ago? Since then I think we’ve seen more books f...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement