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.

Last Post!

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

Where Next?

Popular in Discussions Top

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
und0ck3d
Hello everyone! A few days ago I’ve created a topic here about how people were creating CMSs with Elixir and Phoenix. I’ve been studying...
New
joeerl
I’m playing with Elixir - It’s fun. I think @rvirding does give Elixir courses these days. Re: files and database - when I given Erlang ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
tmbb
This is a post to discuss the new Phoenix LiveView functionality. From Chris’s talk, it appears that they generate all HTML on the serve...
342 18595 126
New
PragTob
Hello everyone, I know we had quite some threads (read through lots of them) about background job processing but it remains a hotly deba...
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

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31525 112
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement