Beginning cleanup

This commit is contained in:
James Eversole
2024-10-12 18:08:29 -05:00
parent 960bb0c1a8
commit 303c923552
3 changed files with 21 additions and 19 deletions

View File

@@ -17,6 +17,19 @@ import GHC.Generics (Generic)
import Servant hiding ((:>), throwError)
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
--
@@ -67,26 +80,15 @@ runLoggerIO = interpret $ \_ -> \case
log :: (IOE :> es, Error ServerError :> es) => String -> Eff es ()
log msg = adapt $ putStrLn msg
-- Utility
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
-- Log IOExceptions to stdout
liftIO $ putStrLn $ "ERROR Exception: " ++ show e
-- Throw a custom Servant ServerError
throwError $ ServerError
{ errHTTPCode = 500
, errReasonPhrase = "Internal Server Error"
, errBody = "This incident will be investigated."
, 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