Beginning cleanup
This commit is contained in:
parent
960bb0c1a8
commit
303c923552
30
src/Core.hs
30
src/Core.hs
@ -17,6 +17,19 @@ import GHC.Generics (Generic)
|
|||||||
import Servant hiding ((:>), throwError)
|
import Servant hiding ((:>), throwError)
|
||||||
import qualified Servant as S
|
import qualified Servant as S
|
||||||
|
|
||||||
|
--
|
||||||
|
-- Routes
|
||||||
|
--
|
||||||
|
type Root = Get '[PlainText] T.Text
|
||||||
|
type UserList = "user" S.:> Get '[JSON] [User]
|
||||||
|
type UserGet = "user" S.:> Capture "userId" Int S.:> Get '[JSON] User
|
||||||
|
type UserPost = "user" S.:> ReqBody '[PlainText] String S.:> PostCreated '[PlainText] NoContent
|
||||||
|
|
||||||
|
type AppAPI = Root
|
||||||
|
:<|> UserList
|
||||||
|
:<|> UserGet
|
||||||
|
:<|> UserPost
|
||||||
|
|
||||||
--
|
--
|
||||||
-- Core data
|
-- Core data
|
||||||
--
|
--
|
||||||
@ -67,26 +80,15 @@ runLoggerIO = interpret $ \_ -> \case
|
|||||||
log :: (IOE :> es, Error ServerError :> es) => String -> Eff es ()
|
log :: (IOE :> es, Error ServerError :> es) => String -> Eff es ()
|
||||||
log msg = adapt $ putStrLn msg
|
log msg = adapt $ putStrLn msg
|
||||||
|
|
||||||
-- Utility
|
|
||||||
adapt :: (IOE :> es, Error ServerError :> es) => IO a -> Eff es a
|
adapt :: (IOE :> es, Error ServerError :> es) => IO a -> Eff es a
|
||||||
|
-- Lift IO into Eff and catch IOExceptions
|
||||||
adapt m = liftIO m `catch` \(e::IOException) -> do
|
adapt m = liftIO m `catch` \(e::IOException) -> do
|
||||||
|
-- Log IOExceptions to stdout
|
||||||
liftIO $ putStrLn $ "ERROR Exception: " ++ show e
|
liftIO $ putStrLn $ "ERROR Exception: " ++ show e
|
||||||
|
-- Throw a custom Servant ServerError
|
||||||
throwError $ ServerError
|
throwError $ ServerError
|
||||||
{ errHTTPCode = 500
|
{ errHTTPCode = 500
|
||||||
, errReasonPhrase = "Internal Server Error"
|
, errReasonPhrase = "Internal Server Error"
|
||||||
, errBody = "This incident will be investigated."
|
, errBody = "This incident will be investigated."
|
||||||
, errHeaders = []
|
, errHeaders = []
|
||||||
}
|
}
|
||||||
|
|
||||||
--
|
|
||||||
-- Routes
|
|
||||||
--
|
|
||||||
type Root = Get '[PlainText] T.Text
|
|
||||||
type UserList = "user" S.:> Get '[JSON] [User]
|
|
||||||
type UserGet = "user" S.:> Capture "userId" Int S.:> Get '[JSON] User
|
|
||||||
type UserPost = "user" S.:> ReqBody '[PlainText] String S.:> PostCreated '[PlainText] NoContent
|
|
||||||
|
|
||||||
type API = Root
|
|
||||||
:<|> UserList
|
|
||||||
:<|> UserGet
|
|
||||||
:<|> UserPost
|
|
||||||
|
@ -17,10 +17,10 @@ runDatabaseDebug = interpret $ \_ -> \case
|
|||||||
DatabaseWrite (statement, values) -> write statement values
|
DatabaseWrite (statement, values) -> write statement values
|
||||||
where
|
where
|
||||||
read _ values =
|
read _ values =
|
||||||
writeLog "We just mocked a READ database operation" >>= \_ -> pure $
|
writeLog "Mocked a READ database operation" >>= \_ -> pure $
|
||||||
Just (User values "Mock User")
|
Just (User values "Mock User")
|
||||||
write _ values =
|
write _ values =
|
||||||
writeLog $ "We just mocked a WRITE database operation with a user named "
|
writeLog $ "Mocked a WRITE database operation with a user named "
|
||||||
++ values
|
++ values
|
||||||
|
|
||||||
runDatabaseIO :: DatabaseEffects es => Eff (Database : es) a -> Eff es a
|
runDatabaseIO :: DatabaseEffects es => Eff (Database : es) a -> Eff es a
|
||||||
|
@ -19,13 +19,13 @@ import qualified Servant as S
|
|||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = run port $ serve proxy app
|
main = run port $ serve proxy app
|
||||||
|
|
||||||
app :: Server API
|
app :: Server AppAPI
|
||||||
app = α $ rootHandler
|
app = α $ rootHandler
|
||||||
:<|> userListHandler
|
:<|> userListHandler
|
||||||
:<|> userGetHandler
|
:<|> userGetHandler
|
||||||
:<|> userPostHandler
|
:<|> userPostHandler
|
||||||
|
|
||||||
α :: ServerT API AppEff -> ServerT API Handler
|
α :: ServerT AppAPI AppEff -> ServerT AppAPI Handler
|
||||||
α = hoistServer proxy
|
α = hoistServer proxy
|
||||||
$ Handler
|
$ Handler
|
||||||
. ExceptT
|
. ExceptT
|
||||||
@ -38,5 +38,5 @@ app = α $ rootHandler
|
|||||||
port :: Int
|
port :: Int
|
||||||
port = 8080
|
port = 8080
|
||||||
|
|
||||||
proxy :: Proxy API
|
proxy :: Proxy AppAPI
|
||||||
proxy = Proxy
|
proxy = Proxy
|
||||||
|
Loading…
x
Reference in New Issue
Block a user