-
Notifications
You must be signed in to change notification settings - Fork 34
/
Main.hs
213 lines (184 loc) · 6.2 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Main (main, catIsMale, catName) where
import Prelude ()
import Prelude.Compat
import Control.Lens hiding ((.=))
import Data.Aeson (FromJSON, ToJSON, Value)
import Data.Maybe (fromMaybe)
import Data.String (IsString (..))
import Data.Text (Text)
import GHC.Generics (Generic)
import Network.Wai (Application)
import System.Environment (getArgs, lookupEnv)
import Text.Read (readMaybe)
import Data.Swagger
import Servant
import Servant.Swagger
import Servant.Swagger.UI
import Servant.Swagger.UI.Core
import Servant.Swagger.UI.JensOleG
import Servant.Swagger.UI.ReDoc
import qualified Network.Wai.Handler.Warp as Warp
#if MIN_VERSION_servant(0,12,0)
#define SUMMARY(d) Summary d :>
#else
#define SUMMARY(d)
#endif
#if MIN_VERSION_servant(0,13,0)
#if __GLASGOW_HASKELL__ >= 802 && MIN_VERSION_base(4,10,0)
import GHC.Generics (D1, Meta (..), Rep)
import GHC.TypeLits (AppendSymbol, Symbol)
#endif
#endif
-- data types
data Cat = Cat { catName :: CatName, catIsMale :: Bool }
deriving (Generic, Show)
newtype CatName = CatName Text
deriving ( Eq, Show, Generic
#if MIN_VERSION_servant(0,5,0)
, FromHttpApiData
#else
, FromText
#endif
)
instance IsString CatName where
fromString = CatName . fromString
-- swagger instances
instance ToJSON Cat
instance ToJSON CatName
instance FromJSON Cat
instance FromJSON CatName
instance ToParamSchema CatName
instance ToSchema Cat
instance ToSchema CatName
-- api
#if MIN_VERSION_servant(0,13,0)
#if __GLASGOW_HASKELL__ >= 802 && MIN_VERSION_base(4,10,0)
-- | Get a typename as type-level 'Symbol'.
--
-- Few non-Generic things are hard-coded, for else we fallback to 'Generic'
--
-- >>> :kind! TypeName Int
-- TypeName Int :: Symbol
-- = "Int"
--
-- >>> :kind! TypeName CatName
-- TypeName CatName :: Symbol
-- = "CatName"
--
-- Unfortunately we cannot use 'TypeError' for other cases,
-- as 'Rep' isn't reduced so 'GenericTypeName' cannot fall past first
-- equation. So you will get somewhat obscure errors saying
-- "no instance for (KnownSymbol ... (GenericTypeName YourNonGenericType ..."
--
-- >>> :kind! TypeName Float
-- TypeName Float :: Symbol
-- = GenericTypeName Float (Rep Float ())
--
-- It would be nice if there were such type family for 'Typeable', i.e. all
-- types :)
--
type family TypeName (x :: *) :: Symbol where
TypeName Int = "Int"
TypeName Text = "Text"
TypeName x = GenericTypeName x (Rep x ())
type family GenericTypeName t (r :: *) :: Symbol where
GenericTypeName t (D1 ('MetaData name mod pkg nt) f x) = name
type Desc t n = Description (AppendSymbol (TypeName t) (AppendSymbol " | " n))
#else
type Desc t n = Description n
#endif
#endif
type FirstCatEndpoint =
#if MIN_VERSION_servant(0,13,0)
"cat"
:> Summary "First cat endpoint"
:> Capture' '[Desc CatName "Cat's name"] ":name" CatName
:> QueryParam' '[Required, Desc Int "Random number"] "num" Int
:> QueryParam' '[Optional, Desc Text "Random text"] "text" Text
:> Get '[JSON] Cat
#else
"cat" :> Capture ":name" CatName
:> QueryParam "num" Int :> QueryParam "text" Text :> Get '[JSON] Cat
#endif
type BasicAPI = Get '[PlainText, JSON] Text
:<|> FirstCatEndpoint
:<|> SUMMARY("Second cat") "cat2" :> Capture ":name" CatName :> Get '[JSON] Cat
:<|> SUMMARY("Third cat") "cat3" :> Capture ":name" CatName :> Get '[JSON] Cat
:<|> SUMMARY("Post endpoint") "post-cat" :> ReqBody '[JSON] Cat :> Post '[JSON] Cat
type API =
-- this serves both: swagger.json and swagger-ui
SwaggerSchemaUI "swagger-ui" "swagger.json"
:<|> BasicAPI
-- To test nested case
type API' = API
:<|> "nested" :> API
:<|> SwaggerSchemaUI' "foo-ui" ("foo" :> "swagger.json" :> Get '[JSON] Value)
-- Implementation
-- | We test different ways to nest API, so we have an enumeration
data Variant
= Normal
| Nested
| SpecDown
deriving (Eq)
data UIFlavour
= Original
| JensOleG
| ReDoc
deriving (Eq)
server' :: UIFlavour -> Server API'
server' uiFlavour = server Normal
:<|> server Nested
:<|> schemaUiServer (swaggerDoc' SpecDown)
where
server :: Variant -> Server API
server variant =
schemaUiServer (swaggerDoc' variant)
:<|> (return "Hello World" :<|> catEndpoint' :<|> catEndpoint :<|> catEndpoint :<|> return)
where
catEndpoint' n _ _ = return $ Cat n (variant == Normal)
catEndpoint n = return $ Cat n (variant == Normal)
-- Unfortunately we have to specify the basePath manually atm.
schemaUiServer
:: (Server api ~ Handler Value)
=> Swagger -> Server (SwaggerSchemaUI' dir api)
schemaUiServer = case uiFlavour of
Original -> swaggerSchemaUIServer
JensOleG -> jensolegSwaggerSchemaUIServer
ReDoc -> redocSchemaUIServer
swaggerDoc' Normal = swaggerDoc
swaggerDoc' Nested = swaggerDoc
& basePath ?~ "/nested"
& info.description ?~ "Nested API"
swaggerDoc' SpecDown = swaggerDoc
& info.description ?~ "Spec nested"
-- Boilerplate
swaggerDoc :: Swagger
swaggerDoc = toSwagger (Proxy :: Proxy BasicAPI)
& info.title .~ "Cats API"
& info.version .~ "2016.8.7"
& info.description ?~ "This is an API that tests servant-swagger support"
api :: Proxy API'
api = Proxy
app :: UIFlavour -> Application
app = serve api . server'
main :: IO ()
main = do
args <- getArgs
let uiFlavour | "jensoleg" `elem` args = JensOleG
| "redoc" `elem` args = ReDoc
| otherwise = Original
p <- fromMaybe 8000 . (>>= readMaybe) <$> lookupEnv "PORT"
putStrLn $ "http://localhost:" ++ show p ++ "/"
Warp.run p (app uiFlavour)