Skip to content

Commit

Permalink
fix: in-db config values not loading for pgrst.server_trace_header
Browse files Browse the repository at this point in the history
…and `pgrst.server_cors_allowed_origins`
  • Loading branch information
laurenceisla committed Mar 22, 2024
1 parent f1f01f1 commit f062a2d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
23 changes: 12 additions & 11 deletions src/PostgREST/App.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Some of its functionality includes:
- Producing HTTP Headers according to RFCs.
- Content Negotiation
-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE RecordWildCards #-}
module PostgREST.App
( postgrest
Expand Down Expand Up @@ -47,7 +46,7 @@ import PostgREST.ApiRequest (Action (..), ApiRequest (..),
Mutation (..), Target (..))
import PostgREST.AppState (AppState)
import PostgREST.Auth (AuthResult (..))
import PostgREST.Config (AppConfig (..))
import PostgREST.Config (AppConfig (..), LogLevel (..))
import PostgREST.Config.PgVersion (PgVersion (..))
import PostgREST.Error (Error)
import PostgREST.Observation (Observation (..))
Expand Down Expand Up @@ -79,7 +78,7 @@ run appState observer = do

Admin.runAdmin conf appState (serverSettings conf) observer

let app = postgrest conf appState (AppState.connectionWorker appState) observer
let app = postgrest configLogLevel appState (AppState.connectionWorker appState) observer

case configServerUnixSocket of
Just path -> do
Expand All @@ -98,12 +97,12 @@ serverSettings AppConfig{..} =
& setServerName ("postgrest/" <> prettyVersion)

-- | PostgREST application
postgrest :: AppConfig -> AppState.AppState -> IO () -> (Observation -> IO ()) -> Wai.Application
postgrest conf appState connWorker observer =
traceHeaderMiddleware conf .
Cors.middleware (configServerCorsAllowedOrigins conf) .
postgrest :: LogLevel -> AppState.AppState -> IO () -> (Observation -> IO ()) -> Wai.Application
postgrest logLevel appState connWorker observer =
traceHeaderMiddleware appState .
Cors.middleware appState .
Auth.middleware appState .
Logger.middleware (configLogLevel conf) $
Logger.middleware logLevel $
-- fromJust can be used, because the auth middleware will **always** add
-- some AuthResult to the vault.
\req respond -> case fromJust $ Auth.getResult req of
Expand Down Expand Up @@ -253,9 +252,11 @@ calcTiming timingEnabled f = if timingEnabled
r <- f
pure (Nothing, r)

traceHeaderMiddleware :: AppConfig -> Wai.Middleware
traceHeaderMiddleware AppConfig{configServerTraceHeader} app req respond =
case configServerTraceHeader of
traceHeaderMiddleware :: AppState -> Wai.Middleware
traceHeaderMiddleware appState app req respond = do
conf <- AppState.getConfig appState

case configServerTraceHeader conf of
Nothing -> app req respond
Just hdr ->
let hdrVal = L.lookup hdr $ Wai.requestHeaders req in
Expand Down
1 change: 1 addition & 0 deletions src/PostgREST/Config/Database.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ dbSettingsNames =
,"openapi_security_active"
,"openapi_server_proxy_uri"
,"raw_media_types"
,"server_cors_allowed_origins"
,"server_trace_header"
,"server_timing_enabled"
]
Expand Down
9 changes: 7 additions & 2 deletions src/PostgREST/Cors.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ import qualified Network.Wai.Middleware.Cors as Wai

import Data.List (lookup)

import PostgREST.AppState (AppState, getConfig)
import PostgREST.Config (AppConfig (..))

import Protolude

middleware :: Maybe [Text] -> Wai.Middleware
middleware corsAllowedOrigins = Wai.cors $ corsPolicy corsAllowedOrigins
middleware :: AppState -> Wai.Middleware
middleware appState app req res = do
conf <- getConfig appState
Wai.cors (corsPolicy $ configServerCorsAllowedOrigins conf) app req res

-- | CORS policy to be used in by Wai Cors middleware
corsPolicy :: Maybe [Text] -> Wai.Request -> Maybe Wai.CorsResourcePolicy
Expand Down
4 changes: 2 additions & 2 deletions test/spec/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ main = do
appState <- AppState.initWithPool sockets pool config noObs
AppState.putPgVersion appState actualPgVersion
AppState.putSchemaCache appState (Just baseSchemaCache)
return ((), postgrest config appState (pure ()) noObs)
return ((), postgrest (configLogLevel config) appState (pure ()) noObs)

-- For tests that run with a different SchemaCache(depends on configSchemas)
appDbs config = do
customSchemaCache <- loadSCache pool config
appState <- AppState.initWithPool sockets pool config noObs
AppState.putPgVersion appState actualPgVersion
AppState.putSchemaCache appState (Just customSchemaCache)
return ((), postgrest config appState (pure ()) noObs)
return ((), postgrest (configLogLevel config) appState (pure ()) noObs)

let withApp = app testCfg
maxRowsApp = app testMaxRowsCfg
Expand Down

0 comments on commit f062a2d

Please sign in to comment.