diff --git a/servant-examples/getting-started/GS5.hs b/servant-examples/getting-started/GS5.hs new file mode 100644 index 00000000..6a6c91b7 --- /dev/null +++ b/servant-examples/getting-started/GS5.hs @@ -0,0 +1,37 @@ +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE TypeFamilies #-} +{-# LANGUAGE DeriveGeneric #-} +{-# LANGUAGE TypeOperators #-} +{-# LANGUAGE OverloadedStrings #-} +module GS5 where + +import Control.Monad.IO.Class +import Control.Monad.Trans.Either +import Data.Aeson +import GHC.Generics +import Network.Wai +import Servant +import System.Directory + +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 + else left custom404Err + + where custom404Err = err404 { errBody = "myfile.txt just isn't there, please leave this server alone." } + +app :: Application +app = serve ioAPI server diff --git a/servant-examples/getting-started/getting-started.hs b/servant-examples/getting-started/getting-started.hs index f3a0ace1..f32ee674 100644 --- a/servant-examples/getting-started/getting-started.hs +++ b/servant-examples/getting-started/getting-started.hs @@ -6,6 +6,7 @@ import qualified GS1 import qualified GS2 import qualified GS3 import qualified GS4 +import qualified GS5 app :: String -> Maybe Application app n = case n of @@ -13,6 +14,7 @@ app n = case n of "2" -> Just GS2.app "3" -> Just GS3.app "4" -> Just GS4.app + "5" -> Just GS5.app _ -> Nothing main :: IO () diff --git a/servant-examples/servant-examples.cabal b/servant-examples/servant-examples.cabal index 2e7af148..ef2f8cc3 100644 --- a/servant-examples/servant-examples.cabal +++ b/servant-examples/servant-examples.cabal @@ -19,12 +19,14 @@ executable getting-started build-depends: aeson >= 0.8 , base >= 4.7 + , directory , either , lucid , servant , servant-lucid , servant-server , time + , transformers , wai , warp hs-source-dirs: getting-started