pomme/internal/logger/logger.go
2023-02-24 22:36:53 -05:00

45 lines
831 B
Go

package logger
import (
"fmt"
"net/http"
"github.com/go-chi/render"
)
type HTTPResponder interface {
comparable
string | int | bool
}
type Response[T string, R int] struct {
Message, Realm, Err T
Status R
}
func NewResponder[T string, R int]() Response[T, R] {
return Response[T, R]{}
}
func (resp *Response[T, R]) NewMessage(w http.ResponseWriter, r *http.Request, e Response[T, R]) {
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
switch e.Realm {
default:
w.Header().Add("WWW-Authenticate", fmt.Sprintf(`realm="%s"`, e.Realm))
fallthrough
case "":
w.Header().Add("API Error", e.Message)
}
w.WriteHeader(e.Status)
v := map[string]any{
"message": message,
"status": status,
}
render.JSON(w, r, v)
return
}