Skip to content

Commit 1a17372

Browse files
committed
Add admin port
1 parent df52a02 commit 1a17372

File tree

4 files changed

+33
-19
lines changed

4 files changed

+33
-19
lines changed

src/PostgREST/App.hs

+26-18
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ data RequestContext = RequestContext
9393
, ctxDbStructure :: DbStructure
9494
, ctxApiRequest :: ApiRequest
9595
, ctxPgVersion :: PgVersion
96-
, ctxListenerOn :: Bool
9796
}
9897

9998
type Handler = ExceptT Error
@@ -115,6 +114,10 @@ run installHandlers maybeRunWithSocket appState = do
115114

116115
let app = postgrest configLogLevel appState (connectionWorker appState)
117116

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+
118121
case configServerUnixSocket of
119122
Just socket ->
120123
-- run the postgrest application with user defined socket. Only for UNIX systems
@@ -128,6 +131,9 @@ run installHandlers maybeRunWithSocket appState = do
128131
do
129132
AppState.logWithZTime appState $ "Listening on port " <> show configServerPort
130133
Warp.runSettings (serverSettings conf) app
134+
where
135+
whenJust :: Applicative m => Maybe a -> (a -> m ()) -> m ()
136+
whenJust mg f = maybe (pure ()) f mg
131137

132138
serverSettings :: AppConfig -> Warp.Settings
133139
serverSettings AppConfig{..} =
@@ -136,6 +142,16 @@ serverSettings AppConfig{..} =
136142
& setPort configServerPort
137143
& setServerName ("postgrest/" <> prettyVersion)
138144

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+
139155
-- | PostgREST application
140156
postgrest :: LogLevel -> AppState.AppState -> IO () -> Wai.Application
141157
postgrest logLev appState connWorker =
@@ -146,12 +162,11 @@ postgrest logLev appState connWorker =
146162
maybeDbStructure <- AppState.getDbStructure appState
147163
pgVer <- AppState.getPgVersion appState
148164
jsonDbS <- AppState.getJsonDbS appState
149-
listenerOn <- AppState.getIsListenerOn appState
150165

151166
let
152167
eitherResponse :: IO (Either Error Wai.Response)
153168
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
155170

156171
response <- either Error.errorResponseFor identity <$> eitherResponse
157172
-- Launch the connWorker when the connection is down. The postgrest
@@ -175,10 +190,9 @@ postgrestResponse
175190
-> PgVersion
176191
-> SQL.Pool
177192
-> UTCTime
178-
-> Bool
179193
-> Wai.Request
180194
-> 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
182196
body <- lift $ Wai.strictRequestBody req
183197

184198
dbStructure <-
@@ -197,7 +211,7 @@ postgrestResponse conf maybeDbStructure jsonDbS pgVer pool time listenerOn req =
197211

198212
let
199213
handleReq apiReq =
200-
handleRequest $ RequestContext conf dbStructure apiReq pgVer listenerOn
214+
handleRequest $ RequestContext conf dbStructure apiReq pgVer
201215

202216
runDbHandler pool (txMode apiRequest) jwtClaims (configDbPreparedStatements conf) .
203217
Middleware.optionalRollback conf apiRequest $
@@ -216,7 +230,7 @@ runDbHandler pool mode jwtClaims prepared handler = do
216230
liftEither resp
217231

218232
handleRequest :: RequestContext -> DbHandler Wai.Response
219-
handleRequest context@(RequestContext _ _ ApiRequest{..} _ _) =
233+
handleRequest context@(RequestContext _ _ ApiRequest{..} _) =
220234
case (iAction, iTarget) of
221235
(ActionRead headersOnly, TargetIdent identifier) ->
222236
handleRead headersOnly identifier context
@@ -334,7 +348,7 @@ handleCreate identifier@QualifiedIdentifier{..} context@RequestContext{..} = do
334348
response HTTP.status201 headers mempty
335349

336350
handleUpdate :: QualifiedIdentifier -> RequestContext -> DbHandler Wai.Response
337-
handleUpdate identifier context@(RequestContext _ _ ApiRequest{..} _ _) = do
351+
handleUpdate identifier context@(RequestContext _ _ ApiRequest{..} _) = do
338352
WriteQueryResult{..} <- writeQuery identifier False mempty context
339353

340354
let
@@ -356,7 +370,7 @@ handleUpdate identifier context@(RequestContext _ _ ApiRequest{..} _ _) = do
356370
response status [contentRangeHeader] mempty
357371

