I have usual happstack case in which we have ServerPart Response
MonadPlus
in list and. Then msum
Chooses the one that does not fail and response generated and returned.
I have opinion that some actions - like cheking cookies - preparing connection context (authorising authenticated user, implementing counts, etc...) should be done in any incoming request - without any path info even defined yet.
Maybe there's some buzzy word I still not knowing yet, specially for such kinds of staff. Could someone advice please?
If you want to take certain actions for every request, you can add them in the do statement before your routing code. For example:
module Main where
import Happstack.Server
main = simpleHTTP nullConf $ do incCounter
mUser <- checkUserAuth
resp <- msum [ part1
, part2
, part3 mUser
]
logResponse resp
return resp
That will always run incCounter
and checkUserAuth
. Then it will try the various routes.
If one of the routes matches, it will then call logResponse
, and then finally return the resp
which will be sent to the user.
Note that while incCounter
and checkUserAuth
will always be run, logResponse
will only be run if one of the parts matches. If none do, then I am pretty sure the code will escape and return a 404. If you wanted logResponse
to always run, then you could add a handler to the msum
that always matches. For example:
resp <- msum [ part1
, part2
, part3 mUser
, notFound $ toResponse "Sorry, page not found."
]
That will almost always run. If one of the parts matches, but explicitly calls 'escape', then I am pretty sure logResponse
still won't be run. There are ways of dealing with that as well.
But, the short answer is, if you want something to happen even time, just put it before your msum
code.