Skip to content

Commit

Permalink
Merge pull request #12 from ambroslins/simplify-runners
Browse files Browse the repository at this point in the history
Simplify `renderServer` for `ReaderT` and `ExceptT`
  • Loading branch information
anton-k authored Oct 19, 2023
2 parents 5f9bbd9 + 3b2cccf commit 6887c95
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/mig-example-apps/Counter/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ main :: IO ()
main = do
env <- initEnv
putStrLn ("The counter server listens on port: " <> show port)
runServer port . withSwagger def =<< (renderServer server env)
runServer port . withSwagger def $ renderServer server env
where
port = 8085

Expand Down
18 changes: 7 additions & 11 deletions mig/src/Mig/Core/Class/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Mig.Core.Class.Server (
fromReaderExcept,
) where

import Control.Monad.Except
import Control.Monad.Except (ExceptT, runExceptT)
import Control.Monad.Reader
import Data.Kind
import Data.OpenApi (ToParamSchema, ToSchema)
Expand Down Expand Up @@ -111,18 +111,17 @@ instance HasServer IO where
renderServer = id

instance HasServer (ReaderT env IO) where
type ServerResult (ReaderT env IO) = env -> IO (Server IO)
type ServerResult (ReaderT env IO) = env -> Server IO
renderServer server initEnv = fromReader initEnv server

-- | Render reader server to IO-based server
fromReader :: env -> Server (ReaderT env IO) -> IO (Server IO)
fromReader env server =
flip runReaderT env $ ReaderT $ \e -> pure $ hoistServer (flip runReaderT e) server
fromReader :: env -> Server (ReaderT env IO) -> Server IO
fromReader env = hoistServer (flip runReaderT env)

instance HasServer (ReaderT env (ExceptT Text IO)) where
type
ServerResult (ReaderT env (ExceptT Text IO)) =
env -> IO (Server IO)
env -> Server IO

renderServer server initEnv = fromReaderExcept initEnv server

Expand All @@ -131,11 +130,8 @@ fromReaderExcept ::
forall env.
env ->
Server (ReaderT env (ExceptT Text IO)) ->
IO (Server IO)
fromReaderExcept env server =
flip runReaderT env $
ReaderT $
\e -> pure $ mapServerFun (handle e) server
Server IO
fromReaderExcept env = mapServerFun (handle env)
where
handle :: env -> ServerFun (ReaderT env (ExceptT Text IO)) -> ServerFun IO
handle e f = \req ->
Expand Down

0 comments on commit 6887c95

Please sign in to comment.