soveran / syro

Simple router for web applications

Home Page:http://soveran.github.io/syro/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Syro getting confused while matching paths

estebanz01 opened this issue · comments

Hey! I have a little issue and maybe someone can help me to sort out: When I define two paths /search/:word/ using POST and other path /search/bot/ using GET and call the second one, it returns a 404 response.

Any ideas? this is what I got so far:

app = Syro.new do
  on 'search' do
    on :word do
      post do
        res.write 'POST it!'
      end
    end

    on 'bot' do
      get do
        res.write 'bit bit bit!'
      end
    end
  end
end

env_post = { 'REQUEST_METHOD' => 'POST', 'PATH_INFO' => '/search/casanova' }
env_get = { 'REQUEST_METHOD' => 'GET', 'PATH_INFO' => '/search/bot' }
app.call(env_post) # 200 OK
app.call(env_get) # 404 response

on(:symbol) {} will execute unconditionally if there is a segment. In this case, it will execute both for '/search/casanova/' and '/search/bot' (and '/search/anythingactually'). And you get a 404 because there is no get inside that block. It's always better to include capture matchers at the end of the block.

Try:

app = Syro.new do
  on 'search' do
    on 'bot' do
      get do
        res.write 'bit bit bit!'
      end
    end

    on :word do
      post do
        res.write 'POST it!'
      end
    end
  end
end

interesting. That did the trick. Any reason?

The first block that matches always win.

on true do
  res.write("win")
end

raise "nothing here"

In your case, every path that starts with '/search/{x}/', it will execute the first block:

on :word do
  res.write(req.path)
end

raise "nothing here"

# GET /search/foo # => /search/foo
# GET /search/bar # => /search/bar

In your case, every path that starts with '/search/{x}/', it will execute the first block:

on :word do
  res.write(req.path)
end

on "foo" do
  raise "nothing here"
end

# GET /search/foo # => /search/foo
# GET /search/bar # => /search/bar

If you change the order of the blocks:

on "foo" do
  res.write("/search/foo".reverse)
end

on :word do
  res.write(req.path)
end

# GET /search/foo  # => oof/hcraes/
# GET /search/foo2 # => /search/foo2
# GET /search/bar  # => /search/bar

nice! thanks. Closing this now.