Skip to content

Commit

Permalink
Move header processing to responseFromQuesma() (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
pivovarit authored May 21, 2024
1 parent f40428c commit c0401cb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
7 changes: 7 additions & 0 deletions quesma/quesma/mux/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ type (

type RequestMatcherFunc func(req *Request) bool

func ServerErrorResult() *Result {
return &Result{
StatusCode: http.StatusInternalServerError,
Meta: map[string]string{"Content-Type": "text/plain"},
}
}

func (f RequestMatcherFunc) Matches(req *Request) bool {
return f(req)
}
Expand Down
21 changes: 11 additions & 10 deletions quesma/quesma/quesma.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,18 @@ func responseFromElastic(ctx context.Context, elkResponse *http.Response, w http
elkResponse.Body.Close()
}

func responseFromQuesma(ctx context.Context, unzipped []byte, w http.ResponseWriter, elkResponse *http.Response, statusCode int, zip bool) {
func responseFromQuesma(ctx context.Context, unzipped []byte, w http.ResponseWriter, elkResponse *http.Response, quesmaResponse *mux.Result, zip bool) {
id := ctx.Value(tracing.RequestIdCtxKey).(string)
logger.Debug().Str(logger.RID, id).Msg("responding from Quesma")

for key, value := range quesmaResponse.Meta {
w.Header().Set(key, value)
}
if zip {
w.Header().Set("Content-Encoding", "gzip")
}
w.Header().Set(quesmaSourceHeader, quesmaSourceClickhouse)
w.WriteHeader(statusCode)
w.WriteHeader(quesmaResponse.StatusCode)
if zip {
zipped, err := gzip.Zip(unzipped)
if err != nil {
Expand Down Expand Up @@ -190,13 +196,8 @@ func (r *router) reroute(ctx context.Context, w http.ResponseWriter, req *http.R
logger.WarnWithCtx(ctx).Msg("empty response from Clickhouse")
}
addProductAndContentHeaders(req.Header, w.Header())
for key, value := range quesmaResponse.Meta {
w.Header().Set(key, value)
}
if zip {
w.Header().Set("Content-Encoding", "gzip")
}
responseFromQuesma(ctx, unzipped, w, elkResponse, quesmaResponse.StatusCode, zip)

responseFromQuesma(ctx, unzipped, w, elkResponse, quesmaResponse, zip)

} else {
if elkResponse != nil && r.config.Mode == config.DualWriteQueryClickhouseFallback {
Expand All @@ -213,7 +214,7 @@ func (r *router) reroute(ctx context.Context, w http.ResponseWriter, req *http.R

// We should not send our error message to the client. There can be sensitive information in it.
// We will send ID of failed request instead
responseFromQuesma(ctx, []byte(fmt.Sprintf("Internal server error. Request ID: %s\n", requestId)), w, elkResponse, 500, zip)
responseFromQuesma(ctx, []byte(fmt.Sprintf("Internal server error. Request ID: %s\n", requestId)), w, elkResponse, mux.ServerErrorResult(), zip)
}
}
} else {
Expand Down

0 comments on commit c0401cb

Please sign in to comment.