agrafix / Spock

Another Haskell web framework for rapid development
https://www.spock.li
678 stars 56 forks source link

Route with trailing slash #112

Open Hithroc opened 7 years ago

Hithroc commented 7 years ago

Is there a way to have different actions for /foo/bar/ and /foo/bar?

In my situation specificaly, I want all /foo/bar reuest be redirected to /foo/bar/ so relative paths on frontend lead to /foo/bar/baz instead of /foo/baz

agrafix commented 7 years ago

Currently there is not, this is normalized. If you'd like you could implement the behavior behind some configuration and send a PR :-)

Hithroc commented 7 years ago

Unfortunately I'm not too familiar with Spock's internal structure to do implement something like that.

Currently I have this hack workaround: I just take the raw path and see if there was a trailing slash.

directoryHook :: MonadIO m => ActionCtxT ctx m () -> ActionCtxT ctx m ()
directoryHook action = do
  rawPath <- T.decodeUtf8 . rawPathInfo <$> request
  if T.last rawPath /= '/'
  then redirect $ rawPath <> "/"
  else action
agrafix commented 7 years ago

Nice! I'll look into the real issue as I find time for it :-)