Skip to content

Commit

Permalink
use default methods instead of erroring
Browse files Browse the repository at this point in the history
  • Loading branch information
jranson committed Aug 12, 2024
1 parent b4647bf commit 07a9550
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 34 deletions.
42 changes: 20 additions & 22 deletions pkg/httpserver/listeners.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ func applyListenerConfigs(conf, oldConf *config.Config,
(!hasOldMC || (conf.Metrics.ListenAddress != oldConf.Metrics.ListenAddress ||
conf.Metrics.ListenPort != oldConf.Metrics.ListenPort)) {
lg.DrainAndClose("metricsListener", 0)
metricsRouter.RegisterRoute("/metrics", []string{""},
[]string{http.MethodGet}, false, metrics.Handler())
metricsRouter.RegisterRoute(conf.Main.ConfigHandlerPath, []string{""},
[]string{http.MethodGet}, false, http.HandlerFunc(handlers.ConfigHandleFunc(conf)))
metricsRouter.RegisterRoute("/metrics", nil, nil,
false, metrics.Handler())
metricsRouter.RegisterRoute(conf.Main.ConfigHandlerPath, nil, nil,
false, http.HandlerFunc(handlers.ConfigHandleFunc(conf)))
if conf.Main.PprofServer == "both" || conf.Main.PprofServer == "metrics" {
routing.RegisterPprofRoutes("metrics", metricsRouter, log)
}
Expand All @@ -151,10 +151,10 @@ func applyListenerConfigs(conf, oldConf *config.Config,
conf.Metrics.ListenAddress, conf.Metrics.ListenPort,
conf.Frontend.ConnectionsLimit, nil, metricsRouter, wg, nil, errorFunc, 0, log)
} else {
metricsRouter.RegisterRoute("/metrics", []string{""},
[]string{http.MethodGet}, false, metrics.Handler())
metricsRouter.RegisterRoute(conf.Main.ConfigHandlerPath, []string{""},
[]string{http.MethodGet}, false, http.HandlerFunc(handlers.ConfigHandleFunc(conf)))
metricsRouter.RegisterRoute("/metrics", nil, nil,
false, metrics.Handler())
metricsRouter.RegisterRoute(conf.Main.ConfigHandlerPath, nil, nil,
false, http.HandlerFunc(handlers.ConfigHandleFunc(conf)))
lg.UpdateRouter("metricsListener", metricsRouter)
}

Expand All @@ -166,27 +166,25 @@ func applyListenerConfigs(conf, oldConf *config.Config,
conf.ReloadConfig.ListenPort != oldConf.ReloadConfig.ListenPort)) {
wg.Add(1)
lg.DrainAndClose("reloadListener", time.Millisecond*500)

rr.RegisterRoute(conf.Main.ConfigHandlerPath, []string{""},
[]string{http.MethodGet}, false, http.HandlerFunc(handlers.ConfigHandleFunc(conf)))
rr.RegisterRoute(conf.ReloadConfig.HandlerPath, []string{""},
[]string{http.MethodGet}, false, reloadHandler)
rr.RegisterRoute(conf.Main.PurgePathHandlerPath, []string{""},
[]string{http.MethodGet}, false, http.HandlerFunc(handlers.PurgePathHandlerFunc(conf, &o)))
rr.RegisterRoute(conf.Main.ConfigHandlerPath, nil, nil,
false, http.HandlerFunc(handlers.ConfigHandleFunc(conf)))
rr.RegisterRoute(conf.ReloadConfig.HandlerPath, nil, nil,
false, reloadHandler)
rr.RegisterRoute(conf.Main.PurgePathHandlerPath, nil, nil,
false, http.HandlerFunc(handlers.PurgePathHandlerFunc(conf, &o)))
if conf.Main.PprofServer == "both" || conf.Main.PprofServer == "reload" {
routing.RegisterPprofRoutes("reload", rr, log)
}
go lg.StartListener("reloadListener",
conf.ReloadConfig.ListenAddress, conf.ReloadConfig.ListenPort,
conf.Frontend.ConnectionsLimit, nil, rr, wg, nil, errorFunc, 0, log)
} else {

rr.RegisterRoute(conf.Main.ConfigHandlerPath, []string{""},
[]string{http.MethodGet}, false, http.HandlerFunc(handlers.ConfigHandleFunc(conf)))
rr.RegisterRoute(conf.ReloadConfig.HandlerPath, []string{""},
[]string{http.MethodGet}, false, reloadHandler)
rr.RegisterRoute(conf.Main.PurgePathHandlerPath, []string{""},
[]string{http.MethodGet}, false, http.HandlerFunc(handlers.PurgePathHandlerFunc(conf, &o)))
rr.RegisterRoute(conf.Main.ConfigHandlerPath, nil, nil,
false, http.HandlerFunc(handlers.ConfigHandleFunc(conf)))
rr.RegisterRoute(conf.ReloadConfig.HandlerPath, nil, nil,
false, reloadHandler)
rr.RegisterRoute(conf.Main.PurgePathHandlerPath, nil, nil,
false, http.HandlerFunc(handlers.PurgePathHandlerFunc(conf, &o)))
lg.UpdateRouter("reloadListener", rr)
}
}
11 changes: 10 additions & 1 deletion pkg/proxy/methods/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
// Package methods provides functionality for handling HTTP methods
package methods

