@@ -0,0 +1,37 @@
1+{-# LANGUAGE DataKinds #-}
2+{-# LANGUAGE TypeFamilies #-}
3+{-# LANGUAGE DeriveGeneric #-}
4+{-# LANGUAGE TypeOperators #-}
5+{-# LANGUAGE OverloadedStrings #-}
6+module GS5 where
7+
8+import Control.Monad.IO.Class
9+import Control.Monad.Trans.Either
10+import Data.Aeson
11+import GHC.Generics
12+import Network.Wai
13+import Servant
14+import System.Directory
15+
16+type IOAPI = "myfile.txt" :> Get '[JSON] FileContent
17+
18+ioAPI :: Proxy IOAPI
19+ioAPI = Proxy
20+
21+newtype FileContent = FileContent
22+ { content :: String }
23+ deriving Generic
24+
25+instance ToJSON FileContent
26+
27+server :: Server IOAPI
28+server = do
29+ exists <- liftIO (doesFileExist "myfile.txt")
30+ if exists
31+ then liftIO (readFile "myfile.txt") >>= return . FileContent
32+ else left custom404Err
33+
34+ where custom404Err = err404 { errBody = "myfile.txt just isn't there, please leave this server alone." }
35+
36+app :: Application
37+app = serve ioAPI server