Wildcard route in Grape

孤街醉人 提交于 2019-12-13 15:26:57

问题


I'm having issues getting Grape to respond to a purely wild card route.

By that I mean that if I have a simple route defined as

get do
...
end

I need to respond to all potential requests made to the API. The situation being I need to parse the path and params and then work through a decision tree based on those values.

I've tried a few variations on the route definition, such as:

get '/*' do
...
end

get '/(*)' do
...
end

But to no avail.

I know that there is some support for regular expressions and route anchoring in Grape, but I've had no luck figuring it out.

Thanks!


回答1:


You were close with the guess at the syntax, you just need to name the matching param:

E.g.

  get '*thing' do
    { :thing => params[:thing] }
  end

Using the * will make the param capture the rest of the URI path, ignoring / separators. But otherwise it behaves just like any other param.

Note this will only pickup within the Rack mount point at best, so if you have

  prefix      'api'
  version     'v2'

then it will respond to paths like /api/v2/hkfhqk/fwggw/ewfewg

If you are using this for custom 404 or other catch-all routes, then you need to add it at the end, otherwise it will mask more specific routes.



来源:https://stackoverflow.com/questions/30650832/wildcard-route-in-grape

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!