git.iko.soy/typeable/servant
on master help sign in
examples: add GS5
1d3eefaAlp Mestanogullari
servant-examples/getting-started/GS5.hsnew file+37
@@ -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
servant-examples/getting-started/getting-started.hs+2
@@ -6,6 +6,7 @@ import qualified GS1
6import qualified GS2
6import qualified GS2
7import qualified GS3
7import qualified GS3
8import qualified GS4
8import qualified GS4
9+import qualified GS5
9
10
10app :: String -> Maybe Application
11app :: String -> Maybe Application
11app n = case n of
12app n = case n of
@@ -13,6 +14,7 @@ app n = case n of
13 "2" -> Just GS2.app
14 "2" -> Just GS2.app
14 "3" -> Just GS3.app
15 "3" -> Just GS3.app
15 "4" -> Just GS4.app
16 "4" -> Just GS4.app
17+ "5" -> Just GS5.app
16 _ -> Nothing
18 _ -> Nothing
17
19
18main :: IO ()
20main :: IO ()
servant-examples/servant-examples.cabal+2
@@ -19,12 +19,14 @@ executable getting-started
19 build-depends:
19 build-depends:
20 aeson >= 0.8
20 aeson >= 0.8
21 , base >= 4.7
21 , base >= 4.7
22+ , directory
22 , either
23 , either
23 , lucid
24 , lucid
24 , servant
25 , servant
25 , servant-lucid
26 , servant-lucid
26 , servant-server
27 , servant-server
27 , time
28 , time
29+ , transformers
28 , wai
30 , wai
29 , warp
31 , warp
30 hs-source-dirs: getting-started
32 hs-source-dirs: getting-started