aboutsummaryrefslogtreecommitdiff
path: root/lib/Server/Util.hs
blob: 5bfba520adffc90adf8acf29beebbef0d4c5dadb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{-# LANGUAGE FlexibleContexts #-}

-- | mostly the monad the service runs in
module Server.Util (Service, ServiceM, runService, sendErrorMsg) where

import           Control.Monad.Logger (LoggingT, runStderrLoggingT)
import qualified Data.Aeson           as A
import           Data.ByteString      (ByteString)
import           Data.Text            (Text)
import           Servant              (Handler, ServerError, ServerT, err404,
                                       errBody, errHeaders, throwError)

type ServiceM  = LoggingT Handler
type Service api = ServerT api ServiceM

runService :: ServiceM a -> Handler a
runService = runStderrLoggingT

sendErrorMsg :: Text -> ServiceM a
sendErrorMsg msg = throwError err404
  { errBody = A.encode $ A.object ["error" A..= (404 :: Int), "msg" A..= msg] }