[fix] Added support for headers cache
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing

This commit is contained in:
Piotr Biernat 2021-07-25 20:13:03 +02:00
parent 1c2c0bb467
commit 8c0f8d4151
4 changed files with 30 additions and 16 deletions

3
pkg/cache/cache.go vendored
View File

@ -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()
}

View File

@ -16,7 +16,7 @@ import (
type TtlItem struct {
ts int // timestamp
ttl int
ttl int // ttl in seconds
}
func NewMemoryDatastore() *MemoryDatastore {

View File

@ -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 {

View File

@ -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)
}