-
Notifications
You must be signed in to change notification settings - Fork 16
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
Discussion / feature-request: allow using a monad other than Aff with HTTPure.serve #134
Comments
Yeah, I've actually been thinking about that quite a bit recently. I think the best approach here would be to replace all the
main :: Effect Unit
main = runReaderT (runLoggerT bootServer serverLogger) $ serverEnv
bootServer
:: forall m
. MonadAsk ServerEnv m
=> MonadLogger m
=> MonadEffect m
=> m Unit
bootServer = void do
serverOptions' <- serverOptions
router' <- router
logStart' <- logStart serverOptions'.port
liftEffect $ serve' serverOptions' router' logStart'
serverOptions :: forall m. MonadAsk ServerEnv m => m ListenOptions
serverOptions =
asks _.port <#>
{ hostname: "0.0.0.0"
, port: _
, backlog: Nothing
}
router :: forall m. MonadAsk ServerEnv m => m (Request -> ResponseM)
router =
ask <#> \env request ->
let
response = routeRequest request
logger = requestLogger request
in
requestEnv env request >>= runReaderT (runLoggerT response logger)
routeRequest :: forall m. MonadAff m => Request -> m Response
routeRequest = const $ liftAff $ notFound
logStart :: forall m. Monad m => Int -> m (Effect Unit)
logStart port =
let
message = info (intTag "Port" port) "Server up"
in
pure $ runLoggerT message serverLogger In any case, I'm absolutely not opposed to it, I just haven't implemented it myself yet. |
As an aside, even if we decide that we don't want to change HTTPure's API after all, I would at the very least really like to polish up the code above and drop it into an example to guide folks for how to use httpure with mtl stacks, and especially how to use a per-request mtl stack. |
Thanks @cprussin, that is helpful. In my case, I want to use the same logger everywhere, so I ended up with something like this: start :: LoggerT Aff Unit
start = do
logger <- wrap pure
liftEffect $ void $ HTTPure.serve
port
(router >>> flip runLoggerT logger)
(launchAff_ $ runLoggerT logStart logger)
where
router _ = liftAff HTTPure.notFound -- actually does more stuff than this
logStart = info (intTag "port" port) "Started HTTP server"
port = 8080 |
@Dretch just wondering, after some time has passed, do you have an opinion on if this should be an API change in HTTPure vs just a pattern that's documented somewhere? |
Hi @cprussin, I wish I could give you some insights, but I have barely touched any code using HTTPure since I made this issue, so I have not developed any opinions. Sorry! |
I have just converted my app to use
monad-logger
and as a result I have replacedAff a
withLoggerT Aff a
in many places in the codebase.HTTPure.serve
is a bit awkward here because it requires that the request handling function returnAff a
. This means that theLoggerT Aff a
toAff a
transformation has to be done inside the request handling function by usingrunLoggerT
. It might be neater ifserve
could work with any monads that implementMonadAff
- then the logging context could be automatically passed through from the caller.Is this a good idea?
The text was updated successfully, but these errors were encountered: