I’m trying to create a router where everything is in a collection of routes (similar to how I do my routes in expressjs). But it doesn’t seem to work. Can anyone help me out?
# routes.ex
defmodule Api.API.Router do
use Raxx.Router
alias Api.API.Actions
def stack(config) do
Raxx.Stack.new(
[
# Add global middleware here.
],
{__MODULE__, config}
)
end
section([{Raxx.Logger, Raxx.Logger.setup(level: :info)}], [
{%{path: []}, Actions.RootRoute},
{%{path: ["users"]}, Actions.Users}
])
section([{Raxx.Logger, Raxx.Logger.setup(level: :debug)}], [
{_, Actions.NotFound}
])
end
# controllers/users.ex
defmodule Api.API.Actions.Users do
use Raxx.SimpleServer
alias Api.API
@impl Raxx.SimpleServer
def handle_request(_request = %{method: :GET}, _state) do
response(:ok)
|> API.set_json_payload(%{path: "root"})
end
def handle_request(_request = %{method: :GET, path: ["test"]}, _state) do
response(:ok)
|> API.set_json_payload(%{path: "test"})
end
def handle_request(_request = %{method: :GET, path: ["testo"]}, _state) do
response(:ok)
|> API.set_json_payload(%{path: "testo"})
end
end
/users/ is normal and works fine, but I get a not found error when I try to access /users/test or /users/testo
Going to ping @crowdhailer for you as he’ll be able to give you the best answer being Raxx’s creator Hopefully he’ll be able to spare a few minutes to help you
Your first function head is matching all Gets requests. This is how Elixir does pattern matching,
Nothing really Raxx specific, apart from the fact it gives you the data structure.
If you don’t specify a path it will match on all, try this instead.
Try
@impl Raxx.SimpleServer
def handle_request(_request = %{method: :GET: path: []}, _state) do
response(:ok)
|> API.set_json_payload(%{path: "root"})
end