@@ -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