From a37e605291b8d3162f1417b036f0bc0664a802f7 Mon Sep 17 00:00:00 2001 From: Piotr Biernat Date: Fri, 23 Jul 2021 18:42:18 +0200 Subject: [PATCH 1/7] Updated server version const --- go.sum | 5 +++++ pkg/server/server.go | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/go.sum b/go.sum index 35d2356..29ee320 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,9 @@ github.com/andybalholm/brotli v1.0.2/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y= +github.com/andybalholm/brotli v1.0.3 h1:fpcw+r1N1h0Poc1F/pHbW40cUm/lMEQslZtCkBQ0UnM= github.com/andybalholm/brotli v1.0.3/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= +github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg= github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= @@ -17,6 +19,7 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/klauspost/compress v1.12.2/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= +github.com/klauspost/compress v1.13.1 h1:wXr2uRxZTJXHLly6qhJabee5JqIhTRoLBhDOA74hDEQ= github.com/klauspost/compress v1.13.1/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= @@ -25,7 +28,9 @@ github.com/onsi/ginkgo v1.15.0/go.mod h1:hF8qUzuuC8DJGygJH3726JnCZX4MYbRB8yFfISq github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7mt48= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasthttp v1.28.0 h1:ruVmTmZaBR5i67NqnjvvH5gEv0zwHfWtbjoyW98iho4= github.com/valyala/fasthttp v1.28.0/go.mod h1:cmWIqlu99AO/RKcp1HWaViTqc57FswJOfYYdPJBl8BA= github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= diff --git a/pkg/server/server.go b/pkg/server/server.go index ee9f769..d61b2fc 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -25,7 +25,7 @@ import ( ) const ( - Version = "0.1" + Version = "0.1-dev" Name = "Vegvisir/" + Version ) From a0f162b91c5814eef927eb5d3a45b6a53efeeebf Mon Sep 17 00:00:00 2001 From: Piotr Biernat Date: Sat, 24 Jul 2021 17:46:22 +0200 Subject: [PATCH 2/7] Rebuild of cache module: - Added new "fail-safe" memory-datasource - Moved some logic from Server to Router --- pkg/cache/datastore.go | 2 + pkg/cache/memory_datastore.go | 42 +++++++++++++++ pkg/cache/redis_datastore.go | 14 +++-- pkg/cache/response.go | 24 +++++++-- pkg/cache/route.go | 44 +++++++++++----- pkg/config/config.go | 14 +++-- pkg/server/response.go | 10 ++++ pkg/server/router.go | 83 +++++++++++++++++++++++++++++ pkg/server/server.go | 99 +++++++++++------------------------ 9 files changed, 235 insertions(+), 97 deletions(-) create mode 100644 pkg/cache/memory_datastore.go create mode 100644 pkg/server/response.go create mode 100644 pkg/server/router.go diff --git a/pkg/cache/datastore.go b/pkg/cache/datastore.go index f61593d..9208916 100644 --- a/pkg/cache/datastore.go +++ b/pkg/cache/datastore.go @@ -13,4 +13,6 @@ type CacheDatastore interface { SetKey(string, interface{}, int) error GetKey(string) (interface{}, error) + + IsConnected() bool } diff --git a/pkg/cache/memory_datastore.go b/pkg/cache/memory_datastore.go new file mode 100644 index 0000000..7c2a997 --- /dev/null +++ b/pkg/cache/memory_datastore.go @@ -0,0 +1,42 @@ +// ___ ____ ___ ___ +// \ \ / / | _ | __| \ \ / / || | __ || || _ | +// \ \/ / |___ | |__ \ \/ / || |___ || ||___| +// \ / | _ | _ | \ / || __ | || ||\\ +// \/ |___ |___ | \/ || ____| || || \\ +// +// 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 +} diff --git a/pkg/cache/redis_datastore.go b/pkg/cache/redis_datastore.go index 4fac007..bf84819 100644 --- a/pkg/cache/redis_datastore.go +++ b/pkg/cache/redis_datastore.go @@ -23,13 +23,11 @@ func NewRedisDatastore(host string, port int) *RedisDatastore { Password: "", // FIXME: use env or param DB: 0, // FIXME: use env or param }), - cache: make(map[string]interface{}), } } type RedisDatastore struct { client *redis.Client - cache map[string]interface{} } func (ds *RedisDatastore) SetKey(key string, data interface{}, ttl int) error { @@ -38,16 +36,10 @@ func (ds *RedisDatastore) SetKey(key string, data interface{}, ttl int) error { return err } - // ds.cache[key] = data - return nil } 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() if err != nil { return nil, err @@ -55,3 +47,9 @@ func (ds *RedisDatastore) GetKey(key string) (interface{}, error) { return data, nil } + +func (ds *RedisDatastore) IsConnected() bool { + _, err := ds.client.Ping().Result() + + return err == nil +} diff --git a/pkg/cache/response.go b/pkg/cache/response.go index c213157..52562e4 100644 --- a/pkg/cache/response.go +++ b/pkg/cache/response.go @@ -34,22 +34,35 @@ func NewResponseCacheManager(datastore CacheDatastore, ttl int) ResponseCacheMan } } -func (rm *ResponseCacheManager) Save(name string, r ResponseCache) { +func (rm *ResponseCacheManager) Save(name string, r ResponseCache) bool { + if !rm.datastore.IsConnected() { + log.Println("Response-cache:", "Not connected to server") + return false + } + data, err := json.Marshal(r) if err != nil { - log.Println("JSON:", err) // FIXME + log.Println("Response-cache", "JSON encode:", err) + return false } name = rm.prefix + name err = rm.datastore.SetKey(name, string(data), rm.ttl) if err != nil { - log.Println("REDIS:", err, name) // FIXME + log.Println("Response-cache:", err, name) // FIXME + return false } + + return true } func (rm *ResponseCacheManager) Load(name string) (bool, *ResponseCache) { - name = rm.prefix + name + if !rm.datastore.IsConnected() { + log.Println("Response-cache:", "Not connected to server") + return false, &ResponseCache{} + } + name = rm.prefix + name data, err := rm.datastore.GetKey(name) if err != nil { return false, &ResponseCache{} @@ -58,7 +71,8 @@ func (rm *ResponseCacheManager) Load(name string) (bool, *ResponseCache) { rc := &ResponseCache{} err = json.Unmarshal([]byte(data.(string)), &rc) if err != nil { - log.Println("JSON:", err) // FIXME + log.Println("Response-cache:", "JSON docode:", err) + return false, &ResponseCache{} } return true, rc diff --git a/pkg/cache/route.go b/pkg/cache/route.go index ff28a1c..b79820f 100644 --- a/pkg/cache/route.go +++ b/pkg/cache/route.go @@ -21,42 +21,58 @@ type RouteCache struct { type RouteCacheManager struct { datastore CacheDatastore + prefix string ttl int } func NewRouteCacheManager(datastore CacheDatastore, ttl int) RouteCacheManager { return RouteCacheManager{ datastore: datastore, + prefix: "route_", ttl: ttl, } } -func (rm *RouteCacheManager) Save(name string, r RouteCache) { +func (rm *RouteCacheManager) Save(name string, r RouteCache) bool { + if !rm.datastore.IsConnected() { + log.Println("Route-cache:", "Not connected to server") + return false + } + data, err := json.Marshal(r) if err != nil { - log.Println("JSON:", err) // FIXME + log.Println("Route-cache", "JSON encode:", err) + return false } - err = rm.datastore.SetKey("route_"+name, data, rm.ttl) + name = rm.prefix + name + err = rm.datastore.SetKey(name, data, rm.ttl) if err != nil { - log.Println("REDIS:", err, name) // FIXME + log.Println("Route-cache:", "Unable to save", name, err) + return false } + + return true } -func (rm *RouteCacheManager) Load(name string) (bool, RouteCache) { - name = "route_" + name - - data, err := rm.datastore.GetKey(name) - if err != nil { - log.Println("REDIS:", err, name) // FIXME - - return false, RouteCache{} +func (rm *RouteCacheManager) Load(name string) (bool, *RouteCache) { + if !rm.datastore.IsConnected() { + log.Println("Route-cache:", "Not connected to server") + return false, &RouteCache{} } - rc := RouteCache{} + name = rm.prefix + name + data, err := rm.datastore.GetKey(name) + if err != nil { + log.Println("Route-cache:", "Unable to load", name, err) + return false, &RouteCache{} + } + + rc := &RouteCache{} err = json.Unmarshal([]byte(data.(string)), &rc) if err != nil { - log.Println("JSON:", err) // FIXME + log.Println("Route-cache:", "JSON decode:", err) + return false, &RouteCache{} } return true, rc diff --git a/pkg/config/config.go b/pkg/config/config.go index 6fb1785..23b6fff 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -18,6 +18,8 @@ import ( type Config struct { Server Server Backends map[string]Backend + + confPath string } type Server struct { @@ -42,12 +44,18 @@ var DefaultRoute = Route{ Target: "", } -func (c *Config) Load(cPath string) error { - if _, err := os.Stat(cPath); err != nil { +func New(confPath string) *Config { + return &Config{ + confPath: confPath, + } +} + +func (c *Config) Load() error { + if _, err := os.Stat(c.confPath); err != nil { return err } - data, err := ioutil.ReadFile(cPath) + data, err := ioutil.ReadFile(c.confPath) if err != nil { return err } diff --git a/pkg/server/response.go b/pkg/server/response.go new file mode 100644 index 0000000..8dbb0e4 --- /dev/null +++ b/pkg/server/response.go @@ -0,0 +1,10 @@ +// ___ ____ ___ ___ +// \ \ / / | _ | __| \ \ / / || | __ || || _ | +// \ \/ / |___ | |__ \ \/ / || |___ || ||___| +// \ / | _ | _ | \ / || __ | || ||\\ +// \/ |___ |___ | \/ || ____| || || \\ +// +// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License +// Repo: https://git.pbiernat.dev/golang/vegvisir + +package server diff --git a/pkg/server/router.go b/pkg/server/router.go new file mode 100644 index 0000000..481824d --- /dev/null +++ b/pkg/server/router.go @@ -0,0 +1,83 @@ +// ___ ____ ___ ___ +// \ \ / / | _ | __| \ \ / / || | __ || || _ | +// \ \/ / |___ | |__ \ \/ / || |___ || ||___| +// \ / | _ | _ | \ / || __ | || ||\\ +// \/ |___ |___ | \/ || ____| || || \\ +// +// 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{} +} diff --git a/pkg/server/server.go b/pkg/server/server.go index d61b2fc..31790d8 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -15,8 +15,6 @@ import ( "log" "os" "os/signal" - "regexp" - "strings" "time" "vegvisir/pkg/cache" "vegvisir/pkg/config" @@ -30,32 +28,40 @@ const ( ) type Server struct { - Config config.Config - - cFilePath string - rCache map[string]cache.RouteCache // Internal route cache - routeCM cache.RouteCacheManager // Redis route cache - respCM cache.ResponseCacheManager // Redis response cache + config *config.Config + router *Router + respCM cache.ResponseCacheManager // Redis response cache } func NewServer(cPath string) *Server { - datastore := cache.NewRedisDatastore("127.0.0.7", 6379) // FIXME use config or env... + config := config.New(cPath) - return &Server{ - 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... + if err := config.Load(); err != nil { + log.Fatalln("Unable to find config file: ", cPath, err) } + + 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() { - if err := s.Config.Load(s.cFilePath); err != nil { - log.Fatalln("Unable to find config file: ", s.cFilePath, err) - } - 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 { log.Fatalf("Server panic! Error message: %s", err) } @@ -86,10 +92,10 @@ func (s *Server) mainHandler(ctx *fasthttp.RequestCtx) { ctx.Response.Header.Add(fasthttp.HeaderServer, Name) // move all below logic to concrete handler or sth.... - reqUri, sReqUri, sReqMethod := ctx.RequestURI(), string(ctx.RequestURI()), string(ctx.Method()) - log.Println("Incomming request:", sReqMethod, sReqUri) + reqUrl, sReqUrl, sReqMethod := ctx.RequestURI(), string(ctx.RequestURI()), string(ctx.Method()) + log.Println("Incomming request:", sReqMethod, sReqUrl) - found, route := s.findRouteByRequestURI(reqUri) + found, route := s.router.FindByRequestURL(reqUrl) if !found { // FIXME: return 404/5xx error in repsonse, maybe define it in Backend config? ctx.SetStatusCode(fasthttp.StatusNotFound) @@ -97,10 +103,10 @@ func (s *Server) mainHandler(ctx *fasthttp.RequestCtx) { } // handle response caching - if ok, data := s.respCM.Load(sReqUri); ok { + if ok, data := s.respCM.Load(sReqUrl); ok { log.Println("Read resp from cache: ", route.TargetUrl) - ctx.SetBody([]byte(data.Body)) + ctx.SetBody([]byte(data.Body)) // FIXME missing headers etc... } else { log.Println("Send req to backend url: ", route.TargetUrl) @@ -126,51 +132,10 @@ func (s *Server) mainHandler(ctx *fasthttp.RequestCtx) { // save response to cache respCache := cache.ResponseCache{ - URL: sReqUri, + URL: sReqUrl, Body: string(bckResp.Body()), // Headers: [] } // FIXME: prepare resp cache struct in respCM.Save method or other service... - s.respCM.Save(sReqUri, respCache) + s.respCM.Save(sReqUrl, 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{} -} From 58eaaef0b31f77e7c2d18bc66e780aa042b11afb Mon Sep 17 00:00:00 2001 From: Piotr Biernat Date: Sun, 25 Jul 2021 01:02:35 +0200 Subject: [PATCH 3/7] [refactor] Simplified selecting cache datasource and fail-safe mechanism --- pkg/cache/cache.go | 39 +++++++++++++++++++++++++++++++++++++++ pkg/config/config.go | 12 ++++++++++++ pkg/main.go | 6 ++---- pkg/server/server.go | 20 +++++--------------- vegvisir.json.dist | 10 ++++++++++ 5 files changed, 68 insertions(+), 19 deletions(-) create mode 100644 pkg/cache/cache.go diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go new file mode 100644 index 0000000..9296fb3 --- /dev/null +++ b/pkg/cache/cache.go @@ -0,0 +1,39 @@ +// ___ ____ ___ ___ +// \ \ / / | _ | __| \ \ / / || | __ || || _ | +// \ \/ / |___ | |__ \ \/ / || |___ || ||___| +// \ / | _ | _ | \ / || __ | || ||\\ +// \/ |___ |___ | \/ || ____| || || \\ +// +// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License +// Repo: https://git.pbiernat.dev/golang/vegvisir + +package cache + +import ( + "log" + "vegvisir/pkg/config" +) + +const ( + TYPE_REDIS = "redis" + TYPE_MEMORY = "memory" +) + +func GetCacheDatastore(config config.Cache) *CacheDatastore { + var datastore CacheDatastore + + if config.Type == TYPE_REDIS { + datastore = NewRedisDatastore(config.Host, config.Port) + } else { + datastore = NewMemoryDatastore() + } + + //fail-safe switch to memory datasource + if !datastore.IsConnected() { + log.Println("Cache server is not responding, switching to memory cache.") + + datastore = NewMemoryDatastore() + } + + return &datastore +} diff --git a/pkg/config/config.go b/pkg/config/config.go index 23b6fff..b322d9f 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -17,6 +17,7 @@ import ( type Config struct { Server Server + Cache Cache Backends map[string]Backend confPath string @@ -27,6 +28,17 @@ type Server struct { Port int } +type Cache struct { + Type string + Host string + Port int + Username string + Password string + Database string + RouteTtl int + ResponseTtl int +} + type Backend struct { PrefixUrl string BackendAddress string diff --git a/pkg/main.go b/pkg/main.go index 27e15bf..3d4ee7f 100644 --- a/pkg/main.go +++ b/pkg/main.go @@ -19,15 +19,13 @@ import ( ) var ( - cFile = flag.String("c", "vegvisir.json", "Path to config file") - + cPath = flag.String("c", "vegvisir.json", "Path to config file") // for profiling... cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`") memprofile = flag.String("memprofile", "", "write memory profile to `file`") ) func main() { - flag.Parse() // cpu profiling if *cpuprofile != "" { @@ -42,7 +40,7 @@ func main() { defer pprof.StopCPUProfile() } - server.NewServer(*cFile).Run() + server.NewServer(*cPath).Run() // memory profiling if *memprofile != "" { diff --git a/pkg/server/server.go b/pkg/server/server.go index 31790d8..826434e 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -40,23 +40,13 @@ func NewServer(cPath string) *Server { log.Fatalln("Unable to find config file: ", cPath, err) } - server := &Server{ + datastore := cache.GetCacheDatastore(config.Cache) + + return &Server{ config: config, + router: NewRouter(config, *datastore, config.Cache.RouteTtl), + respCM: cache.NewResponseCacheManager(*datastore, config.Cache.ResponseTtl), } - - 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() { diff --git a/vegvisir.json.dist b/vegvisir.json.dist index 8394cc5..c98251b 100644 --- a/vegvisir.json.dist +++ b/vegvisir.json.dist @@ -29,5 +29,15 @@ "target": "article-global/$1" }] } + }, + "cache": { + "type": "redis", + "host": "localhost", + "port": 6379, + "username": "", + "password": "", + "database": "0", + "routeTtl": 300, + "responseTtl": 300 } } \ No newline at end of file From 1c2c0bb467325c5ae5209ca0521592c34f89e04f Mon Sep 17 00:00:00 2001 From: Piotr Biernat Date: Sun, 25 Jul 2021 01:44:50 +0200 Subject: [PATCH 4/7] [fix] Added GC to memory cache --- pkg/cache/memory_datastore.go | 20 ++++++++++++++++++++ pkg/cache/response.go | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/pkg/cache/memory_datastore.go b/pkg/cache/memory_datastore.go index 7c2a997..9b5e065 100644 --- a/pkg/cache/memory_datastore.go +++ b/pkg/cache/memory_datastore.go @@ -11,25 +11,39 @@ package cache import ( "errors" + "time" ) +type TtlItem struct { + ts int // timestamp + ttl int +} + func NewMemoryDatastore() *MemoryDatastore { return &MemoryDatastore{ cache: make(map[string]interface{}), + ts: make(map[string]TtlItem), } } type MemoryDatastore struct { cache map[string]interface{} + ts map[string]TtlItem } func (ds *MemoryDatastore) SetKey(key string, data interface{}, ttl int) error { ds.cache[key] = data + ds.ts[key] = TtlItem{ + ts: time.Now().Second(), + ttl: ttl, + } return nil } func (ds *MemoryDatastore) GetKey(key string) (interface{}, error) { + ds.gc(key) // remove key is time of creation is outdated + if data, ok := ds.cache[key]; ok { return data, nil } @@ -40,3 +54,9 @@ func (ds *MemoryDatastore) GetKey(key string) (interface{}, error) { func (ds *MemoryDatastore) IsConnected() bool { return true } + +func (ds *MemoryDatastore) gc(key string) { + if item, ok := ds.ts[key]; ok && item.ts < time.Now().Second()-item.ttl { + delete(ds.cache, key) + } +} diff --git a/pkg/cache/response.go b/pkg/cache/response.go index 52562e4..2ea9d91 100644 --- a/pkg/cache/response.go +++ b/pkg/cache/response.go @@ -49,7 +49,7 @@ func (rm *ResponseCacheManager) Save(name string, r ResponseCache) bool { name = rm.prefix + name err = rm.datastore.SetKey(name, string(data), rm.ttl) if err != nil { - log.Println("Response-cache:", err, name) // FIXME + log.Println("Response-cache:", err, name) return false } From 8c0f8d4151ce4085969cf7d157826d2431e9a759 Mon Sep 17 00:00:00 2001 From: Piotr Biernat Date: Sun, 25 Jul 2021 20:13:03 +0200 Subject: [PATCH 5/7] [fix] Added support for headers cache --- pkg/cache/cache.go | 3 +-- pkg/cache/memory_datastore.go | 2 +- pkg/cache/response.go | 8 +++++--- pkg/server/server.go | 33 +++++++++++++++++++++++---------- 4 files changed, 30 insertions(+), 16 deletions(-) diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go index 9296fb3..526319b 100644 --- a/pkg/cache/cache.go +++ b/pkg/cache/cache.go @@ -28,10 +28,9 @@ func GetCacheDatastore(config config.Cache) *CacheDatastore { datastore = NewMemoryDatastore() } - //fail-safe switch to memory datasource + // fail-safe switch to memory datasource if !datastore.IsConnected() { log.Println("Cache server is not responding, switching to memory cache.") - datastore = NewMemoryDatastore() } diff --git a/pkg/cache/memory_datastore.go b/pkg/cache/memory_datastore.go index 9b5e065..411c453 100644 --- a/pkg/cache/memory_datastore.go +++ b/pkg/cache/memory_datastore.go @@ -16,7 +16,7 @@ import ( type TtlItem struct { ts int // timestamp - ttl int + ttl int // ttl in seconds } func NewMemoryDatastore() *MemoryDatastore { diff --git a/pkg/cache/response.go b/pkg/cache/response.go index 2ea9d91..bd036e4 100644 --- a/pkg/cache/response.go +++ b/pkg/cache/response.go @@ -14,10 +14,12 @@ import ( "log" ) +type Headers map[string]string + type ResponseCache struct { - URL string - Body string - // Headers map[string]string + URL string + Body string + Headers Headers } type ResponseCacheManager struct { diff --git a/pkg/server/server.go b/pkg/server/server.go index 826434e..af33c57 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -23,8 +23,8 @@ import ( ) const ( - Version = "0.1-dev" - Name = "Vegvisir/" + Version + ServerVersion = "0.1-dev" + ServerName = "Vegvisir/" + ServerVersion ) type Server struct { @@ -79,11 +79,11 @@ func (s *Server) Shutdown(ctx context.Context) { // TODO: wait for all connectio } func (s *Server) mainHandler(ctx *fasthttp.RequestCtx) { - ctx.Response.Header.Add(fasthttp.HeaderServer, Name) + // http := client.NewHttpClient(ctx) // move all below logic to concrete handler or sth.... reqUrl, sReqUrl, sReqMethod := ctx.RequestURI(), string(ctx.RequestURI()), string(ctx.Method()) - log.Println("Incomming request:", sReqMethod, sReqUrl) + log.Println("Incoming request:", sReqMethod, sReqUrl) found, route := s.router.FindByRequestURL(reqUrl) if !found { @@ -96,7 +96,12 @@ func (s *Server) mainHandler(ctx *fasthttp.RequestCtx) { if ok, data := s.respCM.Load(sReqUrl); ok { log.Println("Read resp from cache: ", route.TargetUrl) - ctx.SetBody([]byte(data.Body)) // FIXME missing headers etc... + // copy headers and body from cache + ctx.Response.Header.DisableNormalizing() + for key, value := range data.Headers { + ctx.Response.Header.Set(key, value) + } + ctx.SetBody([]byte(data.Body)) } else { log.Println("Send req to backend url: ", route.TargetUrl) @@ -116,15 +121,23 @@ func (s *Server) mainHandler(ctx *fasthttp.RequestCtx) { return } - ctx.Response.Header.SetBytesV(fasthttp.HeaderContentType, bckResp.Header.ContentType()) - ctx.SetStatusCode(bckResp.StatusCode()) + headers := make(cache.Headers) + // rewrite headers from backend to gateway response + bckResp.Header.Set(fasthttp.HeaderServer, ServerName) + bckResp.Header.Del(fasthttp.HeaderXPoweredBy) + ctx.Response.Header.DisableNormalizing() + bckResp.Header.VisitAll(func(key, value []byte) { + headers[string(key)] = string(value) + ctx.Response.Header.SetBytesKV(key, value) + }) + // ctx.SetStatusCode(bckResp.StatusCode()) ctx.SetBody(bckResp.Body()) // save response to cache respCache := cache.ResponseCache{ - URL: sReqUrl, - Body: string(bckResp.Body()), - // Headers: [] + URL: sReqUrl, + Body: string(bckResp.Body()), + Headers: headers, } // FIXME: prepare resp cache struct in respCM.Save method or other service... s.respCM.Save(sReqUrl, respCache) } From e070fc402a4808cacc8b753ddc338a8c522531ed Mon Sep 17 00:00:00 2001 From: Piotr Biernat Date: Mon, 2 Aug 2021 23:14:16 +0200 Subject: [PATCH 6/7] Added go vet, staticheck and lint to .drone.yml --- .drone.yml | 31 ++++++++++++++++++++++++++++--- pkg/server/server.go | 3 ++- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/.drone.yml b/.drone.yml index 92f9976..47fff11 100644 --- a/.drone.yml +++ b/.drone.yml @@ -3,7 +3,32 @@ type: docker name: default steps: -- name: test - image: golang +- name: static_check + image: golang:1.16 commands: - - ls -lah \ No newline at end of file + - go get honnef.co/go/tools/cmd/staticcheck + - staticcheck ./pkg/... + volumes: + - name: gopath + path: /go + +- name: lint + image: golang:1.16 + commands: + - go get golang.org/x/lint/golint + - golint -set_exit_status ./pkg/... + volumes: + - name: gopath + path: /go + +- name: vet + image: golang:1.16 + commands: + - go vet ./pkg/... + volumes: + - name: gopath + path: /go + +volumes: +- name: gopath + temp: {} \ No newline at end of file diff --git a/pkg/server/server.go b/pkg/server/server.go index af33c57..8687249 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -15,6 +15,7 @@ import ( "log" "os" "os/signal" + "syscall" "time" "vegvisir/pkg/cache" "vegvisir/pkg/config" @@ -61,7 +62,7 @@ func (s *Server) Run() { // Wait for an interrupt interrupt := make(chan os.Signal, 1) - signal.Notify(interrupt, os.Interrupt, os.Kill) + signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM) <-interrupt log.Println("SIGKILL or SIGINT caught, shutting down...") From b0e492c163b491365ebd8dcd9c44281edcb24d49 Mon Sep 17 00:00:00 2001 From: Piotr Biernat Date: Tue, 3 Aug 2021 00:13:14 +0200 Subject: [PATCH 7/7] Fixes after code linters suggestions --- go.sum | 14 ++++ pkg/cache/cache.go | 20 ++++-- pkg/cache/datastore.go | 18 ------ pkg/cache/memory_datastore.go | 14 ++-- pkg/cache/redis_datastore.go | 5 ++ pkg/cache/response.go | 10 ++- pkg/cache/route.go | 13 ++-- pkg/client/http.go | 16 +++++ pkg/client/tcp.go | 10 --- pkg/config/config.go | 14 +++- pkg/server/response.go | 10 --- pkg/server/router.go | 44 +++++++------ pkg/server/server.go | 116 ++++++++++++++++++---------------- pkg/server/struct.go | 10 --- vegvisir.json | 16 +++-- vegvisir.json.dist | 8 +-- 16 files changed, 188 insertions(+), 150 deletions(-) delete mode 100644 pkg/cache/datastore.go delete mode 100644 pkg/client/tcp.go delete mode 100644 pkg/server/response.go delete mode 100644 pkg/server/struct.go diff --git a/go.sum b/go.sum index 29ee320..650c1c7 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,7 @@ github.com/andybalholm/brotli v1.0.2/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu github.com/andybalholm/brotli v1.0.3 h1:fpcw+r1N1h0Poc1F/pHbW40cUm/lMEQslZtCkBQ0UnM= github.com/andybalholm/brotli v1.0.3/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg= github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA= @@ -11,22 +12,27 @@ github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:x github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0= github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/klauspost/compress v1.12.2/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= github.com/klauspost/compress v1.13.1 h1:wXr2uRxZTJXHLly6qhJabee5JqIhTRoLBhDOA74hDEQ= github.com/klauspost/compress v1.13.1/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= +github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= +github.com/onsi/ginkgo v1.15.0 h1:1V1NfVQR87RtWAgp1lv9JZJ5Jap+XFGKPi00andXGi4= github.com/onsi/ginkgo v1.15.0/go.mod h1:hF8qUzuuC8DJGygJH3726JnCZX4MYbRB8yFfISqnKUg= github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= +github.com/onsi/gomega v1.10.5 h1:7n6FEkpFmfCoo2t+YYqXH0evK+a9ICQz0xcAy9dYcaQ= github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7mt48= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= @@ -46,6 +52,7 @@ golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/ golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20210510120150-4163338589ed h1:p9UgmWI9wKpfYmgaV/IZKGdXc5qEK45tDwwwDyjS26I= golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -62,10 +69,12 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= @@ -73,16 +82,21 @@ golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4f golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM= google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go index 526319b..91f3496 100644 --- a/pkg/cache/cache.go +++ b/pkg/cache/cache.go @@ -15,14 +15,24 @@ import ( ) const ( - TYPE_REDIS = "redis" - TYPE_MEMORY = "memory" + typeRedis = "redis" + typeMemory = "memory" ) -func GetCacheDatastore(config config.Cache) *CacheDatastore { - var datastore CacheDatastore +// Datastore interface +type Datastore interface { + SetKey(string, interface{}, int) error - if config.Type == TYPE_REDIS { + GetKey(string) (interface{}, error) + + IsConnected() bool +} + +// GetCacheDatastore function +func GetCacheDatastore(config config.Cache) *Datastore { + var datastore Datastore + + if config.Type == typeRedis { datastore = NewRedisDatastore(config.Host, config.Port) } else { datastore = NewMemoryDatastore() diff --git a/pkg/cache/datastore.go b/pkg/cache/datastore.go deleted file mode 100644 index 9208916..0000000 --- a/pkg/cache/datastore.go +++ /dev/null @@ -1,18 +0,0 @@ -// ___ ____ ___ ___ -// \ \ / / | _ | __| \ \ / / || | __ || || _ | -// \ \/ / |___ | |__ \ \/ / || |___ || ||___| -// \ / | _ | _ | \ / || __ | || ||\\ -// \/ |___ |___ | \/ || ____| || || \\ -// -// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License -// Repo: https://git.pbiernat.dev/golang/vegvisir - -package cache - -type CacheDatastore interface { - SetKey(string, interface{}, int) error - - GetKey(string) (interface{}, error) - - IsConnected() bool -} diff --git a/pkg/cache/memory_datastore.go b/pkg/cache/memory_datastore.go index 411c453..cb8315c 100644 --- a/pkg/cache/memory_datastore.go +++ b/pkg/cache/memory_datastore.go @@ -14,26 +14,30 @@ import ( "time" ) -type TtlItem struct { +// TTLItem struct +type TTLItem struct { ts int // timestamp ttl int // ttl in seconds } +// NewMemoryDatastore function func NewMemoryDatastore() *MemoryDatastore { return &MemoryDatastore{ cache: make(map[string]interface{}), - ts: make(map[string]TtlItem), + ts: make(map[string]TTLItem), } } +// MemoryDatastore struct type MemoryDatastore struct { cache map[string]interface{} - ts map[string]TtlItem + ts map[string]TTLItem } +// SetKey function func (ds *MemoryDatastore) SetKey(key string, data interface{}, ttl int) error { ds.cache[key] = data - ds.ts[key] = TtlItem{ + ds.ts[key] = TTLItem{ ts: time.Now().Second(), ttl: ttl, } @@ -41,6 +45,7 @@ func (ds *MemoryDatastore) SetKey(key string, data interface{}, ttl int) error { return nil } +// GetKey function func (ds *MemoryDatastore) GetKey(key string) (interface{}, error) { ds.gc(key) // remove key is time of creation is outdated @@ -51,6 +56,7 @@ func (ds *MemoryDatastore) GetKey(key string) (interface{}, error) { return nil, errors.New("Key not found" + key) } +// IsConnected function func (ds *MemoryDatastore) IsConnected() bool { return true } diff --git a/pkg/cache/redis_datastore.go b/pkg/cache/redis_datastore.go index bf84819..675a35e 100644 --- a/pkg/cache/redis_datastore.go +++ b/pkg/cache/redis_datastore.go @@ -16,6 +16,7 @@ import ( "github.com/go-redis/redis" ) +// NewRedisDatastore function func NewRedisDatastore(host string, port int) *RedisDatastore { return &RedisDatastore{ client: redis.NewClient(&redis.Options{ @@ -26,10 +27,12 @@ func NewRedisDatastore(host string, port int) *RedisDatastore { } } +// RedisDatastore class type RedisDatastore struct { client *redis.Client } +// SetKey function func (ds *RedisDatastore) SetKey(key string, data interface{}, ttl int) error { err := ds.client.Set(key, data, time.Duration(ttl)*time.Second).Err() if err != nil { @@ -39,6 +42,7 @@ func (ds *RedisDatastore) SetKey(key string, data interface{}, ttl int) error { return nil } +// GetKey function func (ds *RedisDatastore) GetKey(key string) (interface{}, error) { data, err := ds.client.Get(key).Result() if err != nil { @@ -48,6 +52,7 @@ func (ds *RedisDatastore) GetKey(key string) (interface{}, error) { return data, nil } +// IsConnected function func (ds *RedisDatastore) IsConnected() bool { _, err := ds.client.Ping().Result() diff --git a/pkg/cache/response.go b/pkg/cache/response.go index bd036e4..5b740d4 100644 --- a/pkg/cache/response.go +++ b/pkg/cache/response.go @@ -14,21 +14,25 @@ import ( "log" ) +// Headers type type Headers map[string]string +// ResponseCache struct type ResponseCache struct { URL string Body string Headers Headers } +// ResponseCacheManager class type ResponseCacheManager struct { - datastore CacheDatastore + datastore Datastore prefix string ttl int } -func NewResponseCacheManager(datastore CacheDatastore, ttl int) ResponseCacheManager { +// NewResponseCacheManager function +func NewResponseCacheManager(datastore Datastore, ttl int) ResponseCacheManager { return ResponseCacheManager{ datastore: datastore, prefix: "response_", @@ -36,6 +40,7 @@ func NewResponseCacheManager(datastore CacheDatastore, ttl int) ResponseCacheMan } } +// Save function func (rm *ResponseCacheManager) Save(name string, r ResponseCache) bool { if !rm.datastore.IsConnected() { log.Println("Response-cache:", "Not connected to server") @@ -58,6 +63,7 @@ func (rm *ResponseCacheManager) Save(name string, r ResponseCache) bool { return true } +// Load function func (rm *ResponseCacheManager) Load(name string) (bool, *ResponseCache) { if !rm.datastore.IsConnected() { log.Println("Response-cache:", "Not connected to server") diff --git a/pkg/cache/route.go b/pkg/cache/route.go index b79820f..25fc33c 100644 --- a/pkg/cache/route.go +++ b/pkg/cache/route.go @@ -14,18 +14,21 @@ import ( "log" ) +// RouteCache struct type RouteCache struct { - SourceUrl string - TargetUrl string + SourceURL string + TargetURL string } +// RouteCacheManager class type RouteCacheManager struct { - datastore CacheDatastore + datastore Datastore prefix string ttl int } -func NewRouteCacheManager(datastore CacheDatastore, ttl int) RouteCacheManager { +// NewRouteCacheManager function +func NewRouteCacheManager(datastore Datastore, ttl int) RouteCacheManager { return RouteCacheManager{ datastore: datastore, prefix: "route_", @@ -33,6 +36,7 @@ func NewRouteCacheManager(datastore CacheDatastore, ttl int) RouteCacheManager { } } +// Save function func (rm *RouteCacheManager) Save(name string, r RouteCache) bool { if !rm.datastore.IsConnected() { log.Println("Route-cache:", "Not connected to server") @@ -55,6 +59,7 @@ func (rm *RouteCacheManager) Save(name string, r RouteCache) bool { return true } +// Load function func (rm *RouteCacheManager) Load(name string) (bool, *RouteCache) { if !rm.datastore.IsConnected() { log.Println("Route-cache:", "Not connected to server") diff --git a/pkg/client/http.go b/pkg/client/http.go index 0510e9a..624dc5f 100644 --- a/pkg/client/http.go +++ b/pkg/client/http.go @@ -8,3 +8,19 @@ // Repo: https://git.pbiernat.dev/golang/vegvisir package client + +import "github.com/valyala/fasthttp" + +// HTTPClient struct +type HTTPClient struct { + context *fasthttp.RequestCtx +} + +// NewHTTPClient function +func NewHTTPClient(ctx *fasthttp.RequestCtx) *HTTPClient { + return &HTTPClient{ + context: ctx, + } +} + +// func (c *HTTPClient) diff --git a/pkg/client/tcp.go b/pkg/client/tcp.go deleted file mode 100644 index 4bd534b..0000000 --- a/pkg/client/tcp.go +++ /dev/null @@ -1,10 +0,0 @@ -// ___ ____ ___ ___ -// \ \ / / | _ | __| \ \ / / || | __ || || _ | -// \ \/ / |___ | |__ \ \/ / || |___ || ||___| -// \ / | _ | _ | \ / || __ | || ||\\ -// \/ |___ |___ | \/ || ____| || || \\ - -// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License -// Repo: https://git.pbiernat.dev/golang/vegvisir - -package client diff --git a/pkg/config/config.go b/pkg/config/config.go index b322d9f..52a3c5e 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -15,6 +15,7 @@ import ( "os" ) +// Config struct type Config struct { Server Server Cache Cache @@ -23,11 +24,13 @@ type Config struct { confPath string } +// Server struct type Server struct { Address string Port int } +// Cache struct type Cache struct { Type string Host string @@ -35,33 +38,38 @@ type Cache struct { Username string Password string Database string - RouteTtl int - ResponseTtl int + RouteTTL int + ResponseTTL int } +// Backend struct type Backend struct { - PrefixUrl string + PrefixURL string BackendAddress string Protocol string Routes []Route } +// Route struct type Route struct { Pattern string Target string } +// DefaultRoute shorthand var DefaultRoute = Route{ Pattern: "(.*)", Target: "", } +// New function func New(confPath string) *Config { return &Config{ confPath: confPath, } } +// Load function func (c *Config) Load() error { if _, err := os.Stat(c.confPath); err != nil { return err diff --git a/pkg/server/response.go b/pkg/server/response.go deleted file mode 100644 index 8dbb0e4..0000000 --- a/pkg/server/response.go +++ /dev/null @@ -1,10 +0,0 @@ -// ___ ____ ___ ___ -// \ \ / / | _ | __| \ \ / / || | __ || || _ | -// \ \/ / |___ | |__ \ \/ / || |___ || ||___| -// \ / | _ | _ | \ / || __ | || ||\\ -// \/ |___ |___ | \/ || ____| || || \\ -// -// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License -// Repo: https://git.pbiernat.dev/golang/vegvisir - -package server diff --git a/pkg/server/router.go b/pkg/server/router.go index 481824d..a880418 100644 --- a/pkg/server/router.go +++ b/pkg/server/router.go @@ -17,18 +17,21 @@ import ( "vegvisir/pkg/config" ) +// Router struct type Router struct { config *config.Config rcm cache.RouteCacheManager // Redis cache rCache map[string]cache.RouteCache // Internal route cache - TMP? } +// Route struct type Route struct { - SourceUrl string - TargetUrl string + SourceURL string + TargetURL string } -func NewRouter(conf *config.Config, cacheDS cache.CacheDatastore, ttl int) *Router { +// NewRouter function +func NewRouter(conf *config.Config, cacheDS cache.Datastore, ttl int) *Router { return &Router{ config: conf, rcm: cache.NewRouteCacheManager(cacheDS, ttl), @@ -36,42 +39,43 @@ func NewRouter(conf *config.Config, cacheDS cache.CacheDatastore, ttl int) *Rout } } +// FindByRequestURL function func (r *Router) FindByRequestURL(url []byte) (bool, Route) { - var sUrl string = string(url) + var sURL string = string(url) - for bId := range r.config.Backends { - bck := r.config.Backends[bId] - if !strings.Contains(sUrl, bck.PrefixUrl) { + 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] + 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 { + if cRoute, ok := r.rCache[sURL]; ok { route := Route{ - SourceUrl: cRoute.SourceUrl, - TargetUrl: cRoute.TargetUrl, + SourceURL: cRoute.SourceURL, + TargetURL: cRoute.TargetURL, } return true, route } - rgxp := regexp.MustCompile(fmt.Sprintf("%s%s", bck.PrefixUrl, routeCfg.Pattern)) + rgxp := regexp.MustCompile(fmt.Sprintf("%s%s", bck.PrefixURL, routeCfg.Pattern)) if rgxp.Match(url) { - targetUrl := bck.BackendAddress + rgxp.ReplaceAllString(sUrl, routeCfg.Target) + targetURL := bck.BackendAddress + rgxp.ReplaceAllString(sURL, routeCfg.Target) - route := Route{ - SourceUrl: sUrl, - TargetUrl: targetUrl, + route := Route{ // FIXME: duplicated #1 + SourceURL: sURL, + TargetURL: targetURL, } // s.rCacheManager.Save(sUri, cRoute) - r.rCache[sUrl] = cache.RouteCache{ - SourceUrl: route.SourceUrl, - TargetUrl: route.TargetUrl, + r.rCache[sURL] = cache.RouteCache{ // FIXME: duplicated #1 + SourceURL: route.SourceURL, + TargetURL: route.TargetURL, } return true, route diff --git a/pkg/server/server.go b/pkg/server/server.go index 8687249..5df8232 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -24,16 +24,20 @@ import ( ) const ( + // ServerVersion static ServerVersion = "0.1-dev" - ServerName = "Vegvisir/" + ServerVersion + // ServerName static + ServerName = "Vegvisir/" + ServerVersion ) +// Server struct type Server struct { config *config.Config router *Router respCM cache.ResponseCacheManager // Redis response cache } +// NewServer function func NewServer(cPath string) *Server { config := config.New(cPath) @@ -45,11 +49,12 @@ func NewServer(cPath string) *Server { return &Server{ config: config, - router: NewRouter(config, *datastore, config.Cache.RouteTtl), - respCM: cache.NewResponseCacheManager(*datastore, config.Cache.ResponseTtl), + router: NewRouter(config, *datastore, config.Cache.RouteTTL), + respCM: cache.NewResponseCacheManager(*datastore, config.Cache.ResponseTTL), } } +// Run function func (s *Server) Run() { go func() { serverAddress := s.config.Server.Address + ":" + fmt.Sprint(s.config.Server.Port) @@ -75,71 +80,74 @@ func (s *Server) Run() { log.Println("Server shutdown successfully.") } +// Shutdown function func (s *Server) Shutdown(ctx context.Context) { // TODO: wait for all connections to finish - log.Println("Shuting down finished") + log.Println("Shuting down...") } func (s *Server) mainHandler(ctx *fasthttp.RequestCtx) { // http := client.NewHttpClient(ctx) // move all below logic to concrete handler or sth.... - reqUrl, sReqUrl, sReqMethod := ctx.RequestURI(), string(ctx.RequestURI()), string(ctx.Method()) - log.Println("Incoming request:", sReqMethod, sReqUrl) + reqURL, sReqURL, sReqMethod := ctx.RequestURI(), string(ctx.RequestURI()), string(ctx.Method()) + // log.Println("Incoming request:", sReqMethod, sReqURL) - found, route := s.router.FindByRequestURL(reqUrl) + found, route := s.router.FindByRequestURL(reqURL) if !found { // FIXME: return 404/5xx error in repsonse, maybe define it in Backend config? ctx.SetStatusCode(fasthttp.StatusNotFound) return } - // handle response caching - if ok, data := s.respCM.Load(sReqUrl); ok { - log.Println("Read resp from cache: ", route.TargetUrl) + go func() { + cacheFound, data := s.respCM.Load(sReqURL) + if cacheFound { // get response from cache + // log.Println("Read resp from cache: ", route.TargetUrl) - // copy headers and body from cache - ctx.Response.Header.DisableNormalizing() - for key, value := range data.Headers { - ctx.Response.Header.Set(key, value) + // copy headers and body from cache + ctx.Response.Header.DisableNormalizing() + for key, value := range data.Headers { + ctx.Response.Header.Set(key, value) + } + ctx.SetBody([]byte(data.Body)) + } else { // send request to backend and save to cache + log.Println("Send req to backend url: ", route.TargetURL) + + // prepare to send request to backend - separate + bckReq := fasthttp.AcquireRequest() + bckResp := fasthttp.AcquireResponse() + defer fasthttp.ReleaseRequest(bckReq) + defer fasthttp.ReleaseResponse(bckResp) + + // copy headers from backend response and prepare request for backend - separate + bckReq.SetRequestURI(route.TargetURL) + bckReq.Header.SetMethod(sReqMethod) + + err := fasthttp.Do(bckReq, bckResp) + if err != nil { + ctx.SetStatusCode(fasthttp.StatusInternalServerError) + return + } + + headers := make(cache.Headers) + // rewrite headers from backend to gateway response + bckResp.Header.Set(fasthttp.HeaderServer, ServerName) + bckResp.Header.Del(fasthttp.HeaderXPoweredBy) + ctx.Response.Header.DisableNormalizing() + bckResp.Header.VisitAll(func(key, value []byte) { + headers[string(key)] = string(value) + ctx.Response.Header.SetBytesKV(key, value) + }) + // ctx.SetStatusCode(bckResp.StatusCode()) + ctx.SetBody(bckResp.Body()) + + // save response to cache + respCache := cache.ResponseCache{ + URL: sReqURL, + Body: string(bckResp.Body()), + Headers: headers, + } // FIXME: prepare resp cache struct in respCM.Save method or other service... + s.respCM.Save(sReqURL, respCache) } - ctx.SetBody([]byte(data.Body)) - } else { - log.Println("Send req to backend url: ", route.TargetUrl) - - // prepare to send request to backend - separate - bckReq := fasthttp.AcquireRequest() - bckResp := fasthttp.AcquireResponse() - defer fasthttp.ReleaseRequest(bckReq) - defer fasthttp.ReleaseResponse(bckResp) - - // copy headers from backend response and prepare request for backend - separate - bckReq.SetRequestURI(route.TargetUrl) - bckReq.Header.SetMethod(sReqMethod) - - err := fasthttp.Do(bckReq, bckResp) - if err != nil { - ctx.SetStatusCode(fasthttp.StatusInternalServerError) - return - } - - headers := make(cache.Headers) - // rewrite headers from backend to gateway response - bckResp.Header.Set(fasthttp.HeaderServer, ServerName) - bckResp.Header.Del(fasthttp.HeaderXPoweredBy) - ctx.Response.Header.DisableNormalizing() - bckResp.Header.VisitAll(func(key, value []byte) { - headers[string(key)] = string(value) - ctx.Response.Header.SetBytesKV(key, value) - }) - // ctx.SetStatusCode(bckResp.StatusCode()) - ctx.SetBody(bckResp.Body()) - - // save response to cache - respCache := cache.ResponseCache{ - URL: sReqUrl, - Body: string(bckResp.Body()), - Headers: headers, - } // FIXME: prepare resp cache struct in respCM.Save method or other service... - s.respCM.Save(sReqUrl, respCache) - } + }() } diff --git a/pkg/server/struct.go b/pkg/server/struct.go deleted file mode 100644 index 8dbb0e4..0000000 --- a/pkg/server/struct.go +++ /dev/null @@ -1,10 +0,0 @@ -// ___ ____ ___ ___ -// \ \ / / | _ | __| \ \ / / || | __ || || _ | -// \ \/ / |___ | |__ \ \/ / || |___ || ||___| -// \ / | _ | _ | \ / || __ | || ||\\ -// \/ |___ |___ | \/ || ____| || || \\ -// -// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License -// Repo: https://git.pbiernat.dev/golang/vegvisir - -package server diff --git a/vegvisir.json b/vegvisir.json index 7bd5829..d1b86d6 100644 --- a/vegvisir.json +++ b/vegvisir.json @@ -5,7 +5,7 @@ }, "backends": { "news-app": { - "prefixUrl": "news/", + "prefixURL": "news/", "backendAddress": "http://172.17.0.1:3030", "routes": [{ "pattern": "new/(.*)/([0-9]*)", @@ -19,7 +19,7 @@ }] }, "article-app": { - "prefixUrl": "art/", + "prefixURL": "art/", "backendAddress": "http://172.17.0.1:3030", "routes": [{ "pattern": "art1/(.*)", @@ -31,9 +31,13 @@ } }, "cache": { - "ttl": 300, - "route_ttl": 300, - "response_ttl": 300, - "headers_ttl": 300 + "type": "redis", + "host": "localhost", + "port": 6379, + "username": "", + "password": "", + "database": "0", + "routeTTL": 10, + "responseTTL": 10 } } \ No newline at end of file diff --git a/vegvisir.json.dist b/vegvisir.json.dist index c98251b..42d01d9 100644 --- a/vegvisir.json.dist +++ b/vegvisir.json.dist @@ -5,7 +5,7 @@ }, "backends": { "news-app": { - "prefixUrl": "news/", + "prefixURL": "news/", "backendAddress": "http://127.0.0.1:3030", "routes": [{ "pattern": "new/(.*)", @@ -19,7 +19,7 @@ }] }, "article-app": { - "prefixUrl": "art/", + "prefixURL": "art/", "backendAddress": "http://127.0.0.1:3030", "routes": [{ "pattern": "art1/(.*)", @@ -37,7 +37,7 @@ "username": "", "password": "", "database": "0", - "routeTtl": 300, - "responseTtl": 300 + "routeTTL": 300, + "responseTTL": 300 } } \ No newline at end of file