Files
upstream-corteza/server/pkg/apigw/route.go
upstream-mirror 7e385fc126
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
2026-09-18 01:22:23 +05:00

95 lines
1.9 KiB
Go

package apigw
import (
"fmt"
"net/http"
"time"
actx "github.com/cortezaproject/corteza/server/pkg/apigw/ctx"
"github.com/cortezaproject/corteza/server/pkg/apigw/profiler"
"github.com/cortezaproject/corteza/server/pkg/apigw/types"
"github.com/cortezaproject/corteza/server/pkg/auth"
h "github.com/cortezaproject/corteza/server/pkg/http"
"go.uber.org/zap"
)
type (
route struct {
ID uint64
endpoint string
method string
meta routeMeta
cfg types.Config
log *zap.Logger
pr *profiler.Profiler
handler http.Handler
errHandler types.ErrorHandlerFunc
}
routeMeta struct {
debug bool
async bool
}
)
func (r route) ServeHTTP(w http.ResponseWriter, req *http.Request) {
var (
ctx = auth.SetIdentityToContext(req.Context(), auth.ServiceUser())
start = time.Now()
scope = types.Scp{}
hit = &profiler.Hit{}
)
r.log.Debug("started serving route")
ar, err := h.NewRequest(req)
if err != nil {
r.log.Error("could not get initial request", zap.Error(err))
}
scope.Set("opts", r.cfg)
scope.Set("request", ar)
// use profiler, override any profiling prefilter
if r.cfg.Profiler.Enabled && r.cfg.Profiler.Global {
// add request to profiler
hit = r.pr.Hit(ar)
hit.Route = r.ID
}
req = req.WithContext(actx.ScopeToContext(ctx, &scope))
req = req.WithContext(actx.ProfilerToContext(req.Context(), hit))
r.handler.ServeHTTP(w, req)
r.log.Debug("finished serving route",
zap.Duration("duration", time.Since(start)),
)
if !r.cfg.Profiler.Enabled {
return
}
if r.cfg.Profiler.Global {
r.pr.Push(hit)
return
}
if hit = actx.ProfilerFromContext(req.Context()); hit != nil && hit.R != nil {
// updated hit from a possible prefilter
// we need to push route ID even if the profiler is disabled
hit.Route = r.ID
hit.Status = http.StatusOK
r.pr.Push(hit)
r.log.Debug("pushing request to profiler")
}
}
func (r route) String() string {
return fmt.Sprintf("%s %s", r.method, r.endpoint)
}