Raxx routing doesn't seem to work

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 :sweat_smile:

Thanks in advance! :smiley:

1 Like

Corresponding tweet for this thread:

https://twitter.com/dev_talk/status/1352205310728036353

Share link for this tweet.


Related portal:

Going to ping @crowdhailer for you as he’ll be able to give you the best answer being Raxx’s creator :blush: Hopefully he’ll be able to spare a few minutes to help you :smiley:

1 Like

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
2 Likes

I haven’t got an opportunity to test this yet, but it makes sense now that I think about it. Thank you!

1 Like

just tested it and /users/ still works fine while the other routes return a 404 :thinking:

defmodule Api.API.Actions.Users do
  use Raxx.SimpleServer
  alias Api.API

  @impl Raxx.SimpleServer
  def handle_request(_request = %{method: :GET, path: []}, _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
1 Like

Not sure what’s happening, as you don’t have a “users” path in the code you shared.

p.s. I’m not often fast on replying so you might want to try the #raxx slack channel, https://app.slack.com/client/T03EPRA2X/C56H3TBH8

The first code block (routes.ex) has it. Will check out slack though, it’s super confusing tho so it may take a while :sweat_smile:

Edit: found the issue and added an _ to the routes array to properly match the path

2 Likes