Jsdr3398

Jsdr3398

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:

Marked As Solved

crowdhailer

crowdhailer

Creator of Raxx

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

Also Liked

Jsdr3398

Jsdr3398

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

AstonJ

AstonJ

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:

Jsdr3398

Jsdr3398

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

Where Next?

Popular Backend topics Top

DevotionGeo
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New
New
New
conradwt
Hi, I’m building an application that will have support for both the web and mobile. At this time, I’m using PhxGenAuth for authenticatio...
New
Ora2pgnewbie
Wanted to check if there is a UI available to use Ora2pg tool.
New
Fl4m3Ph03n1x
Background I am moving towards defined data structures in my application, and I find that TypedStruct is quite useful. Questions Howeve...
New
Fl4m3Ph03n1x
Background I am a fan of dialyzer and friends (looking at Gradient) and I try to have sepcs in my code as much as I can. To this end, I a...
New
ogoldberg
Any recommendations on good resources for learning Elixir, Phoenix, and Ash?
New
Fl4m3Ph03n1x
Background As I often do, I read books to learn and improve myself. I also enjoy teaching and helping others when I can, so this is somet...
New
Shiny
Hey community, this is my first post here so I will try to be as concise as possible and I appreciate any feedback. I’ve been writing Ro...
New

Other popular topics Top

Exadra37
I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
New
Exadra37
Please tell us what is your preferred monitor setup for programming(not gaming) and why you have chosen it. Does your monitor have eye p...
New
AstonJ
Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face: Perhaps if there’s enough peop...
New
Exadra37
On modern versions of macOS, you simply can’t power on your computer, launch a text editor or eBook reader, and write or read, without a ...
New
AstonJ
I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me. I think the Planck and the Preonic (o...
New
New
PragmaticBookshelf
Author Spotlight: Karl Stolley @karlstolley Logic! Rhetoric! Prag! Wow, what a combination. In this spotlight, we sit down with Karl ...
New
First poster: bot
Large Language Models like ChatGPT say The Darnedest Things. The Errors They MakeWhy We Need to Document Them, and What We Have Decided ...
New
New
AstonJ
If you’re getting errors like this: psql: error: connection to server on socket “/tmp/.s.PGSQL.5432” failed: No such file or directory ...
New