Crowdhailer

Crowdhailer

Creator of Raxx

I have started experimenting with a web routing Domain Specific Language(DSL).

The DSL is inspired from working with several minimal web frameworks including Sinatra, roda etc. The heritage is not overly important as the question is about macro best practice. I wanted a simple DSL but my particular requirement was to match on url before method.

My first attempt looked like the following.

route "/user/:id" do
  :GET ->
    IO.inspect(id) # This implicit variable matches the identifier from the URL
    IO.inspect(request) # This implicit variable is always added to the context
    IO.inspect(config) # Same as request
    # actual work here to return request
  :POST ->
    # Other methods etc
end

However to create this DSL I have needed to use Macro.var, and this can mess with the hygiene of a macro. To see what would be required to not implicitly set variables I got to the following DSL.

route "/users/:id", [user_id] do
  get(request, config) ->
    IO.inspect(user_id) # explicitly named in route macro
    IO.inspect(request) # explicitly named in get macro
    IO.inspect(config) # Same as request
    # actual work here to return request
  post(r) ->
    IO.inspect(r) # Named r for brevity
    # config not used so not matched on
end

I see the trade off between the two as follows, version one is slightly more succinct but at the cost of being more “magic”. In addition this magic messing with the hygiene of the macro may have further side effects that I don’t know about yet.

Because of this I am leaning towards favoring the second more explicit DSL but would be interested in other opinions.


Update:

I have abandoned efforts to write a Sinatra style routing DSL. As any useful API must be documented I have decided to use that documentation as a router. My implementation of this is Raxx.Blueprint that will parse an API Blueprint file and generate forward requests to controllers based on that.

Showing Posts 1 to 10

OvermindDL1

OvermindDL1

I subscribe to the Python credo of “Explicit is better than Implicit”. ^.^

Crowdhailer

Crowdhailer OP

Creator of Raxx

It’s a simple guide however certainly gives an answer I wouldn’t argue with.

Crowdhailer

Crowdhailer OP

Creator of Raxx

Here is what an example chat server currently looks like, with this DSL

defmodule Example do
  use Tokumei

  config :port, 8080
  config :static, "./public"
  config :templates, "./templates"

  route "/" do
    get() ->
      ok(home_page())
    post(%{body: body}) ->
      {:ok, %{message: message}} = PublishForm.parse(body)
      {:ok, _} = ChatRoom.publish(message)
      redirect("/")
  end

  route "/updates" do
    get() ->
      {:ok, _} = ChatRoom.join()
      SSE.stream(:updates)
  end

  SSE.streaming :updates do
    {:message, message} ->
      {:send, %{event: "chat", data: "message"}
    _ ->
      {:nosend}
  end
end

Hopefully it the majority of the example is self explanatory.I hope to push the first version to hex soon

Crowdhailer

Crowdhailer OP

Creator of Raxx

There is now a first release of this router. The Tokumei framework has a super alpha first release and a generator to get you started

mix archive.install https://github.com/crowdhailer/tokumei/raw/master/tokumei_new.ez
mix tokumei.new my_app
cd my_app
iex -S mix

A more thorough example, which includes streaming server sent event, is included in the source repo. It’s a chatroom naturally.

Crowdhailer

Crowdhailer OP

Creator of Raxx

Working on an update that will route to specific modules, rather than writing actions inline. This will be helpful in writing larger applications. I am looking for opinions on possibilities for the routing sytanx.

required features are

  • fixed matching
  • variable matching
  • wildcard matching
  • subpath mounting
  • named routes for helpers

Option 1
as above, i.e. path first.
advantages are not having to repeat path declaration and being able to automatically return 405 response

@name :posts
route "/posts",
  GET: ShowPostsPage,
  POST: CreatePost

@name :post
route "/posts/:id",
  GET: ShowPostPage,
  PUT: UpdatePost

@name :search
route "/search/*blob",
  GET: SearchPage

mount "/api", APIController

Option 2
method first
advantages are similar to plug/sinatra/rails

@name :posts
get "/posts", ShowPostsPage,
post "/posts", CreatePost

@name :post
get "/posts/:id", ShowPostPage
put "/posts/:id", UpdatePost

@name :search
get "/search/*blob", SearchPage

mount "/api", APIControlle
OvermindDL1

OvermindDL1

I’d think Option 2 would be best so you could potentially match to out of order get/post’s like is common in Plug/Phoenix.

Crowdhailer

Crowdhailer OP

Creator of Raxx

Sorry, I don’t know what you mean by this:

Crowdhailer

Crowdhailer OP

Creator of Raxx

The streaming solution above was unsatisfactory. keeping track of streaming state from several possible endpoints in a single module was challenging.

A better idea is to use a module for each endpoint each of those having several callbacks for the state of the stream. This PR expands on the idea. Unified streaming solution. by CrowdHailer · Pull Request #47 · CrowdHailer/raxx · GitHub

Crowdhailer

Crowdhailer OP

Creator of Raxx

I can report that experiments on a streaming interface have now finished.
The most flexible approach was to use multiple callbacks for various stages in the streaming process.

Here is an example of a client streaming data to a server.

defmodule StreamingRequest do
  use Raxx.Server

  def handle_headers(%Raxx.Request{method: :PUT, body: true}, _config) do
    {:ok, io_device} = File.open("my/path")
    {[], {:file, device}}
  end

  def handle_fragment(fragment, state = {:file, device}) do
    IO.write(device, fragment)
    {[], state}
  end

  def handle_trailers(_trailers, state) do
    Raxx.response(:see_other)
    |> Raxx.set_header("location", "/")
  end
end

Here is an example of a server streaming data to the client.

defmodule SubscribeToMessages do
  use Raxx.Server

  def handle_headers(_request, _config) do
    {:ok, _} = ChatRoom.join()
    Raxx.response(:ok)
    |> Raxx.set_header("content-type", "text/plain")
    |> Raxx.set_body(true)
  end

  def handle_info({ChatRoom, data}, config) do
    {[Raxx.fragment(data)], config}
  end
end

For full details see the Raxx.Server documentation
I hope to share a few example usecases in the next few weeks.

Crowdhailer

Crowdhailer OP

Creator of Raxx

ace_http 0.5.0 released with HTTP/1.1 support for streaming.

This release of :ace_http implements the Raxx Streaming interface described above (latest interface docs

Where Next? Top

Trending in Discussions Top

budgie
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
axelson
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
achempion
I’ve been using Emacs as my main code editor for more than a two years. It’s a custom build version although I’ve tried doom emacs and sp...
New
juhalehtonen
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
budgie
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
jtormey
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
Null-logic-0
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

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
KristerV
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
mudasobwa
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews