Beginning cleanup
This commit is contained in:
30
src/Core.hs
30
src/Core.hs
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user