import "net/http"
import (
"net/http"
"strings"
)

const (
get uint16 = 1 << iota
Expand Down Expand Up @@ -108,3 +111,9 @@ func MethodMask(methods ...string) uint16 {
}
return i
}

// IsValidMethod returns true if the provided method is recognized in methodsMap
func IsValidMethod(method string) bool {
_, ok := methodsMap[strings.ToUpper(method)]
return ok
}
11 changes: 10 additions & 1 deletion pkg/router/lm/lm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/trickstercache/trickster/v2/pkg/errors"
"github.com/trickstercache/trickster/v2/pkg/proxy/headers"
meth "github.com/trickstercache/trickster/v2/pkg/proxy/methods"
"github.com/trickstercache/trickster/v2/pkg/router"
)

Expand All @@ -27,6 +28,7 @@ func NewRouter() router.Router {
}

var emptyHost = []string{""}
var defaultMethods = []string{http.MethodGet, http.MethodHead}

func (rt *rtr) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == "*" {
Expand All @@ -46,7 +48,14 @@ func (rt *rtr) RegisterRoute(path string, hosts, methods []string,
return errors.ErrInvalidPath
}
if len(methods) == 0 {
return errors.ErrInvalidMethod
methods = defaultMethods
} else {
for i, m := range methods {
if !meth.IsValidMethod(m) {
return errors.ErrInvalidMethod
}
methods[i] = strings.ToUpper(m)
}
}
if hosts == nil {
hosts = emptyHost
Expand Down
3 changes: 3 additions & 0 deletions pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ type Router interface {
// ServeHTTP services the provided HTTP Request and write the Response
ServeHTTP(http.ResponseWriter, *http.Request)
// RegisterRoute registers a handler for the provided path/host/method(s)
// If hosts is nil, the route uses global-routing instead of host-based
// If methods is nil, the route is applicable to GET and HEAD requests
// If methods includes GET but not HEAD, HEAD is automatically included
RegisterRoute(path string, hosts, methods []string, matchPrefix bool,
handler http.Handler) error
// Handler returns the handler matching the method/host/path in the Request
Expand Down
19 changes: 9 additions & 10 deletions pkg/routing/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ import (
func RegisterPprofRoutes(routerName string, r router.Router, logger interface{}) {
tl.Info(logger,
"registering pprof /debug routes", tl.Pairs{"routerName": routerName})
r.RegisterRoute("/debug/pprof/", nil, []string{http.MethodGet},
r.RegisterRoute("/debug/pprof/", nil, nil,
false, http.HandlerFunc(pprof.Index))
r.RegisterRoute("/debug/pprof/cmdline", nil, []string{http.MethodGet},
r.RegisterRoute("/debug/pprof/cmdline", nil, nil,
false, http.HandlerFunc(pprof.Cmdline))
r.RegisterRoute("/debug/pprof/profile", nil, []string{http.MethodGet},
r.RegisterRoute("/debug/pprof/profile", nil, nil,
false, http.HandlerFunc(pprof.Profile))
r.RegisterRoute("/debug/pprof/symbol", nil, []string{http.MethodGet},
r.RegisterRoute("/debug/pprof/symbol", nil, nil,
false, http.HandlerFunc(pprof.Symbol))
r.RegisterRoute("/debug/pprof/trace", nil, []string{http.MethodGet},
r.RegisterRoute("/debug/pprof/trace", nil, nil,
false, http.HandlerFunc(pprof.Trace))
}

Expand Down Expand Up @@ -154,8 +154,7 @@ var noCacheBackends = map[string]interface{}{
// RegisterHealthHandler registers the main health handler
func RegisterHealthHandler(router router.Router, path string,
hc healthcheck.HealthChecker) {
router.RegisterRoute(path, []string{""}, []string{http.MethodGet},
false, health.StatusHandler(hc))
router.RegisterRoute(path, nil, nil, false, health.StatusHandler(hc))
}

func registerBackendRoutes(r router.Router, metricsRouter router.Router,
Expand Down Expand Up @@ -205,9 +204,9 @@ func registerBackendRoutes(r router.Router, metricsRouter router.Router,
tl.Pairs{"path": hp, "backendName": o.Name,
"upstreamPath": o.HealthCheck.Path,
"upstreamVerb": o.HealthCheck.Verb})
metricsRouter.RegisterRoute(hp, []string{""},
[]string{http.MethodGet}, false,
http.Handler(middleware.WithResourcesContext(client, o, nil, nil, nil, logger, h)))
metricsRouter.RegisterRoute(hp, nil, nil, false,
http.Handler(middleware.WithResourcesContext(client, o, nil,
nil, nil, logger, h)))
}
}
return nil
Expand Down

0 comments on commit 07a9550

Please sign in to comment.