Skip to content

Commit

Permalink
Integrate v1 reroute logic with v2 APIs (#1103)
Browse files Browse the repository at this point in the history
This PR implements first step of integration, it calls `router.Matches`
from request handler as in v1 version
  • Loading branch information
pdelewski authored Dec 12, 2024
1 parent 89ac4e0 commit 4351eef
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 24 deletions.
32 changes: 18 additions & 14 deletions quesma/frontend_connectors/basic_http_frontend_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"bytes"
"context"
"fmt"
"github.com/ucarion/urlpath"
"io"
"net/http"
"quesma/quesma/recovery"
quesma_api "quesma_v2/core"
"strings"
"sync"
)

Expand Down Expand Up @@ -40,8 +41,22 @@ func (h *BasicHTTPFrontendConnector) GetRouter() quesma_api.Router {
}

func (h *BasicHTTPFrontendConnector) ServeHTTP(w http.ResponseWriter, req *http.Request) {
handlers := h.router.GetHandlers()
handlerWrapper := getMatchingHandler(req.URL.Path, handlers)
defer recovery.LogPanic()
reqBody, err := PeekBodyV2(req)
if err != nil {
http.Error(w, "Error reading request body", http.StatusInternalServerError)
return
}

quesmaRequest := &quesma_api.Request{
Method: req.Method,
Path: strings.TrimSuffix(req.URL.Path, "/"),
Params: map[string]string{},
Headers: req.Header,
QueryParams: req.URL.Query(),
Body: string(reqBody),
}
handlerWrapper, _ := h.router.Matches(quesmaRequest)
dispatcher := &quesma_api.Dispatcher{}
w = h.responseMutator(w)
if handlerWrapper == nil {
Expand Down Expand Up @@ -69,17 +84,6 @@ func (h *BasicHTTPFrontendConnector) ServeHTTP(w http.ResponseWriter, req *http.
}).ServeHTTP(w, req)
}

func getMatchingHandler(requestPath string, handlers map[string]quesma_api.HandlersPipe) *quesma_api.HandlersPipe {
for path, handler := range handlers {
urlPath := urlpath.New(path)
_, matches := urlPath.Match(requestPath)
if matches {
return &handler
}
}
return nil
}

func (h *BasicHTTPFrontendConnector) Listen() error {
h.mutex.Lock()
defer h.mutex.Unlock()
Expand Down
18 changes: 16 additions & 2 deletions quesma/frontend_connectors/router_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,24 @@ func (r *RouterV2) Reroute(ctx context.Context, w http.ResponseWriter, req *http
} else {
w.Header().Set(QuesmaTableResolverHeader, "n/a")
}

dispatcher := &quesma_api.Dispatcher{}
if handlersPipe != nil {
quesmaResponse, err := recordRequestToClickhouseV2(req.URL.Path, r.QuesmaManagementConsole, func() (*quesma_api.Result, error) {
return handlersPipe.Handler(ctx, quesmaRequest)
var result *quesma_api.Result
result, err = handlersPipe.Handler(ctx, quesmaRequest)

if result == nil {
return result, err
}
metadata, message := dispatcher.Dispatch(handlersPipe.Processors, result.Meta, result.GenericResult)

result = &quesma_api.Result{
Body: result.Body,
Meta: metadata,
StatusCode: result.StatusCode,
GenericResult: message,
}
return result, err
})

zip := strings.Contains(req.Header.Get("Accept-Encoding"), "gzip")
Expand Down
24 changes: 16 additions & 8 deletions quesma/v2/core/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,23 @@ func (p *PathRouter) GetHandlers() map[string]HandlersPipe {
return callInfos
}
func (p *PathRouter) SetHandlers(handlers map[string]HandlersPipe) {
newHandlers := make(map[string]HandlersPipe, 0)
for path, handler := range handlers {
if _, ok := handler.Predicate.(*predicateAlways); ok { // in order to pass processors we have to make this alignment (predicates aren't present in the old API
p.mappings = append(p.mappings, mapping{pattern: path,
compiledPath: urlpath.New(path),
handler: &HandlersPipe{Handler: handler.Handler,
Predicate: handler.Predicate,
Processors: handler.Processors}})
} else {
p.Register(path, handler.Predicate, handler.Handler)
for index := range p.mappings {
if p.mappings[index].pattern == path {
p.mappings[index].handler.Processors = handler.Processors
p.mappings[index].handler.Predicate = handler.Predicate
} else {
newHandlers[path] = handler
}
}
}
for path, handler := range newHandlers {
p.mappings = append(p.mappings, mapping{pattern: path,
compiledPath: urlpath.New(path),
predicate: handler.Predicate,
handler: &HandlersPipe{Handler: handler.Handler,
Predicate: handler.Predicate,
Processors: handler.Processors}})
}
}

0 comments on commit 4351eef

Please sign in to comment.