37 lines
769 B
Go
37 lines
769 B
Go
/*
|
|
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
|
*/
|
|
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"mbase/app/router"
|
|
)
|
|
|
|
type Response[T any] struct {
|
|
Error bool `json:"error" yaml:"error"`
|
|
Message string `json:"message,omitempty" yaml:"message,omitempty"`
|
|
Result T `json:"result,omitempty" yaml:"result,result"`
|
|
}
|
|
|
|
func NewResponse[T any]() *Response[T] {
|
|
return &Response[T]{}
|
|
}
|
|
|
|
func (hand *Handler) SendResult(rctx *router.Context, result any) {
|
|
response := &Response[any]{
|
|
Error: false,
|
|
Result: result,
|
|
}
|
|
rctx.SendJSON(http.StatusOK, response)
|
|
}
|
|
|
|
func (hand *Handler) SendError(rctx *router.Context, err error) {
|
|
response := &Response[any]{
|
|
Error: true,
|
|
Message: err.Error(),
|
|
}
|
|
rctx.SendJSON(http.StatusOK, response)
|
|
}
|