2015-08-17 23:56:29 +02:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
2015-05-06 15:07:49 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2015-08-17 23:56:29 +02:00
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
|
|
|
{-# LANGUAGE TypeOperators #-}
|
2015-05-10 13:39:18 +02:00
|
|
|
module T5 where
|
2015-05-06 15:07:49 +02:00
|
|
|
|
2015-08-17 23:56:29 +02:00
|
|
|
import Control.Monad.IO.Class
|
2015-09-12 14:11:24 +02:00
|
|
|
import Control.Monad.Trans.Except
|
2015-08-17 23:56:29 +02:00
|
|
|
import Data.Aeson
|
|
|
|
import GHC.Generics
|
|
|
|
import Network.Wai
|
|
|
|
import Servant
|
|
|
|
import System.Directory
|
2015-05-06 15:07:49 +02:00
|
|
|
|
|
|
|
type IOAPI = "myfile.txt" :> Get '[JSON] FileContent
|
|
|
|
|
|
|
|
ioAPI :: Proxy IOAPI
|
|
|
|
ioAPI = Proxy
|
|
|
|
|
|
|
|
newtype FileContent = FileContent
|
|
|
|
{ content :: String }
|
|
|
|
deriving Generic
|
|
|
|
|
|
|
|
instance ToJSON FileContent
|
|
|
|
|
|
|
|
server :: Server IOAPI
|
|
|
|
server = do
|
|
|
|
exists <- liftIO (doesFileExist "myfile.txt")
|
|
|
|
if exists
|
|
|
|
then liftIO (readFile "myfile.txt") >>= return . FileContent
|
2015-09-12 14:11:24 +02:00
|
|
|
else throwE custom404Err
|
2015-05-06 15:07:49 +02:00
|
|
|
|
|
|
|
where custom404Err = err404 { errBody = "myfile.txt just isn't there, please leave this server alone." }
|
|
|
|
|
|
|
|
app :: Application
|
2016-01-07 12:51:30 +01:00
|
|
|
app = serve ioAPI EmptyConfig server
|