AndyL
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?
Trending in Discussions
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
Hi there! :wave:
@frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
Lately I’ve been thinking about how to organize components as a LiveView application grows. One of the pain points I’ve found (for myself...
New
What IDE or editor are you using for Elixir development?
Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
Other Trending Topics
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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.Routerand addplug(: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.
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_headersor 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.Routeras your router instead of a phoenix one.kip
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.
mtrudel
(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:
derpycoder
It works flawlessly with Phoenix 1.7!!
And it’s a 2 line change!!
https://github.com/mtrudel/bandit#using-bandit-with-phoenix