git.iko.soy/typeable/servant
on master help sign in
examples: add GS10
commit616b203792e044595348af971e7469f6abe1a24f authorAlp Mestanogullari <alpmestan@gmail.com> dateSat 9 May 2015, 16:05 (11 years ago) parent77b15e9
4 files changed+118−14
servant-examples/getting-started/GS10.hs+94 servant-examples/getting-started/GS9.hs+15−13 servant-examples/getting-started/getting-started.hs+2 servant-examples/servant-examples.cabal+7−1
servant-examples/getting-started/GS10.hsnew file+94
@@ -0,0 +1,94 @@
1+{-# LANGUAGE DataKinds #-}
2+{-# LANGUAGE TypeFamilies #-}
3+{-# LANGUAGE TypeOperators #-}
4+{-# LANGUAGE FlexibleInstances #-}
5+{-# LANGUAGE OverloadedStrings #-}
6+{-# LANGUAGE MultiParamTypeClasses #-}
7+module GS10 where
8+
9+import Data.ByteString.Lazy (ByteString)
10+import Data.Text.Lazy (pack)
11+import Data.Text.Lazy.Encoding (encodeUtf8)
12+import Network.HTTP.Types
13+import Network.Wai
14+import Servant
15+import Servant.Docs
16+import Servant.Docs.Pandoc (pandoc)
17+import Text.Pandoc.Options (def, WriterOptions(..))
18+import Text.Pandoc.Writers.HTML (writeHtmlString)
19+import qualified GS3
20+
21+type DocsAPI = GS3.API :<|> Raw
22+
23+instance ToCapture (Capture "x" Int) where
24+ toCapture _ = DocCapture "x" "(integer) position on the x axis"
25+
26+instance ToCapture (Capture "y" Int) where
27+ toCapture _ = DocCapture "y" "(integer) position on the y axis"
28+
29+instance ToSample GS3.Position GS3.Position where
30+ toSample _ = Just (GS3.Position 3 14)
31+
32+instance ToParam (QueryParam "name" String) where
33+ toParam _ =
34+ DocQueryParam "name"
35+ ["Alp", "John Doe", "..."]
36+ "Name of the person to say hello to."
37+ Normal
38+
39+instance ToSample GS3.HelloMessage GS3.HelloMessage where
40+ toSamples _ =
41+ [ ("When a value is provided for 'name'", GS3.HelloMessage "Hello, Alp")
42+ , ("When 'name' is not specified", GS3.HelloMessage "Hello, anonymous coward")
43+ ]
44+
45+ci :: GS3.ClientInfo
46+ci = GS3.ClientInfo "Alp" "alp@foo.com" 26 ["haskell", "mathematics"]
47+
48+instance ToSample GS3.ClientInfo GS3.ClientInfo where
49+ toSample _ = Just ci
50+
51+instance ToSample GS3.Email GS3.Email where
52+ toSample _ = Just (GS3.emailForClient ci)
53+
54+api :: Proxy DocsAPI
55+api = Proxy
56+
57+docsBS :: ByteString
58+docsBS = encodeUtf8
59+ . pack
60+ . writeHtmlString opts
61+ . pandoc
62+ $ docsWithIntros [intro] GS3.api
63+
64+ where opts = def { writerHtml5 = True
65+ , writerTableOfContents = True
66+ , writerHighlight = True
67+ , writerStandalone = True
68+ , writerTemplate =
69+ concat
70+ [ "<!DOCTYPE html><html>"
71+ , "<head>"
72+ , "<meta charset=\"UTF-8\">"
73+ , "<title>API Docs - $title$</title>"
74+ , "</head>"
75+ , "<body>"
76+ , "$toc$"
77+ , "<hr />"
78+ , "$body$"
79+ , "</body>"
80+ , "</html>"
81+ ]
82+ }
83+ intro = DocIntro "Welcome" ["This is our super webservice's API.", "Enjoy!"]
84+
85+server :: Server DocsAPI
86+server = GS3.server :<|> serveDocs
87+
88+ where serveDocs _ respond =
89+ respond $ responseLBS ok200 [html] docsBS
90+
91+ html = ("Content-Type", "text/html")
92+
93+app :: Application
94+app = serve api server
servant-examples/getting-started/GS9.hs+15−13
@@ -2,20 +2,22 @@
22{-# LANGUAGE TypeFamilies #-}
33{-# LANGUAGE DeriveGeneric #-}
44{-# LANGUAGE TypeOperators #-}
5+{-# LANGUAGE OverloadedStrings #-}
56module GS9 where
67
78import Control.Applicative
89import Control.Monad.IO.Class
910import Data.Aeson
10−import Data.Char
11−import Data.List
11+import Data.Text (Text)
1212import GHC.Generics
13−import qualified Language.Javascript.JQuery as JQ
1413import Math.Probable
1514import Network.Wai
1615import Servant
1716import Servant.JQuery
1817
18+import qualified Data.Text as T
19+import qualified Language.Javascript.JQuery as JQ
20+
1921data Point = Point
2022 { x :: Double
2123 , y :: Double
@@ -29,24 +31,24 @@ randomPoint = liftIO . mwc $ Point <$> d <*> d
2931 where d = doubleIn (-1, 1)
3032
3133data Search a = Search
32− { query :: String
34+ { query :: Text
3335 , results :: [a]
3436 } deriving Generic
3537
36−mkSearch :: String -> [a] -> Search a
38+mkSearch :: Text -> [a] -> Search a
3739mkSearch = Search
3840
3941instance ToJSON a => ToJSON (Search a)
4042
4143data Book = Book
42− { author :: String
43− , title :: String
44+ { author :: Text
45+ , title :: Text
4446 , year :: Int
4547 } deriving Generic
4648
4749instance ToJSON Book
4850
49−book :: String -> String -> Int -> Book
51+book :: Text -> Text -> Int -> Book
5052book = Book
5153
5254books :: [Book]
@@ -59,18 +61,18 @@ books =
5961 , book "Richard Bird" "Introduction to Functional Programming using Haskell" 1998
6062 ]
6163
62−searchBook :: Monad m => Maybe String -> m (Search Book)
64+searchBook :: Monad m => Maybe Text -> m (Search Book)
6365searchBook Nothing = return (mkSearch "" books)
6466searchBook (Just q) = return (mkSearch q books')
6567
66− where books' = filter (\b -> q' `isInfixOf` map toLower (author b)
67− || q' `isInfixOf` map toLower (title b)
68+ where books' = filter (\b -> q' `T.isInfixOf` T.toLower (author b)
69+ || q' `T.isInfixOf` T.toLower (title b)
6870 )
6971 books
70− q' = map toLower q
72+ q' = T.toLower q
7173
7274type API = "point" :> Get '[JSON] Point
73− :<|> "books" :> QueryParam "q" String :> Get '[JSON] (Search Book)
75+ :<|> "books" :> QueryParam "q" Text :> Get '[JSON] (Search Book)
7476
7577type API' = API :<|> Raw
7678
servant-examples/getting-started/getting-started.hs+2
@@ -10,6 +10,7 @@ import qualified GS5
1010import qualified GS6
1111import qualified GS7
1212import qualified GS9
13+import qualified GS10
1314
1415app :: String -> (Application -> IO ()) -> IO ()
1516app n f = case n of
@@ -22,6 +23,7 @@ app n f = case n of
2223 "7" -> f GS7.app
2324 "8" -> f GS3.app
2425 "9" -> GS9.writeJSFiles >> f GS9.app
26+ "10" -> f GS10.app
2527 _ -> usage
2628
2729main :: IO ()
servant-examples/servant-examples.cabal+7−1
@@ -15,19 +15,25 @@ cabal-version: >=1.10
1515
1616executable getting-started
1717 main-is: getting-started.hs
18− other-modules: GS1, GS2, GS3, GS4, GS5, GS6, GS7, GS8, GS9
18+ other-modules: GS1, GS2, GS3, GS4, GS5, GS6, GS7, GS8, GS9, GS10
1919 build-depends:
2020 aeson >= 0.8
2121 , base >= 4.7
22+ , bytestring
2223 , directory
2324 , either
25+ , http-types
2426 , js-jquery
2527 , lucid
28+ , pandoc
2629 , probable
2730 , servant
31+ , servant-docs
2832 , servant-jquery
2933 , servant-lucid
34+ , servant-pandoc >= 0.2
3035 , servant-server
36+ , text
3137 , time
3238 , transformers
3339 , wai