358372
handleSingleUpsert :: QualifiedIdentifier -> RequestContext-> DbHandler Wai.Response
359-
handleSingleUpsert identifier context@(RequestContext _ _ ApiRequest{..} _ _) = do
373+
handleSingleUpsert identifier context@(RequestContext _ _ ApiRequest{..} _) = do
360374
when (iTopLevelRange /= RangeQuery.allRange) $
361375
throwError Error.PutRangeNotAllowedError
362376

@@ -380,7 +394,7 @@ handleSingleUpsert identifier context@(RequestContext _ _ ApiRequest{..} _ _) =
380394
response HTTP.status204 [] mempty
381395

382396
handleDelete :: QualifiedIdentifier -> RequestContext -> DbHandler Wai.Response
383-
handleDelete identifier context@(RequestContext _ _ ApiRequest{..} _ _) = do
397+
handleDelete identifier context@(RequestContext _ _ ApiRequest{..} _) = do
384398
WriteQueryResult{..} <- writeQuery identifier False mempty context
385399

386400
let
@@ -463,13 +477,7 @@ handleInvoke invMethod proc context@RequestContext{..} = do
463477
(if invMethod == InvHead then mempty else LBS.fromStrict body)
464478

465479
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
473481
body <-
474482
lift $ case configOpenApiMode of
475483
OAFollowPriv ->
@@ -576,7 +584,7 @@ returnsScalar (TargetProc proc _) = Proc.procReturnsScalar proc
576584
returnsScalar _ = False
577585

578586
readRequest :: Monad m => QualifiedIdentifier -> RequestContext -> Handler m ReadRequest
579-
readRequest QualifiedIdentifier{..} (RequestContext AppConfig{..} dbStructure apiRequest _ _) =
587+
readRequest QualifiedIdentifier{..} (RequestContext AppConfig{..} dbStructure apiRequest _) =
580588
liftEither $
581589
ReqBuilder.readRequest qiSchema qiName configDbMaxRows
582590
(dbRelationships dbStructure)

src/PostgREST/CLI.hs

+3
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ exampleConfigFile =
199199
|## when none is provided, 660 is applied by default
200200
|# server-unix-socket-mode = "660"
201201
|
202+
|## admin server for health checks, it's disabled by default unless a port is specified
203+
|# admin-server-port = 3001
204+
|
202205
|## determine if the OpenAPI output should follow or ignore role privileges or be disabled entirely
203206
|## admitted values: follow-privileges, ignore-privileges, disabled
204207
|openapi-mode = "follow-privileges"

src/PostgREST/Config.hs

+3
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ data AppConfig = AppConfig
9393
, configServerPort :: Int
9494
, configServerUnixSocket :: Maybe FilePath
9595
, configServerUnixSocketMode :: FileMode
96+
, configAdminServerPort :: Maybe Int
9697
}
9798

9899
data LogLevel = LogCrit | LogError | LogWarn | LogInfo
@@ -147,6 +148,7 @@ toText conf =
147148
,("server-port", show . configServerPort)
148149
,("server-unix-socket", q . maybe mempty T.pack . configServerUnixSocket)
149150
,("server-unix-socket-mode", q . T.pack . showSocketMode)
151+
,("admin-server-port", show . configAdminServerPort)
150152
]
151153

152154
-- quote all app.settings
@@ -242,6 +244,7 @@ parser optPath env dbSettings =
242244
<*> (fromMaybe 3000 <$> optInt "server-port")
243245
<*> (fmap T.unpack <$> optString "server-unix-socket")
244246
<*> parseSocketFileMode "server-unix-socket-mode"
247+
<*> optInt "admin-server-port"
245248
where
246249
parseAppSettings :: C.Key -> C.Parser C.Config [(Text, Text)]
247250
parseAppSettings key = addFromEnv . fmap (fmap coerceText) <$> C.subassocs key C.value

src/PostgREST/Request/ApiRequest.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ userApiRequest conf@AppConfig{..} dbStructure req reqBody
195195
, iRange = ranges
196196
, iTopLevelRange = topLevelRange
197197
, iPayload = relevantPayload
198-
, iPreferRepresentation = fromMaybe (if method `elem` ["GET", "HEAD"] then Full else None) preferRepresentation
198+
, iPreferRepresentation = fromMaybe None preferRepresentation
199199
, iPreferParameters = preferParameters
200200
, iPreferCount = preferCount
201201
, iPreferResolution = preferResolution

0 commit comments

Comments
 (0)