|
|
@ -15,8 +15,8 @@ import (
|
|
|
|
var (
|
|
|
|
var (
|
|
|
|
errorHandler func(context.Context, error) (int, any)
|
|
|
|
errorHandler func(context.Context, error) (int, any)
|
|
|
|
errorLock sync.RWMutex
|
|
|
|
errorLock sync.RWMutex
|
|
|
|
respHandler func(context.Context, any) any
|
|
|
|
okHandler func(context.Context, any) any
|
|
|
|
respLock sync.RWMutex
|
|
|
|
okLock sync.RWMutex
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// Error writes err into w.
|
|
|
|
// Error writes err into w.
|
|
|
@ -40,9 +40,9 @@ func Ok(w http.ResponseWriter) {
|
|
|
|
|
|
|
|
|
|
|
|
// OkJson writes v into w with 200 OK.
|
|
|
|
// OkJson writes v into w with 200 OK.
|
|
|
|
func OkJson(w http.ResponseWriter, v any) {
|
|
|
|
func OkJson(w http.ResponseWriter, v any) {
|
|
|
|
respLock.RLock()
|
|
|
|
okLock.RLock()
|
|
|
|
handler := respHandler
|
|
|
|
handler := okHandler
|
|
|
|
respLock.RUnlock()
|
|
|
|
okLock.RUnlock()
|
|
|
|
if handler != nil {
|
|
|
|
if handler != nil {
|
|
|
|
v = handler(context.Background(), v)
|
|
|
|
v = handler(context.Background(), v)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -51,9 +51,9 @@ func OkJson(w http.ResponseWriter, v any) {
|
|
|
|
|
|
|
|
|
|
|
|
// OkJsonCtx writes v into w with 200 OK.
|
|
|
|
// OkJsonCtx writes v into w with 200 OK.
|
|
|
|
func OkJsonCtx(ctx context.Context, w http.ResponseWriter, v any) {
|
|
|
|
func OkJsonCtx(ctx context.Context, w http.ResponseWriter, v any) {
|
|
|
|
respLock.RLock()
|
|
|
|
okLock.RLock()
|
|
|
|
handlerCtx := respHandler
|
|
|
|
handlerCtx := okHandler
|
|
|
|
respLock.RUnlock()
|
|
|
|
okLock.RUnlock()
|
|
|
|
if handlerCtx != nil {
|
|
|
|
if handlerCtx != nil {
|
|
|
|
v = handlerCtx(ctx, v)
|
|
|
|
v = handlerCtx(ctx, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -80,11 +80,11 @@ func SetErrorHandlerCtx(handlerCtx func(context.Context, error) (int, any)) {
|
|
|
|
errorHandler = handlerCtx
|
|
|
|
errorHandler = handlerCtx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SetResponseHandler sets the response handler, which is called on calling OkJson and OkJsonCtx.
|
|
|
|
// SetOkHandler sets the response handler, which is called on calling OkJson and OkJsonCtx.
|
|
|
|
func SetResponseHandler(handler func(context.Context, any) any) {
|
|
|
|
func SetOkHandler(handler func(context.Context, any) any) {
|
|
|
|
respLock.Lock()
|
|
|
|
okLock.Lock()
|
|
|
|
defer respLock.Unlock()
|
|
|
|
defer okLock.Unlock()
|
|
|
|
respHandler = handler
|
|
|
|
okHandler = handler
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// WriteJson writes v as json string into w with code.
|
|
|
|
// WriteJson writes v as json string into w with code.
|
|
|
|