From 63b297d93d06bdb40d3af5c5ebf27fcbd9a525b2 Mon Sep 17 00:00:00 2001 From: Xeronith Date: Thu, 29 Sep 2022 16:58:21 +0330 Subject: [PATCH] feat(server): :zap: add cache to context --- greataped/contracts/context.go | 1 + greataped/server/http_context.go | 8 +++++++- greataped/server/http_server.go | 8 ++++---- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/greataped/contracts/context.go b/greataped/contracts/context.go index 595c16e..a05f138 100644 --- a/greataped/contracts/context.go +++ b/greataped/contracts/context.go @@ -11,6 +11,7 @@ type ( GUID() string Config() IConfig Storage() IStorage + Cache() ICache Request() IRequest Response() IResponse StringUtil() IStringUtil diff --git a/greataped/server/http_context.go b/greataped/server/http_context.go index 4b21c5b..84afcbf 100644 --- a/greataped/server/http_context.go +++ b/greataped/server/http_context.go @@ -25,15 +25,17 @@ import ( type httpServerContext struct { underlyingContext *fiber.Ctx + cache ICache request IRequest response IResponse stringUtil IStringUtil httpClient *http.Client } -func newContext(underlyingContext *fiber.Ctx) IContext { +func newContext(underlyingContext *fiber.Ctx, cache ICache) IContext { return &httpServerContext{ underlyingContext: underlyingContext, + cache: cache, request: newRequest(underlyingContext), response: newResponse(underlyingContext), stringUtil: utility.NewStringUtil(), @@ -91,6 +93,10 @@ func (context *httpServerContext) Storage() IStorage { return nil } +func (context *httpServerContext) Cache() ICache { + return context.cache +} + func (context *httpServerContext) Request() IRequest { return context.request } diff --git a/greataped/server/http_server.go b/greataped/server/http_server.go index 7a237f7..afbd91b 100644 --- a/greataped/server/http_server.go +++ b/greataped/server/http_server.go @@ -104,19 +104,19 @@ func (server *httpServer) Bind(routes ...IRoute) { switch route.Method() { case HttpGet: server.framework.Get(route.Path(), func(underlyingContext *fiber.Ctx) error { - return route.Handler()(newContext(underlyingContext)) + return route.Handler()(newContext(underlyingContext, server.cache)) }) case HttpPost: server.framework.Post(route.Path(), func(underlyingContext *fiber.Ctx) error { - return route.Handler()(newContext(underlyingContext)) + return route.Handler()(newContext(underlyingContext, server.cache)) }) case HttpPut: server.framework.Put(route.Path(), func(underlyingContext *fiber.Ctx) error { - return route.Handler()(newContext(underlyingContext)) + return route.Handler()(newContext(underlyingContext, server.cache)) }) case HttpDelete: server.framework.Delete(route.Path(), func(underlyingContext *fiber.Ctx) error { - return route.Handler()(newContext(underlyingContext)) + return route.Handler()(newContext(underlyingContext, server.cache)) }) default: panic("unsupported_method")