mirror of
https://github.com/hanzoai/o11y.git
synced 2026-10-15 12:58:33 +00:00
The 28 reads across dashboards, saved views and preferences named
gorilla/mux to answer "which id is this request about?". Which router
matched a route is not a fact a handler should know; the VALUE it needs
is the segment. All 28 now go through coretypes.Param.
Query values are untouched: r.URL.Query().Get stays exactly as it was.
/rules/{id} is a PATH segment and ?limit=25 is a QUERY value, and the
seam exists precisely so those two cannot be confused.
Two shapes of read collapse to one. `nameString, ok := mux.Vars(r)["name"]`
becomes `nameString := coretypes.Param(r, "name")` with the ok-check
becoming an empty-check: a route with {name} cannot match an empty
segment, so present-but-empty was never reachable and absence still
answers "".
The two public-dashboard gates held byte-identical copies of the same
subject lookup. They are now one method, provider.publicDashboardSelectors.
That gate runs before any identity exists — it resolves WHO the request is
about from the path — so two copies is two places for the answer to drift.
Tests drive the REAL handlers through a REAL router at the REAL templates
and assert on what reached the module, not on a status code. Asserting a
reader against its own injector would prove only that the two halves of
coretypes agree with each other. Verified by mutation: with Param stubbed
to "" all four packages go red.
Registration is untouched — {id} literals and *mux.Router stay put for the
one atomic commit that moves them.
Co-authored-by: Hanzo Dev <[email protected]>
201 lines
4.6 KiB
Go
201 lines
4.6 KiB
Go
package implpreference
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/hanzoai/o11y/pkg/errors"
|
|
"github.com/hanzoai/o11y/pkg/http/render"
|
|
"github.com/hanzoai/o11y/pkg/modules/preference"
|
|
"github.com/hanzoai/o11y/pkg/types/authtypes"
|
|
"github.com/hanzoai/o11y/pkg/types/coretypes"
|
|
"github.com/hanzoai/o11y/pkg/types/preferencetypes"
|
|
"github.com/hanzoai/o11y/pkg/valuer"
|
|
)
|
|
|
|
type handler struct {
|
|
module preference.Module
|
|
}
|
|
|
|
func NewHandler(module preference.Module) preference.Handler {
|
|
return &handler{module: module}
|
|
}
|
|
|
|
func (handler *handler) ListByOrg(rw http.ResponseWriter, r *http.Request) {
|
|
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
|
|
defer cancel()
|
|
|
|
claims, err := authtypes.ClaimsFromContext(ctx)
|
|
if err != nil {
|
|
render.Error(rw, err)
|
|
return
|
|
}
|
|
|
|
preferences, err := handler.module.ListByOrg(ctx, valuer.MustNewUUID(claims.OrgID))
|
|
if err != nil {
|
|
render.Error(rw, err)
|
|
return
|
|
}
|
|
|
|
render.Success(rw, http.StatusOK, preferences)
|
|
}
|
|
|
|
func (handler *handler) GetByOrg(rw http.ResponseWriter, r *http.Request) {
|
|
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
|
|
defer cancel()
|
|
|
|
claims, err := authtypes.ClaimsFromContext(ctx)
|
|
if err != nil {
|
|
render.Error(rw, err)
|
|
return
|
|
}
|
|
|
|
nameString := coretypes.Param(r, "name")
|
|
if nameString == "" {
|
|
render.Error(rw, errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "name is required"))
|
|
return
|
|
}
|
|
|
|
name, err := preferencetypes.NewName(nameString)
|
|
if err != nil {
|
|
render.Error(rw, err)
|
|
return
|
|
}
|
|
|
|
preference, err := handler.module.GetByOrg(ctx, valuer.MustNewUUID(claims.OrgID), name)
|
|
if err != nil {
|
|
render.Error(rw, err)
|
|
return
|
|
}
|
|
|
|
render.Success(rw, http.StatusOK, preference)
|
|
}
|
|
|
|
func (handler *handler) UpdateByOrg(rw http.ResponseWriter, r *http.Request) {
|
|
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
|
|
defer cancel()
|
|
|
|
claims, err := authtypes.ClaimsFromContext(ctx)
|
|
if err != nil {
|
|
render.Error(rw, err)
|
|
return
|
|
}
|
|
|
|
nameString := coretypes.Param(r, "name")
|
|
if nameString == "" {
|
|
render.Error(rw, errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "name is required"))
|
|
return
|
|
}
|
|
|
|
name, err := preferencetypes.NewName(nameString)
|
|
if err != nil {
|
|
render.Error(rw, err)
|
|
return
|
|
}
|
|
|
|
req := new(preferencetypes.UpdatablePreference)
|
|
err = json.NewDecoder(r.Body).Decode(req)
|
|
if err != nil {
|
|
render.Error(rw, err)
|
|
return
|
|
}
|
|
|
|
err = handler.module.UpdateByOrg(ctx, valuer.MustNewUUID(claims.OrgID), name, req.Value)
|
|
if err != nil {
|
|
render.Error(rw, err)
|
|
return
|
|
}
|
|
|
|
render.Success(rw, http.StatusNoContent, nil)
|
|
}
|
|
|
|
func (handler *handler) ListByUser(rw http.ResponseWriter, r *http.Request) {
|
|
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
|
|
defer cancel()
|
|
|
|
claims, err := authtypes.ClaimsFromContext(ctx)
|
|
if err != nil {
|
|
render.Error(rw, err)
|
|
return
|
|
}
|
|
|
|
preferences, err := handler.module.ListByUser(ctx, valuer.MustNewUUID(claims.UserID))
|
|
if err != nil {
|
|
render.Error(rw, err)
|
|
return
|
|
}
|
|
|
|
render.Success(rw, http.StatusOK, preferences)
|
|
}
|
|
|
|
func (handler *handler) GetByUser(rw http.ResponseWriter, r *http.Request) {
|
|
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
|
|
defer cancel()
|
|
|
|
claims, err := authtypes.ClaimsFromContext(ctx)
|
|
if err != nil {
|
|
render.Error(rw, err)
|
|
return
|
|
}
|
|
|
|
nameString := coretypes.Param(r, "name")
|
|
if nameString == "" {
|
|
render.Error(rw, errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "name is required"))
|
|
return
|
|
}
|
|
|
|
name, err := preferencetypes.NewName(nameString)
|
|
if err != nil {
|
|
render.Error(rw, err)
|
|
return
|
|
}
|
|
|
|
preference, err := handler.module.GetByUser(ctx, valuer.MustNewUUID(claims.UserID), name)
|
|
if err != nil {
|
|
render.Error(rw, err)
|
|
return
|
|
}
|
|
|
|
render.Success(rw, http.StatusOK, preference)
|
|
}
|
|
|
|
func (handler *handler) UpdateByUser(rw http.ResponseWriter, r *http.Request) {
|
|
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
|
|
defer cancel()
|
|
|
|
claims, err := authtypes.ClaimsFromContext(ctx)
|
|
if err != nil {
|
|
render.Error(rw, err)
|
|
return
|
|
}
|
|
|
|
nameString := coretypes.Param(r, "name")
|
|
if nameString == "" {
|
|
render.Error(rw, errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "name is required"))
|
|
return
|
|
}
|
|
|
|
name, err := preferencetypes.NewName(nameString)
|
|
if err != nil {
|
|
render.Error(rw, err)
|
|
return
|
|
}
|
|
|
|
req := new(preferencetypes.UpdatablePreference)
|
|
err = json.NewDecoder(r.Body).Decode(req)
|
|
if err != nil {
|
|
render.Error(rw, err)
|
|
return
|
|
}
|
|
|
|
err = handler.module.UpdateByUser(ctx, valuer.MustNewUUID(claims.UserID), name, req.Value)
|
|
if err != nil {
|
|
render.Error(rw, err)
|
|
return
|
|
}
|
|
|
|
render.Success(rw, http.StatusNoContent, nil)
|
|
}
|