Skip to content

Commit

Permalink
relocate Route to own pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
jranson committed Aug 12, 2024
1 parent a99c568 commit 231fbae
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 55 deletions.
53 changes: 28 additions & 25 deletions pkg/router/lm/lm.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,27 @@ import (
"github.com/trickstercache/trickster/v2/pkg/proxy/headers"
meth "github.com/trickstercache/trickster/v2/pkg/proxy/methods"
"github.com/trickstercache/trickster/v2/pkg/router"
"github.com/trickstercache/trickster/v2/pkg/router/route"
)

var _ router.Router = &rtr{}
var _ router.Router = &lmRouter{}

type rtr struct {
type lmRouter struct {
matchScheme router.MatchingScheme
routes router.HostRouteSetLookup
routes route.HostRouteSetLookup
}

func NewRouter() router.Router {
return &rtr{
return &lmRouter{
matchScheme: router.DefaultMatchingScheme,
routes: make(router.HostRouteSetLookup),
routes: make(route.HostRouteSetLookup),
}
}

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

func (rt *rtr) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (rt *lmRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == "*" {
if r.ProtoAtLeast(1, 1) {
w.Header().Set(headers.NameConnection, headers.ValueClose)
Expand All @@ -41,7 +42,7 @@ func (rt *rtr) ServeHTTP(w http.ResponseWriter, r *http.Request) {
rt.Handler(r).ServeHTTP(w, r)
}

func (rt *rtr) RegisterRoute(path string, hosts, methods []string,
func (rt *lmRouter) RegisterRoute(path string, hosts, methods []string,
matchPrefix bool, handler http.Handler) error {
pl := len(path)
if pl == 0 {
Expand All @@ -63,21 +64,21 @@ func (rt *rtr) RegisterRoute(path string, hosts, methods []string,
for _, h := range hosts {
hrc, ok := rt.routes[h]
if !ok || hrc == nil {
hrc = &router.HostRouteSet{
ExactMatchRoutes: make(router.RouteLookupLookup),
PrefixMatchRoutes: make(router.PrefixRouteSets, 0, 16),
PrefixMatchRoutesLkp: make(router.PrefixRouteSetLookup),
hrc = &route.HostRouteSet{
ExactMatchRoutes: make(route.RouteLookupLookup),
PrefixMatchRoutes: make(route.PrefixRouteSets, 0, 16),
PrefixMatchRoutesLkp: make(route.PrefixRouteSetLookup),
}
rt.routes[h] = hrc
}
if !matchPrefix {
rl, ok := hrc.ExactMatchRoutes[path]
if rl == nil || !ok {
rl = make(router.RouteLookup)
rl = make(route.RouteLookup)
hrc.ExactMatchRoutes[path] = rl
}
for _, m := range methods {
rl[m] = &router.Route{
rl[m] = &route.Route{
ExactMatch: true,
Method: m,
Host: h,
Expand All @@ -86,7 +87,7 @@ func (rt *rtr) RegisterRoute(path string, hosts, methods []string,
}
if m == http.MethodGet {
if _, ok := rl[http.MethodHead]; !ok {
rl[http.MethodHead] = &router.Route{
rl[http.MethodHead] = &route.Route{
ExactMatch: true,
Method: http.MethodHead,
Host: h,
Expand All @@ -100,19 +101,19 @@ func (rt *rtr) RegisterRoute(path string, hosts, methods []string,
}
prc, ok := hrc.PrefixMatchRoutesLkp[path]
if prc == nil || !ok {
prc = &router.PrefixRouteSet{
prc = &route.PrefixRouteSet{
Path: path,
PathLen: pl,
RoutesByMethod: make(router.RouteLookup),
RoutesByMethod: make(route.RouteLookup),
}
hrc.PrefixMatchRoutesLkp[path] = prc
if len(hrc.PrefixMatchRoutes) == 0 {
hrc.PrefixMatchRoutes = make(router.PrefixRouteSets, 0, 16)
hrc.PrefixMatchRoutes = make(route.PrefixRouteSets, 0, 16)
}
hrc.PrefixMatchRoutes = append(hrc.PrefixMatchRoutes, prc)
}
for _, m := range methods {
prc.RoutesByMethod[m] = &router.Route{
prc.RoutesByMethod[m] = &route.Route{
ExactMatch: true,
Method: m,
Host: h,
Expand All @@ -121,7 +122,7 @@ func (rt *rtr) RegisterRoute(path string, hosts, methods []string,
}
if m == http.MethodGet {
if _, ok := prc.RoutesByMethod[http.MethodHead]; !ok {
prc.RoutesByMethod[http.MethodHead] = &router.Route{
prc.RoutesByMethod[http.MethodHead] = &route.Route{
ExactMatch: true,
Method: http.MethodHead,
Host: h,
Expand All @@ -137,19 +138,19 @@ func (rt *rtr) RegisterRoute(path string, hosts, methods []string,
}

// this sorts the prefix-match paths longest to shortest
func (rt *rtr) sort() {
func (rt *lmRouter) sort() {
for _, hrc := range rt.routes {
if len(hrc.PrefixMatchRoutes) == 0 {
continue
}
prs := prefixRouteSets(hrc.PrefixMatchRoutes)
sort.Sort(prs)
slices.Reverse(prs)
hrc.PrefixMatchRoutes = router.PrefixRouteSets(prs)
hrc.PrefixMatchRoutes = route.PrefixRouteSets(prs)
}
}

func (rt *rtr) Handler(r *http.Request) http.Handler {
func (rt *lmRouter) Handler(r *http.Request) http.Handler {
if rt.matchScheme&router.MatchHostname == router.MatchHostname {
host := r.Host
i := strings.Index(host, ":")
Expand All @@ -168,7 +169,7 @@ func (rt *rtr) Handler(r *http.Request) http.Handler {
return notFoundHandler
}

func (rt *rtr) matchByHost(method, host, path string) http.Handler {
func (rt *lmRouter) matchByHost(method, host, path string) http.Handler {
if hrc, ok := rt.routes[host]; ok && hrc != nil {
if rs, ok := hrc.ExactMatchRoutes[path]; ok && rs != nil {
r, ok := rs[method]
Expand Down Expand Up @@ -197,7 +198,7 @@ func (rt *rtr) matchByHost(method, host, path string) http.Handler {
return nil
}

func (rt *rtr) SetMatchingScheme(s router.MatchingScheme) {
func (rt *lmRouter) SetMatchingScheme(s router.MatchingScheme) {
rt.matchScheme = s
}

Expand All @@ -208,7 +209,9 @@ func MethodNotAllowed(w http.ResponseWriter, r *http.Request) {
var methodNotAllowedHandler = http.HandlerFunc(MethodNotAllowed)
var notFoundHandler = http.HandlerFunc(http.NotFound)

type prefixRouteSets router.PrefixRouteSets
// prefixRouteSets allows the route.PrefixRouteSets to be sorted by path from
// longest-to-shortest using sort.Interface
type prefixRouteSets route.PrefixRouteSets

func (prs prefixRouteSets) Len() int {
return len(prs)
Expand Down
33 changes: 33 additions & 0 deletions pkg/router/route/route.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package route

import "net/http"

type Route struct {
ExactMatch bool
Method string
Host string
Path string
Handler http.Handler
}

type Routes []*Route

type RouteLookup map[string]*Route
type RouteLookupLookup map[string]RouteLookup

type PrefixRouteSet struct {
Path string
PathLen int
RoutesByMethod RouteLookup
}

type PrefixRouteSets []*PrefixRouteSet
type PrefixRouteSetLookup map[string]*PrefixRouteSet

type HostRouteSet struct {
ExactMatchRoutes RouteLookupLookup
PrefixMatchRoutes PrefixRouteSets
PrefixMatchRoutesLkp PrefixRouteSetLookup
}

type HostRouteSetLookup map[string]*HostRouteSet
30 changes: 0 additions & 30 deletions pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,3 @@ const (

DefaultMatchingScheme MatchingScheme = MatchHostname | MatchPathPrefix
)

type Route struct {
ExactMatch bool
Method string
Host string
Path string
Handler http.Handler
}

type Routes []*Route

type RouteLookup map[string]*Route
type RouteLookupLookup map[string]RouteLookup

type PrefixRouteSet struct {
Path string
PathLen int
RoutesByMethod RouteLookup
}

type PrefixRouteSets []*PrefixRouteSet
type PrefixRouteSetLookup map[string]*PrefixRouteSet

type HostRouteSet struct {
ExactMatchRoutes RouteLookupLookup
PrefixMatchRoutes PrefixRouteSets
PrefixMatchRoutesLkp PrefixRouteSetLookup
}

type HostRouteSetLookup map[string]*HostRouteSet

0 comments on commit 231fbae

Please sign in to comment.