Skip to content

Commit

Permalink
fix lint and wrap base context
Browse files Browse the repository at this point in the history
  • Loading branch information
shan-96 committed Jun 8, 2024
1 parent ae18e35 commit fbd6dd7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
4 changes: 2 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
4 changes: 2 additions & 2 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 16 additions & 11 deletions server/wasabi_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit fbd6dd7

Please sign in to comment.