diff --git a/design/design.go b/design/design.go index 1b8d82d949afcf60654a2bf2251d6690e66ecdca..df4c8c84acb64044fe38b34e5487573297d43302 100644 --- a/design/design.go +++ b/design/design.go @@ -59,7 +59,9 @@ var _ = Service("cache", func() { Example("administration") }) - Response(StatusOK) + Response(StatusOK, func() { + ContentType("application/json") + }) }) }) diff --git a/gen/http/cache/server/encode_decode.go b/gen/http/cache/server/encode_decode.go index 1f443c6a3434788bf4ce255fdf6a9b95a2c8e3cc..b6587e807c913d0cd21fc1001df3baddd7353f34 100644 --- a/gen/http/cache/server/encode_decode.go +++ b/gen/http/cache/server/encode_decode.go @@ -21,6 +21,7 @@ import ( func EncodeGetResponse(encoder func(context.Context, http.ResponseWriter) goahttp.Encoder) func(context.Context, http.ResponseWriter, interface{}) error { return func(ctx context.Context, w http.ResponseWriter, v interface{}) error { res, _ := v.(interface{}) + ctx = context.WithValue(ctx, goahttp.ContentTypeKey, "application/json") enc := encoder(ctx, w) body := res w.WriteHeader(http.StatusOK) diff --git a/gen/http/openapi.json b/gen/http/openapi.json index 86a6fd83fe7a6c2c0b2b5249d419ec1ba3b53eae..b9d430311b5a1b01b8f6f061e08c090c3e191545 100644 --- a/gen/http/openapi.json +++ b/gen/http/openapi.json @@ -1 +1 @@ -{"swagger":"2.0","info":{"title":"Cache Service","description":"The cache service exposes interface for working with Redis.","version":""},"host":"localhost:8083","consumes":["application/json","application/xml","application/gob"],"produces":["application/json","application/xml","application/gob"],"paths":{"/liveness":{"get":{"tags":["health"],"summary":"Liveness health","operationId":"health#Liveness","responses":{"200":{"description":"OK response."}},"schemes":["http"]}},"/readiness":{"get":{"tags":["health"],"summary":"Readiness health","operationId":"health#Readiness","responses":{"200":{"description":"OK response."}},"schemes":["http"]}},"/v1/cache":{"get":{"tags":["cache"],"summary":"Get cache","description":"Get JSON value from the cache.","operationId":"cache#Get","parameters":[{"name":"x-cache-key","in":"header","description":"Cache entry key","required":true,"type":"string"},{"name":"x-cache-namespace","in":"header","description":"Cache entry namespace","required":true,"type":"string"},{"name":"x-cache-scope","in":"header","description":"Cache entry scope","required":true,"type":"string"}],"responses":{"200":{"description":"OK response.","schema":{"type":"string","format":"binary"}}},"schemes":["http"]},"post":{"tags":["cache"],"summary":"Set cache","description":"Set a JSON value in the cache.","operationId":"cache#Set","parameters":[{"name":"x-cache-key","in":"header","description":"Cache entry key","required":true,"type":"string"},{"name":"x-cache-namespace","in":"header","description":"Cache entry namespace","required":true,"type":"string"},{"name":"x-cache-scope","in":"header","description":"Cache entry scope","required":true,"type":"string"},{"name":"any","in":"body","required":true,"schema":{"type":"string","format":"binary"}}],"responses":{"201":{"description":"Created response."}},"schemes":["http"]}}}} \ No newline at end of file +{"swagger":"2.0","info":{"title":"Cache Service","description":"The cache service exposes interface for working with Redis.","version":""},"host":"localhost:8083","consumes":["application/json","application/xml","application/gob"],"produces":["application/json","application/xml","application/gob"],"paths":{"/liveness":{"get":{"tags":["health"],"summary":"Liveness health","operationId":"health#Liveness","responses":{"200":{"description":"OK response."}},"schemes":["http"]}},"/readiness":{"get":{"tags":["health"],"summary":"Readiness health","operationId":"health#Readiness","responses":{"200":{"description":"OK response."}},"schemes":["http"]}},"/v1/cache":{"get":{"tags":["cache"],"summary":"Get cache","description":"Get JSON value from the cache.","operationId":"cache#Get","produces":["application/json"],"parameters":[{"name":"x-cache-key","in":"header","description":"Cache entry key","required":true,"type":"string"},{"name":"x-cache-namespace","in":"header","description":"Cache entry namespace","required":true,"type":"string"},{"name":"x-cache-scope","in":"header","description":"Cache entry scope","required":true,"type":"string"}],"responses":{"200":{"description":"OK response.","schema":{"type":"string","format":"binary"}}},"schemes":["http"]},"post":{"tags":["cache"],"summary":"Set cache","description":"Set a JSON value in the cache.","operationId":"cache#Set","parameters":[{"name":"x-cache-key","in":"header","description":"Cache entry key","required":true,"type":"string"},{"name":"x-cache-namespace","in":"header","description":"Cache entry namespace","required":true,"type":"string"},{"name":"x-cache-scope","in":"header","description":"Cache entry scope","required":true,"type":"string"},{"name":"any","in":"body","required":true,"schema":{"type":"string","format":"binary"}}],"responses":{"201":{"description":"Created response."}},"schemes":["http"]}}}} \ No newline at end of file diff --git a/gen/http/openapi.yaml b/gen/http/openapi.yaml index ea46a9bd8011f0a652f062ab2bdfc45f70e449b0..b1bac2cdadd957be492a9618414d12516397b6b2 100644 --- a/gen/http/openapi.yaml +++ b/gen/http/openapi.yaml @@ -42,6 +42,8 @@ paths: summary: Get cache description: Get JSON value from the cache. operationId: cache#Get + produces: + - application/json parameters: - name: x-cache-key in: header diff --git a/internal/clients/redis/client.go b/internal/clients/redis/client.go index fbe9f5240a879a114e59e4a3d840ec285067998e..0b1f1c993de879d277aa217a2876cb5dd7571375 100644 --- a/internal/clients/redis/client.go +++ b/internal/clients/redis/client.go @@ -31,7 +31,7 @@ func New(addr, user, pass string, db int, defaultTTL time.Duration) *Client { } } -func (c *Client) Get(ctx context.Context, key string) (interface{}, error) { +func (c *Client) Get(ctx context.Context, key string) ([]byte, error) { result := c.rdb.Get(ctx, key) if result.Err() != nil { if result.Err() == redis.Nil { @@ -39,7 +39,7 @@ func (c *Client) Get(ctx context.Context, key string) (interface{}, error) { } return nil, result.Err() } - return result.Val(), nil + return []byte(result.Val()), nil } func (c *Client) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error { diff --git a/internal/service/cache/service.go b/internal/service/cache/service.go index 7c4357f97ebbc3de9daba45752af072d0d2e5686..4644cc015ed6f44f86ea7dc4b6ead5f73420f384 100644 --- a/internal/service/cache/service.go +++ b/internal/service/cache/service.go @@ -13,7 +13,7 @@ import ( ) type Cache interface { - Get(ctx context.Context, key string) (interface{}, error) + Get(ctx context.Context, key string) ([]byte, error) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error } @@ -47,7 +47,13 @@ func (s *Service) Get(ctx context.Context, req *cache.CacheGetRequest) (interfac return nil, errors.New("error getting value from cache", err) } - return data, nil + var decodedValue interface{} + if err := json.Unmarshal(data, &decodedValue); err != nil { + s.logger.Error("cannot decode json value from cache", zap.Error(err)) + return nil, errors.New("cannot decode json value from cache", err) + } + + return decodedValue, nil } func (s *Service) Set(ctx context.Context, req *cache.CacheSetRequest) error { @@ -68,6 +74,7 @@ func (s *Service) Set(ctx context.Context, req *cache.CacheSetRequest) error { s.logger.Error("error encode payload to json", zap.Error(err), operation) return errors.New("error encode payload to json", err) } + if err := s.cache.Set(ctx, key, value, 0); err != nil { s.logger.Error("error setting value in cache", zap.Error(err), operation) return errors.New("error setting value in cache", err)