diff --git a/rest/httpx/responses.go b/rest/httpx/responses.go index 104f04aa..0461c788 100644 --- a/rest/httpx/responses.go +++ b/rest/httpx/responses.go @@ -15,8 +15,8 @@ import ( var ( errorHandler func(context.Context, error) (int, any) errorLock sync.RWMutex - respHandler func(context.Context, any) any - respLock sync.RWMutex + okHandler func(context.Context, any) any + okLock sync.RWMutex ) // Error writes err into w. @@ -40,9 +40,9 @@ func Ok(w http.ResponseWriter) { // OkJson writes v into w with 200 OK. func OkJson(w http.ResponseWriter, v any) { - respLock.RLock() - handler := respHandler - respLock.RUnlock() + okLock.RLock() + handler := okHandler + okLock.RUnlock() if handler != nil { v = handler(context.Background(), v) } @@ -51,9 +51,9 @@ func OkJson(w http.ResponseWriter, v any) { // OkJsonCtx writes v into w with 200 OK. func OkJsonCtx(ctx context.Context, w http.ResponseWriter, v any) { - respLock.RLock() - handlerCtx := respHandler - respLock.RUnlock() + okLock.RLock() + handlerCtx := okHandler + okLock.RUnlock() if handlerCtx != nil { v = handlerCtx(ctx, v) } @@ -80,11 +80,11 @@ func SetErrorHandlerCtx(handlerCtx func(context.Context, error) (int, any)) { errorHandler = handlerCtx } -// SetResponseHandler sets the response handler, which is called on calling OkJson and OkJsonCtx. -func SetResponseHandler(handler func(context.Context, any) any) { - respLock.Lock() - defer respLock.Unlock() - respHandler = handler +// SetOkHandler sets the response handler, which is called on calling OkJson and OkJsonCtx. +func SetOkHandler(handler func(context.Context, any) any) { + okLock.Lock() + defer okLock.Unlock() + okHandler = handler } // WriteJson writes v as json string into w with code. diff --git a/rest/httpx/responses_test.go b/rest/httpx/responses_test.go index 4ef823ca..44d4d720 100644 --- a/rest/httpx/responses_test.go +++ b/rest/httpx/responses_test.go @@ -141,16 +141,16 @@ func TestOkJson(t *testing.T) { }) t.Run("with handler", func(t *testing.T) { - respLock.RLock() - prev := respHandler - respLock.RUnlock() + okLock.RLock() + prev := okHandler + okLock.RUnlock() t.Cleanup(func() { - respLock.Lock() - respHandler = prev - respLock.Unlock() + okLock.Lock() + okHandler = prev + okLock.Unlock() }) - SetResponseHandler(func(_ context.Context, v interface{}) any { + SetOkHandler(func(_ context.Context, v interface{}) any { return fmt.Sprintf("hello %s", v.(message).Name) }) w := tracedResponseWriter{ @@ -175,16 +175,16 @@ func TestOkJsonCtx(t *testing.T) { }) t.Run("with handler", func(t *testing.T) { - respLock.RLock() - prev := respHandler - respLock.RUnlock() + okLock.RLock() + prev := okHandler + okLock.RUnlock() t.Cleanup(func() { - respLock.Lock() - respHandler = prev - respLock.Unlock() + okLock.Lock() + okHandler = prev + okLock.Unlock() }) - SetResponseHandler(func(_ context.Context, v interface{}) any { + SetOkHandler(func(_ context.Context, v interface{}) any { return fmt.Sprintf("hello %s", v.(message).Name) }) w := tracedResponseWriter{