How to add constraints on routes? #1934
-
Hi, First, thanks for this great framework! I have an action like this: class Products::Show < BrowserAction
get "/:brand/:ref" do
product = ProductQuery.new.brand(brand).ref(ref)
html ShowPage, product: product
end
end The issue is that when the browser requests Is it possible to add a constraint to the route to exclude the get "/:brand/:ref", to: "products#show", constraints: { brand: /^(?!css).*$/ } |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Hey! Glad you're enjoying it. There's not currently a way to handle that. But if you'd like to open an issue: https://github.com/luckyframework/lucky_router/issues I'm wondering if we can start taking advantage of annotations for this. @[Luucky::RouteField(ignore: /^(?!css).*$/)]
get "/:brand/:ref" do
html ShowPage
end |
Beta Was this translation helpful? Give feedback.
-
An issue was already open, I added a comment: luckyframework/lucky_router#16 In the meantime, to work around this issue, I serve the static file when the product is not found. class Products::Show < BrowserAction
get "/:brand/:ref" do
if product = ProductQuery.new.brand(brand).ref(ref).first?
html ShowPage, product: product
else
if File.exists?("public/#{brand}/#{ref}")
file "public/#{brand}/#{ref}", disposition: "inline"
else
raise Lucky::RouteNotFoundError.new(context)
end
end
end |
Beta Was this translation helpful? Give feedback.
-
I was on my way to configure NGINX to directly serve static files without passing the request to Lucky. But I realise that I can configure Lucky to handle static files before routes simply by moving # src/app_server.cr
class AppServer < Lucky::BaseAppServer
def middleware : Array(HTTP::Handler)
[
Lucky::RequestIdHandler.new,
Lucky::ForceSSLHandler.new,
Lucky::HttpMethodOverrideHandler.new,
Lucky::LogHandler.new,
Lucky::ErrorHandler.new(action: Errors::Show),
Lucky::RemoteIpHandler.new,
- Lucky::RouteHandler.new,
Lucky::StaticCompressionHandler.new("./public", file_ext: "gz", content_encoding: "gzip"),
- Lucky::StaticFileHandler.new("./public", fallthrough: false, directory_listing: false),
+ Lucky::StaticFileHandler.new("./public", fallthrough: true, directory_listing: false),
+ Lucky::RouteHandler.new,
Lucky::RouteNotFoundHandler.new,
] of HTTP::Handler
end
...
end This solves my problem and I think it's a better solution than adding constraints on routes. |
Beta Was this translation helpful? Give feedback.
I was on my way to configure NGINX to directly serve static files without passing the request to Lucky. But I realise that I can configure Lucky to handle static files before routes simply by moving
RouteHandler
afterStaticFileHandler
inmiddleware
and settingfallthrough
totrue
.