-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathServer.hs
More file actions
200 lines (156 loc) · 5.25 KB
/
Copy pathServer.hs
File metadata and controls
200 lines (156 loc) · 5.25 KB
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
-- Necessary for servant-docs instances
{-# OPTIONS_GHC -Wno-orphans #-}
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
module Server.Server (platformAPI, platformApp)
where
import Protolude (
Int,
Monoid (mempty),
Proxy (Proxy),
($),
)
import Protolude qualified as P
import Data.Aeson (Object, Value, object)
import Data.Aeson.KeyMap qualified as KeyMap
import Data.ByteString.Lazy qualified as BL
import Data.Text (Text)
import Data.Text qualified as T
import Network.Wai (Application)
import Network.Wai.Parse (
defaultParseRequestBodyOptions,
setMaxRequestFilesSize,
setMaxRequestNumFiles,
)
import Servant (Context (EmptyContext, (:.)), NoContent)
import Servant.API (
Capture,
Get,
JSON,
PlainText,
Post,
ReqBody,
(:<|>) ((:<|>)),
(:>),
)
import Servant.Docs (
DocCapture (DocCapture),
ToCapture (toCapture),
ToSample,
singleSample,
toSamples,
)
import Servant.HTML.Blaze (HTML)
import Servant.Multipart (
MultipartData (MultipartData),
MultipartOptions (generalOptions),
Tmp,
ToMultipartSample (toMultipartSamples),
defaultMultipartOptions,
)
import Servant.Server (Server)
import Servant.Server qualified as Servant
import Text.Blaze.Internal (MarkupM)
import AirGQL.Config (Config (maxDbSize), defaultConfig)
import AirGQL.ExternalAppContext (ExternalAppContext)
import AirGQL.Lib (SQLPost, insertOnly, readOnly, writeOnly)
import AirGQL.Servant.Database (
apiDatabaseSchemaGetHandler,
apiDatabaseVacuumPostHandler,
)
import AirGQL.Servant.GraphQL (
gqlQueryGetHandler,
gqlQueryPostHandler,
playgroundDefaultQueryHandler,
)
import AirGQL.Servant.SqlQuery (sqlQueryPostHandler)
import AirGQL.Types.SchemaConf (
SchemaConf (accessMode, pragmaConf),
defaultSchemaConf,
)
import AirGQL.Types.SqlQueryPostResult (SqlQueryPostResult)
import AirGQL.Types.Types (GQLPost)
{- FOURMOLU_DISABLE -}
-- ATTENTION: Order of handlers matters!
type PlatformAPI =
-- gqlQueryGetHandler
-- Redirect to GraphiQL playground
"graphql" :> Get '[HTML] NoContent
-- gqlQueryPostHandler
:<|> "graphql"
:> ReqBody '[JSON] GQLPost
:> Post '[JSON] Object
:<|> "readonly" :> "graphql"
:> ReqBody '[JSON] GQLPost
:> Post '[JSON] Object
:<|> "writeonly" :> "graphql"
:> ReqBody '[JSON] GQLPost
:> Post '[JSON] Object
:<|> "insertonly" :> "graphql"
:> ReqBody '[JSON] GQLPost
:> Post '[JSON] Object
-- playgroundDefaultQueryHandler
:<|> "playground" :> "default-query"
:> Get '[PlainText] Text
-- apiDatabaseSchemaGetHandler
:<|> "schema" :> Get '[PlainText] Text
-- apiDatabaseVacuumPostHandler
:<|> "vacuum" :> Post '[JSON] Object
-- sqlQueryPostHandler
:<|> "sql"
:> ReqBody '[JSON] SQLPost
:> Post '[JSON] SqlQueryPostResult
{- FOURMOLU_ENABLE -}
-- | Instances for automatic documentation generation via servant-docs
instance ToSample (MultipartData Tmp) where
toSamples _ = singleSample $ MultipartData mempty mempty
instance ToMultipartSample Tmp (MultipartData Tmp) where
toMultipartSamples _ = []
instance ToSample Value where
toSamples _ = singleSample $ object []
instance ToSample (KeyMap.KeyMap Value) where
toSamples _ = singleSample $ KeyMap.fromList []
instance ToSample (MarkupM ()) where
toSamples _ = singleSample mempty
instance ToSample BL.ByteString where
toSamples _ = singleSample mempty
instance ToSample Text where
toSamples _ = singleSample mempty
instance ToSample P.ByteString where
toSamples _ = singleSample mempty
instance ToCapture (Capture "readonlyId" Text) where
toCapture _ = DocCapture "readonlyId" "Read-only ID of the database"
instance ToCapture (Capture "dbId" Text) where
toCapture _ = DocCapture "dbId" "ID of the database to be served"
platformAPI :: Proxy PlatformAPI
platformAPI = Proxy
platformServer :: ExternalAppContext -> P.FilePath -> Server PlatformAPI
platformServer ctx filePath = do
-- Single-database server: the file path is fixed at startup, and its own
-- path doubles as the database identifier for schema names / file URLs.
let dbId = T.pack filePath
gqlQueryGetHandler dbId
:<|> gqlQueryPostHandler defaultSchemaConf filePath dbId
:<|> gqlQueryPostHandler defaultSchemaConf{accessMode = readOnly} filePath dbId
:<|> gqlQueryPostHandler defaultSchemaConf{accessMode = writeOnly} filePath dbId
:<|> gqlQueryPostHandler defaultSchemaConf{accessMode = insertOnly} filePath dbId
:<|> playgroundDefaultQueryHandler filePath dbId
:<|> apiDatabaseSchemaGetHandler ctx filePath
:<|> apiDatabaseVacuumPostHandler filePath
:<|> sqlQueryPostHandler defaultSchemaConf.pragmaConf filePath
platformApp :: ExternalAppContext -> P.FilePath -> Application
platformApp ctx filePath = do
let
maxFileSizeInByte :: Int = defaultConfig.maxDbSize
multipartOpts :: MultipartOptions Tmp
multipartOpts =
(defaultMultipartOptions (Proxy :: Proxy Tmp))
{ generalOptions =
setMaxRequestNumFiles 1 $
setMaxRequestFilesSize
(P.fromIntegral maxFileSizeInByte)
defaultParseRequestBodyOptions
}
context :: Context '[MultipartOptions Tmp]
context =
multipartOpts :. EmptyContext
Servant.serveWithContext platformAPI context $ platformServer ctx filePath