Substrate combines Open API Spex and decentralized routing definitions for REST services. This package is heavily inspired by Flask-Rebar.
- Automatic OpenAPI generation via Open API Spex.
- Request, response, and query param validation via Open API Spex.
- Decentralized routing definitions move route definitions out of the Phoenix router and into your controllers, alongside the rest of the endpoint's OpenAPI specifications.
# lib/your_app_web/registry.ex
defmodule YourAppWeb.Registry do
use Substrate.Registry, prefix: "/api"
end
# lib/your_app_web/router.ex
defmodule YourAppWeb.Router do
use Phoenix.Router
require Substrate
Substrate.collect_all do
entry(YourAppWeb.UserController, :list_users)
entry(YourAppWeb.UserController, :get_users)
end
end
# lib/your_app_web/controllers/users_controller.ex
defmodule YourAppWeb.UserListController do
use Phoenix.Controller
use YourAppWeb.Registry,
path: "/users",
pethod: :get
def list_users(conn, _path_args_, _validated_params_, _request_body) do
conn
|> json(%{
data: [%{name: "John McPhee"}]
})
end
end
The package can be installed by adding substrate
to your list of dependencies in mix.exs
:
def deps do
[
{:substrate, "~> 0.1"}
]
end