Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get matched route in ActionM using capture #266

Open
kt0d opened this issue Jun 10, 2020 · 2 comments
Open

Get matched route in ActionM using capture #266

kt0d opened this issue Jun 10, 2020 · 2 comments

Comments

@kt0d
Copy link

kt0d commented Jun 10, 2020

When using regex to match a route, I am able to later retrieve matched path with param "0", as in example described here:

https://hackage.haskell.org/package/scotty-0.12/docs/Web-Scotty.html#v:regex

Can this be done if I match a route using capture?

@jfraudeau
Copy link
Contributor

It's not possible in the current state of things. As the original pattern is turned into a WAI middleware, see :

scotty/Web/Scotty/Route.hs

Lines 86 to 108 in dc66aa0

addroute :: (ScottyError e, MonadIO m) => StdMethod -> RoutePattern -> ActionT e m () -> ScottyT e m ()
addroute method pat action = ScottyT $ MS.modify $ \s -> addRoute (route (routeOptions s) (handler s) (Just method) pat action) s
route :: (ScottyError e, MonadIO m) => RouteOptions -> ErrorHandler e m -> Maybe StdMethod -> RoutePattern -> ActionT e m () -> Middleware m
route opts h method pat action app req =
let tryNext = app req
{- |
We match all methods in the case where 'method' is 'Nothing'.
See https://github.com/scotty-web/scotty/issues/196
-}
methodMatches :: Bool
methodMatches =
case method of
Nothing -> True
Just m -> Right m == parseMethod (requestMethod req)
in if methodMatches
then case matchRoute pat req of
Just captures -> do
env <- liftIO $ catch (Right <$> mkEnv req captures opts) (\ex -> return . Left $ ex)
res <- evalAction h env action
maybe tryNext return res
Nothing -> tryNext
else tryNext

It should be possible to add the orignal pattern to ActionEnv

What do you think @ocramz ? This could be a nice new feature, I could see myself using it for logging and metrics metadata

@ocramz
Copy link
Collaborator

ocramz commented Oct 3, 2023

Agreed this would be nice to have !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants