Compare commits
No commits in common. "80a0cba90fb556ad6bf2f78af01e90947a572e7f" and "a37e605291b8d3162f1417b036f0bc0664a802f7" have entirely different histories.
80a0cba90f
...
a37e605291
2
pkg/cache/datastore.go
vendored
2
pkg/cache/datastore.go
vendored
@ -13,6 +13,4 @@ type CacheDatastore interface {
|
|||||||
SetKey(string, interface{}, int) error
|
SetKey(string, interface{}, int) error
|
||||||
|
|
||||||
GetKey(string) (interface{}, error)
|
GetKey(string) (interface{}, error)
|
||||||
|
|
||||||
IsConnected() bool
|
|
||||||
}
|
}
|
||||||
|
42
pkg/cache/memory_datastore.go
vendored
42
pkg/cache/memory_datastore.go
vendored
@ -1,42 +0,0 @@
|
|||||||
// ___ ____ ___ ___
|
|
||||||
// \ \ / / | _ | __| \ \ / / || | __ || || _ |
|
|
||||||
// \ \/ / |___ | |__ \ \/ / || |___ || ||___|
|
|
||||||
// \ / | _ | _ | \ / || __ | || ||\\
|
|
||||||
// \/ |___ |___ | \/ || ____| || || \\
|
|
||||||
//
|
|
||||||
// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License
|
|
||||||
// Repo: https://git.pbiernat.dev/golang/vegvisir
|
|
||||||
|
|
||||||
package cache
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
)
|
|
||||||
|
|
||||||
func NewMemoryDatastore() *MemoryDatastore {
|
|
||||||
return &MemoryDatastore{
|
|
||||||
cache: make(map[string]interface{}),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type MemoryDatastore struct {
|
|
||||||
cache map[string]interface{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ds *MemoryDatastore) SetKey(key string, data interface{}, ttl int) error {
|
|
||||||
ds.cache[key] = data
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ds *MemoryDatastore) GetKey(key string) (interface{}, error) {
|
|
||||||
if data, ok := ds.cache[key]; ok {
|
|
||||||
return data, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, errors.New("Key not found" + key)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ds *MemoryDatastore) IsConnected() bool {
|
|
||||||
return true
|
|
||||||
}
|
|
14
pkg/cache/redis_datastore.go
vendored
14
pkg/cache/redis_datastore.go
vendored
@ -23,11 +23,13 @@ func NewRedisDatastore(host string, port int) *RedisDatastore {
|
|||||||
Password: "", // FIXME: use env or param
|
Password: "", // FIXME: use env or param
|
||||||
DB: 0, // FIXME: use env or param
|
DB: 0, // FIXME: use env or param
|
||||||
}),
|
}),
|
||||||
|
cache: make(map[string]interface{}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type RedisDatastore struct {
|
type RedisDatastore struct {
|
||||||
client *redis.Client
|
client *redis.Client
|
||||||
|
cache map[string]interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ds *RedisDatastore) SetKey(key string, data interface{}, ttl int) error {
|
func (ds *RedisDatastore) SetKey(key string, data interface{}, ttl int) error {
|
||||||
@ -36,10 +38,16 @@ func (ds *RedisDatastore) SetKey(key string, data interface{}, ttl int) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ds.cache[key] = data
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ds *RedisDatastore) GetKey(key string) (interface{}, error) {
|
func (ds *RedisDatastore) GetKey(key string) (interface{}, error) {
|
||||||
|
// if data, ok := ds.cache[key]; ok {
|
||||||
|
// return data, nil
|
||||||
|
// }
|
||||||
|
|
||||||
data, err := ds.client.Get(key).Result()
|
data, err := ds.client.Get(key).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -47,9 +55,3 @@ func (ds *RedisDatastore) GetKey(key string) (interface{}, error) {
|
|||||||
|
|
||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ds *RedisDatastore) IsConnected() bool {
|
|
||||||
_, err := ds.client.Ping().Result()
|
|
||||||
|
|
||||||
return err == nil
|
|
||||||
}
|
|
||||||
|
24
pkg/cache/response.go
vendored
24
pkg/cache/response.go
vendored
@ -34,35 +34,22 @@ func NewResponseCacheManager(datastore CacheDatastore, ttl int) ResponseCacheMan
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rm *ResponseCacheManager) Save(name string, r ResponseCache) bool {
|
func (rm *ResponseCacheManager) Save(name string, r ResponseCache) {
|
||||||
if !rm.datastore.IsConnected() {
|
|
||||||
log.Println("Response-cache:", "Not connected to server")
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
data, err := json.Marshal(r)
|
data, err := json.Marshal(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Response-cache", "JSON encode:", err)
|
log.Println("JSON:", err) // FIXME
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
name = rm.prefix + name
|
name = rm.prefix + name
|
||||||
err = rm.datastore.SetKey(name, string(data), rm.ttl)
|
err = rm.datastore.SetKey(name, string(data), rm.ttl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Response-cache:", err, name) // FIXME
|
log.Println("REDIS:", err, name) // FIXME
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rm *ResponseCacheManager) Load(name string) (bool, *ResponseCache) {
|
func (rm *ResponseCacheManager) Load(name string) (bool, *ResponseCache) {
|
||||||
if !rm.datastore.IsConnected() {
|
|
||||||
log.Println("Response-cache:", "Not connected to server")
|
|
||||||
return false, &ResponseCache{}
|
|
||||||
}
|
|
||||||
|
|
||||||
name = rm.prefix + name
|
name = rm.prefix + name
|
||||||
|
|
||||||
data, err := rm.datastore.GetKey(name)
|
data, err := rm.datastore.GetKey(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, &ResponseCache{}
|
return false, &ResponseCache{}
|
||||||
@ -71,8 +58,7 @@ func (rm *ResponseCacheManager) Load(name string) (bool, *ResponseCache) {
|
|||||||
rc := &ResponseCache{}
|
rc := &ResponseCache{}
|
||||||
err = json.Unmarshal([]byte(data.(string)), &rc)
|
err = json.Unmarshal([]byte(data.(string)), &rc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Response-cache:", "JSON docode:", err)
|
log.Println("JSON:", err) // FIXME
|
||||||
return false, &ResponseCache{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true, rc
|
return true, rc
|
||||||
|
38
pkg/cache/route.go
vendored
38
pkg/cache/route.go
vendored
@ -21,58 +21,42 @@ type RouteCache struct {
|
|||||||
|
|
||||||
type RouteCacheManager struct {
|
type RouteCacheManager struct {
|
||||||
datastore CacheDatastore
|
datastore CacheDatastore
|
||||||
prefix string
|
|
||||||
ttl int
|
ttl int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRouteCacheManager(datastore CacheDatastore, ttl int) RouteCacheManager {
|
func NewRouteCacheManager(datastore CacheDatastore, ttl int) RouteCacheManager {
|
||||||
return RouteCacheManager{
|
return RouteCacheManager{
|
||||||
datastore: datastore,
|
datastore: datastore,
|
||||||
prefix: "route_",
|
|
||||||
ttl: ttl,
|
ttl: ttl,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rm *RouteCacheManager) Save(name string, r RouteCache) bool {
|
func (rm *RouteCacheManager) Save(name string, r RouteCache) {
|
||||||
if !rm.datastore.IsConnected() {
|
|
||||||
log.Println("Route-cache:", "Not connected to server")
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
data, err := json.Marshal(r)
|
data, err := json.Marshal(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Route-cache", "JSON encode:", err)
|
log.Println("JSON:", err) // FIXME
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
name = rm.prefix + name
|
err = rm.datastore.SetKey("route_"+name, data, rm.ttl)
|
||||||
err = rm.datastore.SetKey(name, data, rm.ttl)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Route-cache:", "Unable to save", name, err)
|
log.Println("REDIS:", err, name) // FIXME
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rm *RouteCacheManager) Load(name string) (bool, *RouteCache) {
|
func (rm *RouteCacheManager) Load(name string) (bool, RouteCache) {
|
||||||
if !rm.datastore.IsConnected() {
|
name = "route_" + name
|
||||||
log.Println("Route-cache:", "Not connected to server")
|
|
||||||
return false, &RouteCache{}
|
|
||||||
}
|
|
||||||
|
|
||||||
name = rm.prefix + name
|
|
||||||
data, err := rm.datastore.GetKey(name)
|
data, err := rm.datastore.GetKey(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Route-cache:", "Unable to load", name, err)
|
log.Println("REDIS:", err, name) // FIXME
|
||||||
return false, &RouteCache{}
|
|
||||||
|
return false, RouteCache{}
|
||||||
}
|
}
|
||||||
|
|
||||||
rc := &RouteCache{}
|
rc := RouteCache{}
|
||||||
err = json.Unmarshal([]byte(data.(string)), &rc)
|
err = json.Unmarshal([]byte(data.(string)), &rc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Route-cache:", "JSON decode:", err)
|
log.Println("JSON:", err) // FIXME
|
||||||
return false, &RouteCache{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true, rc
|
return true, rc
|
||||||
|
@ -18,8 +18,6 @@ import (
|
|||||||
type Config struct {
|
type Config struct {
|
||||||
Server Server
|
Server Server
|
||||||
Backends map[string]Backend
|
Backends map[string]Backend
|
||||||
|
|
||||||
confPath string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
@ -44,18 +42,12 @@ var DefaultRoute = Route{
|
|||||||
Target: "",
|
Target: "",
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(confPath string) *Config {
|
func (c *Config) Load(cPath string) error {
|
||||||
return &Config{
|
if _, err := os.Stat(cPath); err != nil {
|
||||||
confPath: confPath,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Config) Load() error {
|
|
||||||
if _, err := os.Stat(c.confPath); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := ioutil.ReadFile(c.confPath)
|
data, err := ioutil.ReadFile(cPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -1,10 +0,0 @@
|
|||||||
// ___ ____ ___ ___
|
|
||||||
// \ \ / / | _ | __| \ \ / / || | __ || || _ |
|
|
||||||
// \ \/ / |___ | |__ \ \/ / || |___ || ||___|
|
|
||||||
// \ / | _ | _ | \ / || __ | || ||\\
|
|
||||||
// \/ |___ |___ | \/ || ____| || || \\
|
|
||||||
//
|
|
||||||
// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License
|
|
||||||
// Repo: https://git.pbiernat.dev/golang/vegvisir
|
|
||||||
|
|
||||||
package server
|
|
@ -1,83 +0,0 @@
|
|||||||
// ___ ____ ___ ___
|
|
||||||
// \ \ / / | _ | __| \ \ / / || | __ || || _ |
|
|
||||||
// \ \/ / |___ | |__ \ \/ / || |___ || ||___|
|
|
||||||
// \ / | _ | _ | \ / || __ | || ||\\
|
|
||||||
// \/ |___ |___ | \/ || ____| || || \\
|
|
||||||
//
|
|
||||||
// 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"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Router struct {
|
|
||||||
config *config.Config
|
|
||||||
rcm cache.RouteCacheManager // Redis cache
|
|
||||||
rCache map[string]cache.RouteCache // Internal route cache - TMP?
|
|
||||||
}
|
|
||||||
|
|
||||||
type Route struct {
|
|
||||||
SourceUrl string
|
|
||||||
TargetUrl string
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewRouter(conf *config.Config, cacheDS cache.CacheDatastore, ttl int) *Router {
|
|
||||||
return &Router{
|
|
||||||
config: conf,
|
|
||||||
rcm: cache.NewRouteCacheManager(cacheDS, ttl),
|
|
||||||
rCache: make(map[string]cache.RouteCache),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Router) FindByRequestURL(url []byte) (bool, Route) {
|
|
||||||
var sUrl string = string(url)
|
|
||||||
|
|
||||||
for bId := range r.config.Backends {
|
|
||||||
bck := r.config.Backends[bId]
|
|
||||||
if !strings.Contains(sUrl, bck.PrefixUrl) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
for rId := range bck.Routes {
|
|
||||||
routeCfg := &bck.Routes[rId]
|
|
||||||
|
|
||||||
// if ok, cRoute := s.rCacheManager.Load(sUri); ok {
|
|
||||||
// return true, cRoute
|
|
||||||
// }
|
|
||||||
if cRoute, ok := r.rCache[sUrl]; ok {
|
|
||||||
route := Route{
|
|
||||||
SourceUrl: cRoute.SourceUrl,
|
|
||||||
TargetUrl: cRoute.TargetUrl,
|
|
||||||
}
|
|
||||||
return true, route
|
|
||||||
}
|
|
||||||
|
|
||||||
rgxp := regexp.MustCompile(fmt.Sprintf("%s%s", bck.PrefixUrl, routeCfg.Pattern))
|
|
||||||
if rgxp.Match(url) {
|
|
||||||
targetUrl := bck.BackendAddress + rgxp.ReplaceAllString(sUrl, routeCfg.Target)
|
|
||||||
|
|
||||||
route := Route{
|
|
||||||
SourceUrl: sUrl,
|
|
||||||
TargetUrl: targetUrl,
|
|
||||||
}
|
|
||||||
|
|
||||||
// s.rCacheManager.Save(sUri, cRoute)
|
|
||||||
r.rCache[sUrl] = cache.RouteCache{
|
|
||||||
SourceUrl: route.SourceUrl,
|
|
||||||
TargetUrl: route.TargetUrl,
|
|
||||||
}
|
|
||||||
|
|
||||||
return true, route
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false, Route{}
|
|
||||||
}
|
|
@ -15,6 +15,8 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"vegvisir/pkg/cache"
|
"vegvisir/pkg/cache"
|
||||||
"vegvisir/pkg/config"
|
"vegvisir/pkg/config"
|
||||||
@ -28,40 +30,32 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
config *config.Config
|
Config config.Config
|
||||||
router *Router
|
|
||||||
|
cFilePath string
|
||||||
|
rCache map[string]cache.RouteCache // Internal route cache
|
||||||
|
routeCM cache.RouteCacheManager // Redis route cache
|
||||||
respCM cache.ResponseCacheManager // Redis response cache
|
respCM cache.ResponseCacheManager // Redis response cache
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer(cPath string) *Server {
|
func NewServer(cPath string) *Server {
|
||||||
config := config.New(cPath)
|
datastore := cache.NewRedisDatastore("127.0.0.7", 6379) // FIXME use config or env...
|
||||||
|
|
||||||
if err := config.Load(); err != nil {
|
return &Server{
|
||||||
log.Fatalln("Unable to find config file: ", cPath, err)
|
cFilePath: cPath,
|
||||||
|
rCache: make(map[string]cache.RouteCache),
|
||||||
|
routeCM: cache.NewRouteCacheManager(datastore, 30), //FIXME use ttl(seconds) from config or env...
|
||||||
|
respCM: cache.NewResponseCacheManager(datastore, 30), //FIXME use ttl(seconds) from config or env...
|
||||||
}
|
}
|
||||||
|
|
||||||
server := &Server{
|
|
||||||
config: config,
|
|
||||||
}
|
|
||||||
|
|
||||||
redisDS := cache.NewRedisDatastore("127.0.0.1", 6379) // FIXME use config or env...
|
|
||||||
if !redisDS.IsConnected() {
|
|
||||||
log.Println("Redis server not responding, switching to memory cache...")
|
|
||||||
|
|
||||||
memDS := cache.NewMemoryDatastore()
|
|
||||||
server.router = NewRouter(config, memDS, 30) //FIXME for memory datasource ttl is useles right now...
|
|
||||||
server.respCM = cache.NewResponseCacheManager(memDS, 30) //FIXME for memory datasource ttl is useles right now...
|
|
||||||
} else {
|
|
||||||
server.router = NewRouter(config, redisDS, 30) //FIXME use ttl(seconds) from config or env...
|
|
||||||
server.respCM = cache.NewResponseCacheManager(redisDS, 30) //FIXME use ttl(seconds) from config or env...
|
|
||||||
}
|
|
||||||
|
|
||||||
return server
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Run() {
|
func (s *Server) Run() {
|
||||||
|
if err := s.Config.Load(s.cFilePath); err != nil {
|
||||||
|
log.Fatalln("Unable to find config file: ", s.cFilePath, err)
|
||||||
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
serverAddress := s.config.Server.Address + ":" + fmt.Sprint(s.config.Server.Port)
|
serverAddress := s.Config.Server.Address + ":" + fmt.Sprint(s.Config.Server.Port)
|
||||||
if err := fasthttp.ListenAndServe(serverAddress, s.mainHandler); err != nil {
|
if err := fasthttp.ListenAndServe(serverAddress, s.mainHandler); err != nil {
|
||||||
log.Fatalf("Server panic! Error message: %s", err)
|
log.Fatalf("Server panic! Error message: %s", err)
|
||||||
}
|
}
|
||||||
@ -92,10 +86,10 @@ func (s *Server) mainHandler(ctx *fasthttp.RequestCtx) {
|
|||||||
ctx.Response.Header.Add(fasthttp.HeaderServer, Name)
|
ctx.Response.Header.Add(fasthttp.HeaderServer, Name)
|
||||||
|
|
||||||
// move all below logic to concrete handler or sth....
|
// move all below logic to concrete handler or sth....
|
||||||
reqUrl, sReqUrl, sReqMethod := ctx.RequestURI(), string(ctx.RequestURI()), string(ctx.Method())
|
reqUri, sReqUri, sReqMethod := ctx.RequestURI(), string(ctx.RequestURI()), string(ctx.Method())
|
||||||
log.Println("Incomming request:", sReqMethod, sReqUrl)
|
log.Println("Incomming request:", sReqMethod, sReqUri)
|
||||||
|
|
||||||
found, route := s.router.FindByRequestURL(reqUrl)
|
found, route := s.findRouteByRequestURI(reqUri)
|
||||||
if !found {
|
if !found {
|
||||||
// FIXME: return 404/5xx error in repsonse, maybe define it in Backend config?
|
// FIXME: return 404/5xx error in repsonse, maybe define it in Backend config?
|
||||||
ctx.SetStatusCode(fasthttp.StatusNotFound)
|
ctx.SetStatusCode(fasthttp.StatusNotFound)
|
||||||
@ -103,10 +97,10 @@ func (s *Server) mainHandler(ctx *fasthttp.RequestCtx) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// handle response caching
|
// handle response caching
|
||||||
if ok, data := s.respCM.Load(sReqUrl); ok {
|
if ok, data := s.respCM.Load(sReqUri); ok {
|
||||||
log.Println("Read resp from cache: ", route.TargetUrl)
|
log.Println("Read resp from cache: ", route.TargetUrl)
|
||||||
|
|
||||||
ctx.SetBody([]byte(data.Body)) // FIXME missing headers etc...
|
ctx.SetBody([]byte(data.Body))
|
||||||
} else {
|
} else {
|
||||||
log.Println("Send req to backend url: ", route.TargetUrl)
|
log.Println("Send req to backend url: ", route.TargetUrl)
|
||||||
|
|
||||||
@ -132,10 +126,51 @@ func (s *Server) mainHandler(ctx *fasthttp.RequestCtx) {
|
|||||||
|
|
||||||
// save response to cache
|
// save response to cache
|
||||||
respCache := cache.ResponseCache{
|
respCache := cache.ResponseCache{
|
||||||
URL: sReqUrl,
|
URL: sReqUri,
|
||||||
Body: string(bckResp.Body()),
|
Body: string(bckResp.Body()),
|
||||||
// Headers: []
|
// Headers: []
|
||||||
} // FIXME: prepare resp cache struct in respCM.Save method or other service...
|
} // FIXME: prepare resp cache struct in respCM.Save method or other service...
|
||||||
s.respCM.Save(sReqUrl, respCache)
|
s.respCM.Save(sReqUri, respCache)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Server) findRouteByRequestURI(uri []byte) (bool, cache.RouteCache) {
|
||||||
|
var sUri string = string(uri)
|
||||||
|
|
||||||
|
for bId := range s.Config.Backends {
|
||||||
|
bck := s.Config.Backends[bId]
|
||||||
|
if !strings.Contains(sUri, bck.PrefixUrl) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
for rId := range bck.Routes {
|
||||||
|
route := &bck.Routes[rId]
|
||||||
|
|
||||||
|
// if ok, cRoute := s.rCacheManager.Load(sUri); ok {
|
||||||
|
// return true, cRoute
|
||||||
|
// }
|
||||||
|
if cRoute, ok := s.rCache[sUri]; ok {
|
||||||
|
return true, cRoute
|
||||||
|
}
|
||||||
|
|
||||||
|
rgxp := regexp.MustCompile(fmt.Sprintf("%s%s", bck.PrefixUrl, route.Pattern))
|
||||||
|
if rgxp.Match(uri) {
|
||||||
|
targetUrl := bck.BackendAddress + rgxp.ReplaceAllString(sUri, route.Target)
|
||||||
|
|
||||||
|
cRoute := cache.RouteCache{ // FIXME: data duplication and use short alias for backend and route!
|
||||||
|
// Backend: bck,
|
||||||
|
// Route: *route,
|
||||||
|
SourceUrl: sUri,
|
||||||
|
TargetUrl: targetUrl,
|
||||||
|
}
|
||||||
|
|
||||||
|
// s.rCacheManager.Save(sUri, cRoute)
|
||||||
|
s.rCache[sUri] = cRoute
|
||||||
|
|
||||||
|
return true, cRoute
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, cache.RouteCache{}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user