@@ -61,7 +61,7 @@ run tracer (fromIntegral -> port) host configurationFile databaseDirectory = do
6161 & Warp. setServerName " db-server"
6262 & Warp. setTimeout 120
6363 & Warp. setMaximumBodyFlush Nothing
64- & Warp. setBeforeMainLoop (traceWith tr HttpServerListening {host, port})
64+ & Warp. setBeforeMainLoop (traceWith tr ( HttpListenLog ( ListenEvent {host, port})) )
6565
6666webApp :: ChainDB IO StandardBlock -> Application
6767webApp db req send =
@@ -74,8 +74,8 @@ webApp db req send =
7474 _ -> send responseNotFound
7575 where
7676 responseNotFound = responseLBS status404 [] " "
77- responseBadRequest msg = responseLBS status400 [] msg
78- responseOk content = responseLBS status200 [(" content-type" , " application/json" )] content
77+ responseBadRequest = responseLBS status400 []
78+ responseOk = responseLBS status200 [(" content-type" , " application/json" )]
7979
8080 handleGetSnapshots =
8181 listSnapshots db >>= \ snapshotsPoints ->
@@ -90,7 +90,7 @@ webApp db req send =
9090 Err err -> send $ responseBadRequest (" Bad query: " <> toBytestring err)
9191 Found result -> send $ responseOk (LHex. encode result)
9292
93- handleGetHeader slot hash = handleWithPoint getHeader slot hash
93+ handleGetHeader = handleWithPoint getHeader
9494
9595 handleGetSnapshot slot =
9696 case makeSlot slot of
@@ -101,9 +101,9 @@ webApp db req send =
101101 Err err -> send $ responseBadRequest (" Bad query: " <> toBytestring err)
102102 Found snapshot -> send $ responseOk (LHex. encode snapshot)
103103
104- handleGetParent slot hash = handleWithPoint getParent slot hash
104+ handleGetParent = handleWithPoint getParent
105105
106- handleGetBlock slot hash = handleWithPoint getBlock slot hash
106+ handleGetBlock = handleWithPoint getBlock
107107
108108-- * Tracing
109109
@@ -131,12 +131,12 @@ withLog hdl k = do
131131tracerMiddleware :: Tracer IO HttpServerLog -> Middleware
132132tracerMiddleware tr runApp req send = do
133133 start <- GHC.Clock. getMonotonicTimeNSec
134- traceWith tr HttpRequest {path, method}
134+ traceWith tr ( HttpRequestLog ( RequestEvent {path, method}))
135135 runApp req $ \ res -> do
136136 result <- send res
137137 end <- GHC.Clock. getMonotonicTimeNSec
138138 let time = mkRequestTime start end
139- traceWith tr HttpResponse {status = mkStatus (responseStatus res), time}
139+ traceWith tr ( HttpResponseLog ( ResponseEvent {status = mkStatus (responseStatus res), time}))
140140 pure result
141141 where
142142 method = decodeUtf8 (requestMethod req)
@@ -161,9 +161,20 @@ data HttpStatus = HttpStatus {code :: Int, message :: Text}
161161 deriving stock (Eq , Generic , Show )
162162 deriving anyclass (ToJSON , FromJSON )
163163
164- data HttpServerLog
165- = HttpServerListening { host :: String , port :: Int }
166- | HttpRequest { path :: [ Text ], method :: Text }
167- | HttpResponse { status :: HttpStatus , time :: RequestTime }
164+ data HttpServerLog =
165+ HttpListenLog ListenEvent
166+ | HttpRequestLog RequestEvent
167+ | HttpResponseLog ResponseEvent
168168 deriving stock (Eq , Show , Generic )
169169 deriving anyclass (ToJSON , FromJSON )
170+
171+ data ListenEvent = ListenEvent { host :: String , port :: Int }
172+ deriving stock (Eq , Show , Generic )
173+ deriving anyclass (ToJSON , FromJSON )
174+ data RequestEvent = RequestEvent { path :: [Text ], method :: Text }
175+ deriving stock (Eq , Show , Generic )
176+ deriving anyclass (ToJSON , FromJSON )
177+ data ResponseEvent = ResponseEvent { status :: HttpStatus , time :: RequestTime }
178+ deriving stock (Eq , Show , Generic )
179+ deriving anyclass (ToJSON , FromJSON )
180+
0 commit comments