Closed nicemaker closed 2 years ago
I would do something like this: https://gist.github.com/brynedwards/f27b9df1fa32a879a535fce0335f1039
You could make the app
routes more concise like so:
app :: SpockM () () () ()
app = do
get ("search" <//> var) $ flip searchAction Nothing
get ("search" <//> var <//> var) searchAction
Either way I think it should do what you want.
Thanks a million, that gist also answered a few other haskell confusion I just had in my head. But theoretically, a maybevar
approach would work, wouldn't it?
then you would have var
, wildcard
and maybevar
.
I guess so. agrafix would probably have a better idea, I'm not familiar with Spock's routing internals
Yes, we could add maybevar to hook it to a function 'maybeHandler :: Maybe SomeType -> ...', but it would require support from the routing framework.
Alternatively you could also define such a combinator yourself (not tested, just a sketch):
optParamGet :: (Typeable p, FromHttpApiData p) => Path xs 'Open -> (Maybe p -> HVect Elim xs (SpockActionCtx ctx conn sess st ())) -> SpockCtxM ctx conn sess st ()
optParamGet basePath handle =
do get basePath (handle Nothing)
get (basePath <//> var) (\x -> handle (Just x))
thanks agrafix, going to try it out.
If have following paths:
/search/<location>/<terms>
/search/<location>/
If I haven't miss something I have to create two path definitions for that, like:
can that somehow be combined? I was thinking of something like this:
get("search"<//>var<//>maybevar)->searchAction
P.S. This is my first Spock App, I like it very much, please keep on! My haskell is still in baby shoes, if I would know more I would help you guys somehow.