📎 Webclip
Building a web framework from scratch in Elixir
The article walks through building a simple web framework on top of Plug and Cowboy in Elixir. It starts with a basic module that handles requests through init/1 and call/2, then adds response headers, pattern matching on paths and methods, composable plugs, templates, and Ecto-backed user lookup.
Reading notes#
- Plug modules respond to web requests through init/1 and call/2, with init/1 running once at startup and call/2 running for each request.
- The connection object is immutable, so functions like put_resp_header return a modified copy of conn that must be passed along the chain.
- Pattern matching on conn.path_info and method lets one router handle different routes and extract values such as user_id from the URL.
- A macro can wrap the common call logic so routers can share the same routing syntax without repeating code.
- Plugs can be composed, so one plug can call another, which makes it possible to layer routing, logging, authentication, and other behavior.
- EEx templates can render HTML, and templates can be precompiled with EEx.function_from_file to avoid parsing the file on every request.
- Ecto is used to define a User schema, create the users table through a migration, and fetch a record from the database in the user route.
- The example inserts a User record in IEx and then serves the user’s first and last name from the template.
- Testing is done by passing a fake conn to the router’s call function and checking the returned state, status, and response body with ExUnit and Plug.Test.
- The article says routing based on pattern matching is fast because the Erlang VM optimizes it, and it argues that this thin layer over Plug and Cowboy keeps performance high.
- Suggested next steps include organizing code into submodules, adding more macros, and using module reloading tools during development.
