Skip to content

Commit

Permalink
Remove Execute() router function and replace with direct handler call
Browse files Browse the repository at this point in the history
  • Loading branch information
pivovarit committed May 20, 2024
1 parent bc2f642 commit 72c218f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
24 changes: 8 additions & 16 deletions quesma/quesma/mux/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions quesma/quesma/quesma.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 72c218f

Please sign in to comment.