liveBook · Manning

Plug

As explained in Elixir in Action, Third Edition

Plug is an Elixir library for composing web application connection adapters and middleware behind a unified interface.

Plug provides a unified API for connecting Elixir web applications to HTTP servers and composing request middleware without exposing the underlying server implementation. With plug_cowboy, applications access Cowboy through Plug rather than calling Cowboy directly. section 11.3.1

A Plug.Cowboy child specification turns server options into a supervised HTTP-server component. The supervision tree can start listeners and request processes while keeping connection-management details behind Plug. section 11.3.2

Request handling combines router macros with explicit connection operations:

  • use Plug.Router, then apply plug :match and plug :dispatch to establish routing behavior. section 11.3.3
  • Define post and get clauses for paths such as /add_entry and /entries; Plug generates route-dispatch functions from these macros. section 11.3.3

Minimal supervised server

Declare {:plug_cowboy, "~> 2.6"}, return Plug.Cowboy.child_spec(scheme: :http, options: [port: 5454], plug: __MODULE__), and add the module to the supervision tree.

How Elixir in Action, Third Edition covers it

How deep it goes
Explains dependency setup, supervised startup, routing macros, and request handling, while stopping short of an in-depth library treatment.
Examples
Uses a todo server, /add_entry and /entries routes, a supervised port 5454 listener, and curl requests.
What it assumes
Assumes Elixir modules, supervision, macros, and application processes while leaving Plug internals largely unexplored.
What you can do after
Build a supervised HTTP endpoint that routes requests, delegates application work, and returns responses through Plug.Conn.
How it connects
Connects HTTP servers with Cowboy, supervision trees, HTTP routing, Elixir macros, and Plug.Conn.

This explainer was generated by Manning's knowledge graph from the text of Elixir in Action, Third Edition. The section links point to the parts of the book it draws on.