diff --git a/quesma/quesma/mux/mux.go b/quesma/quesma/mux/mux.go index 7765946b0..72713607f 100644 --- a/quesma/quesma/mux/mux.go +++ b/quesma/quesma/mux/mux.go @@ -18,14 +18,14 @@ type ( compiledPath urlpath.Path httpMethod string predicate MatchPredicate - handler handler + handler Handler } Result struct { Body string Meta map[string]string StatusCode int } - handler func(ctx context.Context, body, uri string, params map[string]string, headers http.Header, queryParams url.Values) (*Result, error) + Handler func(ctx context.Context, body, uri string, params map[string]string, headers http.Header, queryParams url.Values) (*Result, error) MatchPredicate func(params map[string]string, body string) bool ) @@ -36,40 +36,32 @@ func NewPathRouter() *PathRouter { return &PathRouter{mappings: make([]mapping, 0)} } -func (p *PathRouter) RegisterPath(pattern, httpMethod string, handler handler) { +func (p *PathRouter) RegisterPath(pattern, httpMethod string, handler Handler) { mapping := mapping{pattern, urlpath.New(pattern), httpMethod, identity(), handler} p.mappings = append(p.mappings, mapping) } -func (p *PathRouter) RegisterPathMatcher(pattern string, httpMethods []string, predicate MatchPredicate, handler handler) { +func (p *PathRouter) RegisterPathMatcher(pattern string, httpMethods []string, predicate MatchPredicate, handler Handler) { for _, httpMethod := range httpMethods { mapping := mapping{pattern, urlpath.New(pattern), httpMethod, predicate, handler} p.mappings = append(p.mappings, mapping) } } -func (p *PathRouter) Execute(ctx context.Context, path, body, httpMethod string, headers http.Header, queryParams url.Values) (*Result, error) { +func (p *PathRouter) Matches(path, httpMethod, body string) (Handler, urlpath.Match, bool) { handler, meta, found := p.findHandler(path, httpMethod, body) - if found { - return handler(ctx, body, path, meta.Params, headers, queryParams) - } - return nil, nil -} - -func (p *PathRouter) Matches(path, httpMethod, body string) bool { - _, _, found := p.findHandler(path, httpMethod, body) if found { routerStatistics.addMatched(path) logger.Debug().Msgf("Matched path: %s", path) - return true + return handler, meta, true } else { routerStatistics.addUnmatched(path) logger.Debug().Msgf("Non-matched path: %s", path) - return false + return nil, urlpath.Match{}, false } } -func (p *PathRouter) findHandler(path, httpMethod, body string) (handler, urlpath.Match, bool) { +func (p *PathRouter) findHandler(path, httpMethod, body string) (Handler, urlpath.Match, bool) { path = strings.TrimSuffix(path, "/") for _, m := range p.mappings { meta, match := m.compiledPath.Match(path) diff --git a/quesma/quesma/quesma.go b/quesma/quesma/quesma.go index 727dfe038..06cb7eb51 100644 --- a/quesma/quesma/quesma.go +++ b/quesma/quesma/quesma.go @@ -121,7 +121,8 @@ func (r *router) reroute(ctx context.Context, w http.ResponseWriter, req *http.R w.WriteHeader(500) w.Write(queryparser.InternalQuesmaError("Unknown Quesma error")) }) - if router.Matches(req.URL.Path, req.Method, string(reqBody)) { + handler, parameters, found := router.Matches(req.URL.Path, req.Method, string(reqBody)) + if found { var elkResponseChan = make(chan elasticResult) if r.config.Elasticsearch.Call { @@ -130,7 +131,7 @@ func (r *router) reroute(ctx context.Context, w http.ResponseWriter, req *http.R req.URL.Query() quesmaResponse, err := recordRequestToClickhouse(req.URL.Path, r.quesmaManagementConsole, func() (*mux.Result, error) { - return router.Execute(ctx, req.URL.Path, string(reqBody), req.Method, req.Header, req.URL.Query()) + return handler(ctx, string(reqBody), req.URL.Path, parameters.Params, req.Header, req.URL.Query()) }) var elkRawResponse elasticResult var elkResponse *http.Response