@@ -50,6 +50,7 @@ import PostgREST.Auth (AuthResult (..))
50
50
import PostgREST.Config (AppConfig (.. ))
51
51
import PostgREST.Config.PgVersion (PgVersion (.. ))
52
52
import PostgREST.Error (Error )
53
+ import PostgREST.Observation (Observation (.. ))
53
54
import PostgREST.Query (DbHandler )
54
55
import PostgREST.Response.Performance (ServerTiming (.. ),
55
56
serverTimingHeader )
@@ -66,26 +67,26 @@ import System.TimeIt (timeItT)
66
67
67
68
type Handler = ExceptT Error
68
69
69
- run :: AppState -> IO ()
70
- run appState = do
71
- AppState. logWithZTime appState $ " Starting PostgREST " <> T. decodeUtf8 prettyVersion <> " ... "
70
+ run :: AppState -> ( Observation -> IO () ) -> IO ()
71
+ run appState observer = do
72
+ observer $ AppStartObs prettyVersion
72
73
73
74
conf@ AppConfig {.. } <- AppState. getConfig appState
74
75
AppState. connectionWorker appState -- Loads the initial SchemaCache
75
- Unix. installSignalHandlers (AppState. getMainThreadId appState) (AppState. connectionWorker appState) (AppState. reReadConfig False appState)
76
+ Unix. installSignalHandlers (AppState. getMainThreadId appState) (AppState. connectionWorker appState) (AppState. reReadConfig False appState observer )
76
77
-- reload schema cache + config on NOTIFY
77
- AppState. runListener conf appState
78
+ AppState. runListener conf appState observer
78
79
79
- Admin. runAdmin conf appState $ serverSettings conf
80
+ Admin. runAdmin conf appState ( serverSettings conf) observer
80
81
81
- let app = postgrest conf appState (AppState. connectionWorker appState)
82
+ let app = postgrest conf appState (AppState. connectionWorker appState) observer
82
83
83
- what <- case configServerUnixSocket of
84
- Just path -> pure $ " unix socket " <> show path
84
+ case configServerUnixSocket of
85
+ Just path -> do
86
+ observer $ AppServerUnixObs path
85
87
Nothing -> do
86
88
port <- NS. socketPort $ AppState. getSocketREST appState
87
- pure $ " port " <> show port
88
- AppState. logWithZTime appState $ " Listening on " <> what
89
+ observer $ AppServerPortObs port
89
90
90
91
Warp. runSettingsSocket (serverSettings conf) (AppState. getSocketREST appState) app
91
92
@@ -97,8 +98,8 @@ serverSettings AppConfig{..} =
97
98
& setServerName (" postgrest/" <> prettyVersion)
98
99
99
100
-- | PostgREST application
100
- postgrest :: AppConfig -> AppState. AppState -> IO () -> Wai. Application
101
- postgrest conf appState connWorker =
101
+ postgrest :: AppConfig -> AppState. AppState -> IO () -> ( Observation -> IO () ) -> Wai. Application
102
+ postgrest conf appState connWorker observer =
102
103
traceHeaderMiddleware conf .
103
104
Cors. middleware (configServerCorsAllowedOrigins conf) .
104
105
Auth. middleware appState .
@@ -115,7 +116,7 @@ postgrest conf appState connWorker =
115
116
let
116
117
eitherResponse :: IO (Either Error Wai. Response )
117
118
eitherResponse =
118
- runExceptT $ postgrestResponse appState appConf maybeSchemaCache pgVer authResult req
119
+ runExceptT $ postgrestResponse appState appConf maybeSchemaCache pgVer authResult req observer
119
120
120
121
response <- either Error. errorResponseFor identity <$> eitherResponse
121
122
-- Launch the connWorker when the connection is down. The postgrest
@@ -134,8 +135,9 @@ postgrestResponse
134
135
-> PgVersion
135
136
-> AuthResult
136
137
-> Wai. Request
138
+ -> (Observation -> IO () )
137
139
-> Handler IO Wai. Response
138
- postgrestResponse appState conf@ AppConfig {.. } maybeSchemaCache pgVer authResult@ AuthResult {.. } req = do
140
+ postgrestResponse appState conf@ AppConfig {.. } maybeSchemaCache pgVer authResult@ AuthResult {.. } req observer = do
139
141
sCache <-
140
142
case maybeSchemaCache of
141
143
Just sCache ->
@@ -151,22 +153,23 @@ postgrestResponse appState conf@AppConfig{..} maybeSchemaCache pgVer authResult@
151
153
ApiRequest. userApiRequest conf req body sCache
152
154
153
155
let jwtTime = if configServerTimingEnabled then Auth. getJwtDur req else Nothing
154
- handleRequest authResult conf appState (Just authRole /= configDbAnonRole) configDbPreparedStatements pgVer apiRequest sCache jwtTime parseTime
156
+ handleRequest authResult conf appState (Just authRole /= configDbAnonRole) configDbPreparedStatements pgVer apiRequest sCache jwtTime parseTime observer
155
157
156
- runDbHandler :: AppState. AppState -> AppConfig -> SQL. IsolationLevel -> SQL. Mode -> Bool -> Bool -> DbHandler b -> Handler IO b
157
- runDbHandler appState config isoLvl mode authenticated prepared handler = do
158
+ runDbHandler :: AppState. AppState -> AppConfig -> SQL. IsolationLevel -> SQL. Mode -> Bool -> Bool -> ( Observation -> IO () ) -> DbHandler b -> Handler IO b
159
+ runDbHandler appState config isoLvl mode authenticated prepared observer handler = do
158
160
dbResp <- lift $ do
159
161
let transaction = if prepared then SQL. transaction else SQL. unpreparedTransaction
160
- AppState. usePool appState config . transaction isoLvl mode $ runExceptT handler
162
+ AppState. usePool appState config ( transaction isoLvl mode $ runExceptT handler) observer
161
163
162
164
resp <-
163
165
liftEither . mapLeft Error. PgErr $
164
166
mapLeft (Error. PgError authenticated) dbResp
165
167
166
168
liftEither resp
167
169
168
- handleRequest :: AuthResult -> AppConfig -> AppState. AppState -> Bool -> Bool -> PgVersion -> ApiRequest -> SchemaCache -> Maybe Double -> Maybe Double -> Handler IO Wai. Response
169
- handleRequest AuthResult {.. } conf appState authenticated prepared pgVer apiReq@ ApiRequest {.. } sCache jwtTime parseTime =
170
+ handleRequest :: AuthResult -> AppConfig -> AppState. AppState -> Bool -> Bool -> PgVersion -> ApiRequest -> SchemaCache ->
171
+ Maybe Double -> Maybe Double -> (Observation -> IO () ) -> Handler IO Wai. Response
172
+ handleRequest AuthResult {.. } conf appState authenticated prepared pgVer apiReq@ ApiRequest {.. } sCache jwtTime parseTime observer =
170
173
case (iAction, iTarget) of
171
174
(ActionRead headersOnly, TargetIdent identifier) -> do
172
175
(planTime', wrPlan) <- withTiming $ liftEither $ Plan. wrappedReadPlan identifier conf sCache apiReq
@@ -231,7 +234,7 @@ handleRequest AuthResult{..} conf appState authenticated prepared pgVer apiReq@A
231
234
roleSettings = fromMaybe mempty (HM. lookup authRole $ configRoleSettings conf)
232
235
roleIsoLvl = HM. findWithDefault SQL. ReadCommitted authRole $ configRoleIsoLvl conf
233
236
runQuery isoLvl funcSets mode query =
234
- runDbHandler appState conf isoLvl mode authenticated prepared $ do
237
+ runDbHandler appState conf isoLvl mode authenticated prepared observer $ do
235
238
Query. setPgLocals conf authClaims authRole (HM. toList roleSettings) funcSets apiReq
236
239
Query. runPreReq conf
237
240
query
0 commit comments