From fbd6dd7cbfec5c013472c81f20d1b6ecfd90d5d8 Mon Sep 17 00:00:00 2001 From: shan-96 Date: Sat, 8 Jun 2024 18:57:33 +0700 Subject: [PATCH] fix lint and wrap base context --- server/server.go | 4 ++-- server/server_test.go | 4 ++-- server/wasabi_context.go | 27 ++++++++++++++++----------- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/server/server.go b/server/server.go index 9870f4c..e51c2c3 100644 --- a/server/server.go +++ b/server/server.go @@ -57,8 +57,8 @@ func NewServer(addr string, opts ...Option) *Server { server.handler = &http.Server{ Addr: addr, - ReadHeaderTimeout: server.baseCtx.GetHttpConfig().ReadHeaderTimeout, - ReadTimeout: server.baseCtx.GetHttpConfig().ReadTimeout, + ReadHeaderTimeout: server.baseCtx.GetHTTPConfig().ReadHeaderTimeout, + ReadTimeout: server.baseCtx.GetHTTPConfig().ReadTimeout, BaseContext: func(_ net.Listener) context.Context { return server.baseCtx }, diff --git a/server/server_test.go b/server/server_test.go index e9ccb39..80829d6 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -57,8 +57,8 @@ func TestServer_AddChannel(t *testing.T) { func TestServer_WithBaseContext(t *testing.T) { // Create a new Server instance with a base context - ctx := context.WithValue(context.Background(), testCtxKey("test"), "test") - + ctx:= NewWasabiDefaultContext(context.Background()) + ctx.WithValue(testCtxKey("test"), "test") server := NewServer(":0", WithBaseContext(NewWasabiDefaultContext(ctx))) // Check if the base context was set correctly diff --git a/server/wasabi_context.go b/server/wasabi_context.go index 6c3bf75..b148756 100644 --- a/server/wasabi_context.go +++ b/server/wasabi_context.go @@ -7,38 +7,43 @@ import ( type WasabiContext interface { context.Context - GetHttpConfig() HttpConfig + WithValue(key, value interface{}) WasabiContext + GetHTTPConfig() HTTPConfig } type WasabiDefaultContextImpl struct { context.Context } -func (w WasabiDefaultContextImpl) GetHttpConfig() HttpConfig { - return *getDefaultHttpConfig() +func (w WasabiDefaultContextImpl) GetHTTPConfig() HTTPConfig { + return *getDefaultHTTPConfig() } -type HttpConfig struct { +func (w WasabiDefaultContextImpl) WithValue(key, value interface{}) WasabiContext { + return WasabiDefaultContextImpl{Context: context.WithValue(w.Context, key, value)} +} + +type HTTPConfig struct { ReadHeaderTimeout time.Duration ReadTimeout time.Duration } func NewWasabiDefaultContext(fromContext context.Context) WasabiDefaultContextImpl { - var ctx = new(WasabiDefaultContextImpl) - return *ctx + var ctx = WasabiDefaultContextImpl{fromContext} + return ctx } -var defaultHttpConfig *HttpConfig +var defaultHTTPConfig *HTTPConfig -func getDefaultHttpConfig() *HttpConfig { - if defaultHttpConfig == nil { - defaultHttpConfig = &HttpConfig{ +func getDefaultHTTPConfig() *HTTPConfig { + if defaultHTTPConfig == nil { + defaultHTTPConfig = &HTTPConfig{ ReadHeaderTimeout: ReadHeaderTimeoutSeconds * time.Second, ReadTimeout: ReadTimeoutSeconds * time.Second, } } - return defaultHttpConfig + return defaultHTTPConfig } const (