2021-07-24 17:46:22 +02:00
|
|
|
// ___ ____ ___ ___
|
|
|
|
// \ \ / / | _ | __| \ \ / / || | __ || || _ |
|
|
|
|
// \ \/ / |___ | |__ \ \/ / || |___ || ||___|
|
|
|
|
// \ / | _ | _ | \ / || __ | || ||\\
|
|
|
|
// \/ |___ |___ | \/ || ____| || || \\
|
|
|
|
//
|
|
|
|
// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License
|
|
|
|
// Repo: https://git.pbiernat.dev/golang/vegvisir
|
|
|
|
|
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"vegvisir/pkg/cache"
|
|
|
|
"vegvisir/pkg/config"
|
|
|
|
)
|
|
|
|
|
2021-08-03 00:13:14 +02:00
|
|
|
// Router struct
|
2021-07-24 17:46:22 +02:00
|
|
|
type Router struct {
|
|
|
|
config *config.Config
|
|
|
|
rcm cache.RouteCacheManager // Redis cache
|
|
|
|
rCache map[string]cache.RouteCache // Internal route cache - TMP?
|
|
|
|
}
|
|
|
|
|
2021-08-03 00:13:14 +02:00
|
|
|
// Route struct
|
2021-07-24 17:46:22 +02:00
|
|
|
type Route struct {
|
2021-08-03 00:13:14 +02:00
|
|
|
SourceURL string
|
|
|
|
TargetURL string
|
2021-07-24 17:46:22 +02:00
|
|
|
}
|
|
|
|
|
2021-08-03 00:13:14 +02:00
|
|
|
// NewRouter function
|
|
|
|
func NewRouter(conf *config.Config, cacheDS cache.Datastore, ttl int) *Router {
|
2021-07-24 17:46:22 +02:00
|
|
|
return &Router{
|
|
|
|
config: conf,
|
|
|
|
rcm: cache.NewRouteCacheManager(cacheDS, ttl),
|
|
|
|
rCache: make(map[string]cache.RouteCache),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-03 00:13:14 +02:00
|
|
|
// FindByRequestURL function
|
2021-07-24 17:46:22 +02:00
|
|
|
func (r *Router) FindByRequestURL(url []byte) (bool, Route) {
|
2021-08-03 00:13:14 +02:00
|
|
|
var sURL string = string(url)
|
2021-07-24 17:46:22 +02:00
|
|
|
|
2021-08-03 00:13:14 +02:00
|
|
|
for bID := range r.config.Backends {
|
|
|
|
bck := r.config.Backends[bID]
|
|
|
|
if !strings.Contains(sURL, bck.PrefixURL) {
|
2021-07-24 17:46:22 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-08-03 00:13:14 +02:00
|
|
|
for rID := range bck.Routes {
|
|
|
|
routeCfg := &bck.Routes[rID]
|
2021-07-24 17:46:22 +02:00
|
|
|
|
|
|
|
// if ok, cRoute := s.rCacheManager.Load(sUri); ok {
|
|
|
|
// return true, cRoute
|
|
|
|
// }
|
2021-08-03 00:13:14 +02:00
|
|
|
if cRoute, ok := r.rCache[sURL]; ok {
|
2021-07-24 17:46:22 +02:00
|
|
|
route := Route{
|
2021-08-03 00:13:14 +02:00
|
|
|
SourceURL: cRoute.SourceURL,
|
|
|
|
TargetURL: cRoute.TargetURL,
|
2021-07-24 17:46:22 +02:00
|
|
|
}
|
|
|
|
return true, route
|
|
|
|
}
|
|
|
|
|
2021-08-03 00:13:14 +02:00
|
|
|
rgxp := regexp.MustCompile(fmt.Sprintf("%s%s", bck.PrefixURL, routeCfg.Pattern))
|
2021-07-24 17:46:22 +02:00
|
|
|
if rgxp.Match(url) {
|
2021-08-03 00:13:14 +02:00
|
|
|
targetURL := bck.BackendAddress + rgxp.ReplaceAllString(sURL, routeCfg.Target)
|
2021-07-24 17:46:22 +02:00
|
|
|
|
2021-08-03 00:13:14 +02:00
|
|
|
route := Route{ // FIXME: duplicated #1
|
|
|
|
SourceURL: sURL,
|
|
|
|
TargetURL: targetURL,
|
2021-07-24 17:46:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// s.rCacheManager.Save(sUri, cRoute)
|
2021-08-03 00:13:14 +02:00
|
|
|
r.rCache[sURL] = cache.RouteCache{ // FIXME: duplicated #1
|
|
|
|
SourceURL: route.SourceURL,
|
|
|
|
TargetURL: route.TargetURL,
|
2021-07-24 17:46:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true, route
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, Route{}
|
|
|
|
}
|