mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-23 09:00:20 +08:00
chore: refactor httpx.SetOkHandler (#3373)
This commit is contained in:
parent
92e5819e91
commit
40e7a4cd07
@ -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.
|
||||
|
@ -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{
|
||||
|
Loading…
Reference in New Issue
Block a user