develop-issue-4 #7

Merged
keedosn merged 3 commits from develop-issue-4 into develop 2021-07-25 21:01:07 +02:00
4 changed files with 30 additions and 16 deletions
Showing only changes of commit 8c0f8d4151 - Show all commits

1
pkg/cache/cache.go vendored
View File

@ -31,7 +31,6 @@ func GetCacheDatastore(config config.Cache) *CacheDatastore {
// fail-safe switch to memory datasource // fail-safe switch to memory datasource
if !datastore.IsConnected() { if !datastore.IsConnected() {
log.Println("Cache server is not responding, switching to memory cache.") log.Println("Cache server is not responding, switching to memory cache.")
datastore = NewMemoryDatastore() datastore = NewMemoryDatastore()
} }

View File

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

View File

@ -14,10 +14,12 @@ import (
"log" "log"
) )
type Headers map[string]string
type ResponseCache struct { type ResponseCache struct {
URL string URL string
Body string Body string
// Headers map[string]string Headers Headers
} }
type ResponseCacheManager struct { type ResponseCacheManager struct {

View File

@ -23,8 +23,8 @@ import (
) )
const ( const (
Version = "0.1-dev" ServerVersion = "0.1-dev"
Name = "Vegvisir/" + Version ServerName = "Vegvisir/" + ServerVersion
) )
type Server struct { 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) { 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.... // move all below logic to concrete handler or sth....
reqUrl, sReqUrl, sReqMethod := ctx.RequestURI(), string(ctx.RequestURI()), string(ctx.Method()) 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) found, route := s.router.FindByRequestURL(reqUrl)
if !found { if !found {
@ -96,7 +96,12 @@ func (s *Server) mainHandler(ctx *fasthttp.RequestCtx) {
if ok, data := s.respCM.Load(sReqUrl); ok { if ok, data := s.respCM.Load(sReqUrl); 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... // 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 { } else {
log.Println("Send req to backend url: ", route.TargetUrl) log.Println("Send req to backend url: ", route.TargetUrl)
@ -116,15 +121,23 @@ func (s *Server) mainHandler(ctx *fasthttp.RequestCtx) {
return return
} }
ctx.Response.Header.SetBytesV(fasthttp.HeaderContentType, bckResp.Header.ContentType()) headers := make(cache.Headers)
ctx.SetStatusCode(bckResp.StatusCode()) // 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()) ctx.SetBody(bckResp.Body())
// save response to cache // save response to cache
respCache := cache.ResponseCache{ respCache := cache.ResponseCache{
URL: sReqUrl, URL: sReqUrl,
Body: string(bckResp.Body()), Body: string(bckResp.Body()),
// Headers: [] 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(sReqUrl, respCache)
} }