examples: add GS5
This commit is contained in:
parent
e81ac8fbc5
commit
1d3eefaf48
3 changed files with 41 additions and 0 deletions
37
servant-examples/getting-started/GS5.hs
Normal file
37
servant-examples/getting-started/GS5.hs
Normal file
|
@ -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
|
|
@ -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 ()
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue