git.iko.soy/typeable/servant
on feef8ca help sign in
37 lines 962 B history raw
1{-# LANGUAGE DataKinds #-}
2{-# LANGUAGE DeriveGeneric #-}
3{-# LANGUAGE OverloadedStrings #-}
4{-# LANGUAGE TypeFamilies #-}
5{-# LANGUAGE TypeOperators #-}
6module T5 where
8import Control.Monad.IO.Class
9import Control.Monad.Trans.Except
10import Data.Aeson
11import GHC.Generics
12import Network.Wai
13import Servant
14import System.Directory
16type IOAPI = "myfile.txt" :> Get '[JSON] FileContent
18ioAPI :: Proxy IOAPI
19ioAPI = Proxy
21newtype FileContent = FileContent
22 { content :: String }
23 deriving Generic
25instance ToJSON FileContent
27server :: Server IOAPI
28server = do
29 exists <- liftIO (doesFileExist "myfile.txt")
30 if exists
31 then liftIO (readFile "myfile.txt") >>= return . FileContent
32 else throwE custom404Err
34 where custom404Err = err404 { errBody = "myfile.txt just isn't there, please leave this server alone." }
36app :: Application
37app = serve ioAPI server