feat(server): add cache to context

master
Xeronith 2022-09-29 16:58:21 +03:30
rodzic e0ccadf480
commit 63b297d93d
3 zmienionych plików z 12 dodań i 5 usunięć

Wyświetl plik

@ -11,6 +11,7 @@ type (
GUID() string GUID() string
Config() IConfig Config() IConfig
Storage() IStorage Storage() IStorage
Cache() ICache
Request() IRequest Request() IRequest
Response() IResponse Response() IResponse
StringUtil() IStringUtil StringUtil() IStringUtil

Wyświetl plik

@ -25,15 +25,17 @@ import (
type httpServerContext struct { type httpServerContext struct {
underlyingContext *fiber.Ctx underlyingContext *fiber.Ctx
cache ICache
request IRequest request IRequest
response IResponse response IResponse
stringUtil IStringUtil stringUtil IStringUtil
httpClient *http.Client httpClient *http.Client
} }
func newContext(underlyingContext *fiber.Ctx) IContext { func newContext(underlyingContext *fiber.Ctx, cache ICache) IContext {
return &httpServerContext{ return &httpServerContext{
underlyingContext: underlyingContext, underlyingContext: underlyingContext,
cache: cache,
request: newRequest(underlyingContext), request: newRequest(underlyingContext),
response: newResponse(underlyingContext), response: newResponse(underlyingContext),
stringUtil: utility.NewStringUtil(), stringUtil: utility.NewStringUtil(),
@ -91,6 +93,10 @@ func (context *httpServerContext) Storage() IStorage {
return nil return nil
} }
func (context *httpServerContext) Cache() ICache {
return context.cache
}
func (context *httpServerContext) Request() IRequest { func (context *httpServerContext) Request() IRequest {
return context.request return context.request
} }

Wyświetl plik

@ -104,19 +104,19 @@ func (server *httpServer) Bind(routes ...IRoute) {
switch route.Method() { switch route.Method() {
case HttpGet: case HttpGet:
server.framework.Get(route.Path(), func(underlyingContext *fiber.Ctx) error { server.framework.Get(route.Path(), func(underlyingContext *fiber.Ctx) error {
return route.Handler()(newContext(underlyingContext)) return route.Handler()(newContext(underlyingContext, server.cache))
}) })
case HttpPost: case HttpPost:
server.framework.Post(route.Path(), func(underlyingContext *fiber.Ctx) error { server.framework.Post(route.Path(), func(underlyingContext *fiber.Ctx) error {
return route.Handler()(newContext(underlyingContext)) return route.Handler()(newContext(underlyingContext, server.cache))
}) })
case HttpPut: case HttpPut:
server.framework.Put(route.Path(), func(underlyingContext *fiber.Ctx) error { server.framework.Put(route.Path(), func(underlyingContext *fiber.Ctx) error {
return route.Handler()(newContext(underlyingContext)) return route.Handler()(newContext(underlyingContext, server.cache))
}) })
case HttpDelete: case HttpDelete:
server.framework.Delete(route.Path(), func(underlyingContext *fiber.Ctx) error { server.framework.Delete(route.Path(), func(underlyingContext *fiber.Ctx) error {
return route.Handler()(newContext(underlyingContext)) return route.Handler()(newContext(underlyingContext, server.cache))
}) })
default: default:
panic("unsupported_method") panic("unsupported_method")