@@ -93,7 +93,6 @@ data RequestContext = RequestContext
93
93
, ctxDbStructure :: DbStructure
94
94
, ctxApiRequest :: ApiRequest
95
95
, ctxPgVersion :: PgVersion
96
- , ctxListenerOn :: Bool
97
96
}
98
97
99
98
type Handler = ExceptT Error
@@ -115,6 +114,10 @@ run installHandlers maybeRunWithSocket appState = do
115
114
116
115
let app = postgrest configLogLevel appState (connectionWorker appState)
117
116
117
+ whenJust configAdminServerPort $ \ adminPort -> do
118
+ AppState. logWithZTime appState $ " Admin server listening on port " <> show adminPort
119
+ void . forkIO $ Warp. runSettings (serverSettings conf & setPort adminPort) $ adminApp appState
120
+
118
121
case configServerUnixSocket of
119
122
Just socket ->
120
123
-- run the postgrest application with user defined socket. Only for UNIX systems
@@ -128,6 +131,9 @@ run installHandlers maybeRunWithSocket appState = do
128
131
do
129
132
AppState. logWithZTime appState $ " Listening on port " <> show configServerPort
130
133
Warp. runSettings (serverSettings conf) app
134
+ where
135
+ whenJust :: Applicative m => Maybe a -> (a -> m () ) -> m ()
136
+ whenJust mg f = maybe (pure () ) f mg
131
137
132
138
serverSettings :: AppConfig -> Warp. Settings
133
139
serverSettings AppConfig {.. } =
@@ -136,6 +142,16 @@ serverSettings AppConfig{..} =
136
142
& setPort configServerPort
137
143
& setServerName (" postgrest/" <> prettyVersion)
138
144
145
+ adminApp :: AppState. AppState -> Wai. Application
146
+ adminApp appState req respond =
147
+ case Wai. pathInfo req of
148
+ [] -> do
149
+ listenerOn <- AppState. getIsListenerOn appState
150
+ if listenerOn
151
+ then respond $ Wai. responseLBS HTTP. status200 [] mempty
152
+ else respond $ Wai. responseLBS HTTP. status503 [] mempty
153
+ _ -> respond $ Wai. responseLBS HTTP. status404 [] mempty
154
+
139
155
-- | PostgREST application
140
156
postgrest :: LogLevel -> AppState. AppState -> IO () -> Wai. Application
141
157
postgrest logLev appState connWorker =
@@ -146,12 +162,11 @@ postgrest logLev appState connWorker =
146
162
maybeDbStructure <- AppState. getDbStructure appState
147
163
pgVer <- AppState. getPgVersion appState
148
164
jsonDbS <- AppState. getJsonDbS appState
149
- listenerOn <- AppState. getIsListenerOn appState
150
165
151
166
let
152
167
eitherResponse :: IO (Either Error Wai. Response )
153
168
eitherResponse =
154
- runExceptT $ postgrestResponse conf maybeDbStructure jsonDbS pgVer (AppState. getPool appState) time listenerOn req
169
+ runExceptT $ postgrestResponse conf maybeDbStructure jsonDbS pgVer (AppState. getPool appState) time req
155
170
156
171
response <- either Error. errorResponseFor identity <$> eitherResponse
157
172
-- Launch the connWorker when the connection is down. The postgrest
@@ -175,10 +190,9 @@ postgrestResponse
175
190
-> PgVersion
176
191
-> SQL. Pool
177
192
-> UTCTime
178
- -> Bool
179
193
-> Wai. Request
180
194
-> Handler IO Wai. Response
181
- postgrestResponse conf maybeDbStructure jsonDbS pgVer pool time listenerOn req = do
195
+ postgrestResponse conf maybeDbStructure jsonDbS pgVer pool time req = do
182
196
body <- lift $ Wai. strictRequestBody req
183
197
184
198
dbStructure <-
@@ -197,7 +211,7 @@ postgrestResponse conf maybeDbStructure jsonDbS pgVer pool time listenerOn req =
197
211
198
212
let
199
213
handleReq apiReq =
200
- handleRequest $ RequestContext conf dbStructure apiReq pgVer listenerOn
214
+ handleRequest $ RequestContext conf dbStructure apiReq pgVer
201
215
202
216
runDbHandler pool (txMode apiRequest) jwtClaims (configDbPreparedStatements conf) .
203
217
Middleware. optionalRollback conf apiRequest $
@@ -216,7 +230,7 @@ runDbHandler pool mode jwtClaims prepared handler = do
216
230
liftEither resp
217
231
218
232
handleRequest :: RequestContext -> DbHandler Wai. Response
219
- handleRequest context@ (RequestContext _ _ ApiRequest {.. } _ _ ) =
233
+ handleRequest context@ (RequestContext _ _ ApiRequest {.. } _) =
220
234
case (iAction, iTarget) of
221
235
(ActionRead headersOnly, TargetIdent identifier) ->
222
236
handleRead headersOnly identifier context
@@ -334,7 +348,7 @@ handleCreate identifier@QualifiedIdentifier{..} context@RequestContext{..} = do
334
348
response HTTP. status201 headers mempty
335
349
336
350
handleUpdate :: QualifiedIdentifier -> RequestContext -> DbHandler Wai. Response
337
- handleUpdate identifier context@ (RequestContext _ _ ApiRequest {.. } _ _ ) = do
351
+ handleUpdate identifier context@ (RequestContext _ _ ApiRequest {.. } _) = do
338
352
WriteQueryResult {.. } <- writeQuery identifier False mempty context
339
353
340
354
let
@@ -356,7 +370,7 @@ handleUpdate identifier context@(RequestContext _ _ ApiRequest{..} _ _) = do
356
370
response status [contentRangeHeader] mempty
357
371
358
372
handleSingleUpsert :: QualifiedIdentifier -> RequestContext -> DbHandler Wai. Response
359
- handleSingleUpsert identifier context@ (RequestContext _ _ ApiRequest {.. } _ _ ) = do
373
+ handleSingleUpsert identifier context@ (RequestContext _ _ ApiRequest {.. } _) = do
360
374
when (iTopLevelRange /= RangeQuery. allRange) $
361
375
throwError Error. PutRangeNotAllowedError
362
376
@@ -380,7 +394,7 @@ handleSingleUpsert identifier context@(RequestContext _ _ ApiRequest{..} _ _) =
380
394
response HTTP. status204 [] mempty
381
395
382
396
handleDelete :: QualifiedIdentifier -> RequestContext -> DbHandler Wai. Response
383
- handleDelete identifier context@ (RequestContext _ _ ApiRequest {.. } _ _ ) = do
397
+ handleDelete identifier context@ (RequestContext _ _ ApiRequest {.. } _) = do
384
398
WriteQueryResult {.. } <- writeQuery identifier False mempty context
385
399
386
400
let
@@ -463,13 +477,7 @@ handleInvoke invMethod proc context@RequestContext{..} = do
463
477
(if invMethod == InvHead then mempty else LBS. fromStrict body)
464
478
465
479
handleOpenApi :: Bool -> Schema -> RequestContext -> DbHandler Wai. Response
466
- handleOpenApi _ _ (RequestContext _ _ ApiRequest {iPreferRepresentation = None } _ isListenerOn) =
467
- return $
468
- Wai. responseLBS
469
- (if isListenerOn then HTTP. status200 else HTTP. status503)
470
- mempty
471
- mempty
472
- handleOpenApi headersOnly tSchema (RequestContext conf@ AppConfig {.. } dbStructure apiRequest ctxPgVersion _) = do
480
+ handleOpenApi headersOnly tSchema (RequestContext conf@ AppConfig {.. } dbStructure apiRequest ctxPgVersion) = do
473
481
body <-
474
482
lift $ case configOpenApiMode of
475
483
OAFollowPriv ->
@@ -576,7 +584,7 @@ returnsScalar (TargetProc proc _) = Proc.procReturnsScalar proc
576
584
returnsScalar _ = False
577
585
578
586
readRequest :: Monad m => QualifiedIdentifier -> RequestContext -> Handler m ReadRequest
579
- readRequest QualifiedIdentifier {.. } (RequestContext AppConfig {.. } dbStructure apiRequest _ _ ) =
587
+ readRequest QualifiedIdentifier {.. } (RequestContext AppConfig {.. } dbStructure apiRequest _) =
580
588
liftEither $
581
589
ReqBuilder. readRequest qiSchema qiName configDbMaxRows
582
590
(dbRelationships dbStructure)
0 commit comments