2026-03-04 18:30:21 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
2026-03-05 14:18:10 +00:00
|
|
|
"fmt"
|
2026-03-05 17:24:50 +00:00
|
|
|
"io"
|
2026-03-04 18:30:21 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/auth"
|
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/html"
|
|
|
|
|
nhttp "github.com/Gleipnir-Technology/nidus-sync/http"
|
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/platform"
|
2026-03-05 14:18:10 +00:00
|
|
|
"github.com/gorilla/schema"
|
2026-03-04 18:30:21 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
)
|
|
|
|
|
|
2026-03-05 14:18:10 +00:00
|
|
|
var decoder = schema.NewDecoder()
|
|
|
|
|
|
2026-03-12 23:49:16 +00:00
|
|
|
type handlerFunctionGet[T any] func(context.Context, *http.Request, platform.User, queryParams) (*T, *nhttp.ErrorWithStatus)
|
2026-03-04 18:30:21 +00:00
|
|
|
type wrappedHandler func(http.ResponseWriter, *http.Request)
|
|
|
|
|
type contentAuthenticated[T any] struct {
|
|
|
|
|
C T
|
|
|
|
|
Config html.ContentConfig
|
|
|
|
|
User platform.User
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ErrorAPI struct {
|
|
|
|
|
Message string `json:"message"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func authenticatedHandlerJSON[T any](f handlerFunctionGet[T]) http.Handler {
|
2026-03-12 23:49:16 +00:00
|
|
|
return auth.NewEnsureAuth(func(w http.ResponseWriter, r *http.Request, u platform.User) {
|
2026-03-04 18:30:21 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
|
var body []byte
|
2026-03-06 14:12:47 +00:00
|
|
|
var params queryParams
|
2026-03-12 23:49:16 +00:00
|
|
|
err := decoder.Decode(¶ms, r.URL.Query())
|
2026-03-06 14:12:47 +00:00
|
|
|
if err != nil {
|
|
|
|
|
log.Error().Err(err).Msg("decode query failure")
|
|
|
|
|
http.Error(w, "failed to decode query", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-03-12 23:49:16 +00:00
|
|
|
resp, e := f(ctx, r, u, params)
|
2026-03-04 18:30:21 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
//log.Info().Str("template", template).Err(e).Msg("handler done")
|
|
|
|
|
if e != nil {
|
2026-03-11 23:52:44 +00:00
|
|
|
log.Warn().Int("status", e.Status).Err(e).Str("user message", e.Message).Msg("Responding with an error from api")
|
2026-03-04 18:30:21 +00:00
|
|
|
body, err = json.Marshal(ErrorAPI{Message: e.Error()})
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Error().Err(err).Msg("failed to marshal error")
|
|
|
|
|
http.Error(w, "{\"message\": \"boom. I can't even tell you what went wrong\"}", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
http.Error(w, string(body), e.Status)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
body, err = json.Marshal(resp)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, "{\"message\": \"failed to marshal json\"}", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
w.Write(body)
|
|
|
|
|
})
|
|
|
|
|
}
|
2026-03-05 14:18:10 +00:00
|
|
|
|
2026-03-12 23:49:16 +00:00
|
|
|
type handlerFunctionPost[ReqType any, ResponseType any] func(context.Context, *http.Request, platform.User, ReqType) (ResponseType, *nhttp.ErrorWithStatus)
|
2026-03-05 14:18:10 +00:00
|
|
|
|
2026-03-05 17:24:50 +00:00
|
|
|
func authenticatedHandlerJSONPost[ReqType any, ResponseType any](f handlerFunctionPost[ReqType, ResponseType]) http.Handler {
|
2026-03-12 23:49:16 +00:00
|
|
|
return auth.NewEnsureAuth(func(w http.ResponseWriter, r *http.Request, u platform.User) {
|
2026-03-05 14:18:10 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2026-03-05 17:24:50 +00:00
|
|
|
var req ReqType
|
|
|
|
|
body, err := io.ReadAll(r.Body)
|
2026-03-05 14:18:10 +00:00
|
|
|
if err != nil {
|
2026-03-05 17:24:50 +00:00
|
|
|
respondError(w, http.StatusInternalServerError, "Failed to read body: %w", err)
|
2026-03-05 14:18:10 +00:00
|
|
|
return
|
|
|
|
|
}
|
2026-03-05 17:24:50 +00:00
|
|
|
err = json.Unmarshal(body, &req)
|
2026-03-05 14:18:10 +00:00
|
|
|
if err != nil {
|
2026-03-05 17:24:50 +00:00
|
|
|
respondError(w, http.StatusBadRequest, "Failed to decode request: %w", err)
|
2026-03-05 14:18:10 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ctx := r.Context()
|
2026-03-12 23:49:16 +00:00
|
|
|
response, e := f(ctx, r, u, req)
|
2026-03-05 14:18:10 +00:00
|
|
|
if e != nil {
|
2026-03-11 23:52:44 +00:00
|
|
|
log.Warn().Int("status", e.Status).Err(e).Str("user message", e.Message).Msg("Responding with an error from api")
|
|
|
|
|
body, err = json.Marshal(ErrorAPI{Message: e.Error()})
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Error().Err(err).Msg("failed to marshal error")
|
|
|
|
|
http.Error(w, "{\"message\": \"boom. I can't even tell you what went wrong\"}", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
http.Error(w, string(body), e.Status)
|
2026-03-05 14:18:10 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
resp_body, err := json.Marshal(response)
|
|
|
|
|
if err != nil {
|
|
|
|
|
respondError(w, http.StatusInternalServerError, "Failed to marshal json response: %w", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
w.Write(resp_body)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func respondError(w http.ResponseWriter, status int, format string, args ...any) {
|
|
|
|
|
outer_err := fmt.Errorf(format, args...)
|
|
|
|
|
body, err := json.Marshal(ErrorAPI{
|
|
|
|
|
Message: outer_err.Error(),
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, "{\"message\": \"failed to marshal json\"}", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
http.Error(w, string(body), status)
|
|
|
|
|
}
|