git.iko.soy/typeable/servant
on master help sign in
examples: add GS5
commit1d3eefaf48dea6e3d43057956a90c5e9cf2c239e authorAlp Mestanogullari <alpmestan@gmail.com> dateWed 6 May 2015, 15:07 (11 years ago) parente81ac8f
3 files changed+41
servant-examples/getting-started/GS5.hs+37 servant-examples/getting-started/getting-started.hs+2 servant-examples/servant-examples.cabal+2
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
66import qualified GS2
77import qualified GS3
88import qualified GS4
9+import qualified GS5
910
1011app :: String -> Maybe Application
1112app n = case n of
@@ -13,6 +14,7 @@ app n = case n of
1314 "2" -> Just GS2.app
1415 "3" -> Just GS3.app
1516 "4" -> Just GS4.app
17+ "5" -> Just GS5.app
1618 _ -> Nothing
1719
1820main :: IO ()
servant-examples/servant-examples.cabal+2
@@ -19,12 +19,14 @@ executable getting-started
1919 build-depends:
2020 aeson >= 0.8
2121 , base >= 4.7
22+ , directory
2223 , either
2324 , lucid
2425 , servant
2526 , servant-lucid
2627 , servant-server
2728 , time
29+ , transformers
2830 , wai
2931 , warp
3032 hs-source-dirs: getting-started