This document discusses building REST APIs with Elixir and the Maru framework. It provides instructions on getting started with Maru, including adding it as a dependency and starting the Maru router. It then shows an example of building a REST API for users with Maru, including defining routes to get all users or a single user by ID, and filtering and sorting the results.
8. Getting Started
# mix.exs
defp deps do
[ {:maru, "~> 0.11"} ]
end
def application do
[ applications: [:maru] ]
end
$ mix deps.get
$ iex -S mix
9. Dummily
|> Maru
|> JSON
|> kohei-kimura/dummily
case uri do
¡°/users¡± ->
¡°/users/1¡± -> ID 1
¡°/users?username=john¡± -> john
end
10. Dummily
defmodule Dummily do
defmodule Dummily.Router.Users do
use Maru.Router
namespace :users do
version "v1" do
params do
optional :username, type: String
optional :email, type: String
optional :posts, type: Integer
optional :sort, type: Atom, values: [:username, :id, :posts], default:
:id
end
get do
users = all_users() |> filter(params) |> sort_by(params[:sort])
conn |> put_status(200) |> json(users)
end
route_param :id, type: Integer do
get do
users = all_users() |> filter(params)
conn |> put_status(200) |> json(users)
end
end
end
end
end
11. |> Maru: REST API /DSL
|> / Phoenix
|>
|> REST API
|>
|> API GW