@@ -9,9 +9,14 @@ import (
99 "fmt"
1010 "io"
1111 "net/http"
12+ "quesma/clickhouse"
1213 "quesma/logger"
14+ "quesma/quesma/config"
1315 "quesma/quesma/recovery"
16+ "quesma/quesma/types"
17+ "quesma/schema"
1418 quesma_api "quesma_v2/core"
19+ "quesma_v2/core/diag"
1520 "strings"
1621 "sync"
1722)
@@ -22,11 +27,32 @@ type BasicHTTPFrontendConnector struct {
2227 mutex sync.Mutex
2328 responseMutator func (w http.ResponseWriter ) http.ResponseWriter
2429 endpoint string
30+ routerInstance * RouterV2
31+ logManager * clickhouse.LogManager
32+ registry schema.Registry
33+ config * config.QuesmaConfiguration
34+
35+ diagnostic diag.Diagnostic
36+ }
37+
38+ func (h * BasicHTTPFrontendConnector ) InjectDiagnostic (diagnostic diag.Diagnostic ) {
39+
40+ h .diagnostic = diagnostic
41+
42+ // TODO this is a hack
43+ if h .routerInstance != nil {
44+ h .routerInstance .InjectDiagnostic (diagnostic )
45+ }
2546}
2647
27- func NewBasicHTTPFrontendConnector (endpoint string ) * BasicHTTPFrontendConnector {
48+ func NewBasicHTTPFrontendConnector (endpoint string , config * config.QuesmaConfiguration ) * BasicHTTPFrontendConnector {
49+
2850 return & BasicHTTPFrontendConnector {
29- endpoint : endpoint ,
51+ endpoint : endpoint ,
52+ config : config ,
53+ routerInstance : NewRouterV2 (config ),
54+ logManager : nil ,
55+ registry : nil ,
3056 responseMutator : func (w http.ResponseWriter ) http.ResponseWriter {
3157 return w
3258 },
@@ -43,25 +69,40 @@ func (h *BasicHTTPFrontendConnector) GetRouter() quesma_api.Router {
4369
4470func (h * BasicHTTPFrontendConnector ) ServeHTTP (w http.ResponseWriter , req * http.Request ) {
4571 defer recovery .LogPanic ()
72+
73+ ctx := req .Context ()
74+ requestPreprocessors := quesma_api.ProcessorChain {}
75+ requestPreprocessors = append (requestPreprocessors , quesma_api .NewTraceIdPreprocessor ())
76+
4677 reqBody , err := PeekBodyV2 (req )
4778 if err != nil {
4879 http .Error (w , "Error reading request body" , http .StatusInternalServerError )
4980 return
5081 }
51- ctx := req .Context ()
52- requestPreprocessors := quesma_api.ProcessorChain {}
53- requestPreprocessors = append (requestPreprocessors , quesma_api .NewTraceIdPreprocessor ())
82+
83+ ua := req .Header .Get ("User-Agent" )
84+ if h .diagnostic .PhoneHomeAgent () != nil {
85+ h .diagnostic .PhoneHomeAgent ().UserAgentCounters ().Add (ua , 1 )
86+ }
5487
5588 quesmaRequest , ctx , err := preprocessRequest (ctx , & quesma_api.Request {
56- Method : req .Method ,
57- Path : strings .TrimSuffix (req .URL .Path , "/" ),
58- Params : map [string ]string {},
59- Headers : req .Header ,
60- QueryParams : req .URL .Query (),
61- Body : string (reqBody ),
89+ Method : req .Method ,
90+ Path : strings .TrimSuffix (req .URL .Path , "/" ),
91+ Params : map [string ]string {},
92+ Headers : req .Header ,
93+ QueryParams : req .URL .Query (),
94+ Body : string (reqBody ),
95+ OriginalRequest : req ,
6296 }, requestPreprocessors )
6397
98+ if err != nil {
99+ logger .ErrorWithCtx (ctx ).Msgf ("Error preprocessing request: %v" , err )
100+ }
101+
102+ quesmaRequest .ParsedBody = types .ParseRequestBody (quesmaRequest .Body )
103+
64104 handlersPipe , decision := h .router .Matches (quesmaRequest )
105+
65106 if decision != nil {
66107 w .Header ().Set (QuesmaTableResolverHeader , decision .String ())
67108 } else {
@@ -70,53 +111,53 @@ func (h *BasicHTTPFrontendConnector) ServeHTTP(w http.ResponseWriter, req *http.
70111 dispatcher := & quesma_api.Dispatcher {}
71112 w = h .responseMutator (w )
72113
73- http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
74- if handlersPipe != nil {
75- result , _ := handlersPipe .Handler (context .Background (), & quesma_api.Request {OriginalRequest : req })
76- var quesmaResponse * quesma_api.Result
77-
78- if result != nil {
79- metadata , message := dispatcher .Dispatch (handlersPipe .Processors , result .Meta , result .GenericResult )
80- result = & quesma_api.Result {
81- Body : result .Body ,
82- Meta : metadata ,
83- StatusCode : result .StatusCode ,
84- GenericResult : message ,
85- }
86- quesmaResponse = result
114+ if handlersPipe != nil {
115+ quesmaResponse , err := recordRequestToClickhouseV2 (req .URL .Path , h .diagnostic .DebugInfoCollector (), func () (* quesma_api.Result , error ) {
116+ var result * quesma_api.Result
117+ result , err = handlersPipe .Handler (ctx , quesmaRequest )
118+
119+ if result == nil {
120+ return result , err
87121 }
88- zip := strings .Contains (req .Header .Get ("Accept-Encoding" ), "gzip" )
89- _ = zip
90- if err == nil {
91- logger .Debug ().Ctx (ctx ).Msg ("responding from quesma" )
92- unzipped := []byte {}
93- if quesmaResponse != nil {
94- unzipped = quesmaResponse .GenericResult .([]byte )
95- }
96- if len (unzipped ) == 0 {
97- logger .WarnWithCtx (ctx ).Msgf ("empty response from Clickhouse, method=%s" , req .Method )
98- }
99- AddProductAndContentHeaders (req .Header , w .Header ())
100- _ , err := w .Write (unzipped )
101- if err != nil {
102- fmt .Printf ("Error writing response: %s\n " , err )
103- }
104-
105- } else {
122+ metadata , message := dispatcher .Dispatch (handlersPipe .Processors , result .Meta , result .GenericResult )
106123
124+ result = & quesma_api.Result {
125+ Body : result .Body ,
126+ Meta : metadata ,
127+ StatusCode : result .StatusCode ,
128+ GenericResult : message ,
107129 }
130+ return result , err
131+ })
132+
133+ zip := strings .Contains (req .Header .Get ("Accept-Encoding" ), "gzip" )
134+ if err == nil {
135+ logger .Debug ().Ctx (ctx ).Msg ("responding from quesma" )
136+ unzipped := []byte {}
137+ if quesmaResponse != nil {
138+ unzipped = quesmaResponse .GenericResult .([]byte )
139+ }
140+ if len (unzipped ) == 0 {
141+ logger .WarnWithCtx (ctx ).Msgf ("empty response from Clickhouse, method=%s" , req .Method )
142+ }
143+ AddProductAndContentHeaders (req .Header , w .Header ())
144+
145+ responseFromQuesmaV2 (ctx , unzipped , w , quesmaResponse , zip )
146+
108147 } else {
109- if h .router .GetFallbackHandler () != nil {
110- fmt .Printf ("No handler found for path: %s\n " , req .URL .Path )
111- handler := h .router .GetFallbackHandler ()
112- result , _ := handler (context .Background (), & quesma_api.Request {OriginalRequest : req })
113- _ , err := w .Write (result .GenericResult .([]byte ))
114- if err != nil {
115- fmt .Printf ("Error writing response: %s\n " , err )
116- }
148+ h .routerInstance .errorResponseV2 (ctx , err , w )
149+ }
150+ } else {
151+ if h .router .GetFallbackHandler () != nil {
152+ fmt .Printf ("No handler found for path: %s\n " , req .URL .Path )
153+ handler := h .router .GetFallbackHandler ()
154+ result , _ := handler (context .Background (), & quesma_api.Request {OriginalRequest : req })
155+ _ , err := w .Write (result .GenericResult .([]byte ))
156+ if err != nil {
157+ fmt .Printf ("Error writing response: %s\n " , err )
117158 }
118159 }
119- }). ServeHTTP ( w , req )
160+ }
120161}
121162
122163func (h * BasicHTTPFrontendConnector ) Listen () error {
0 commit comments