Files
zeekayandHanzo Dev 308c4b7372 dashboards, views, preferences: a path segment is read by name, not by router
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]>
2026-08-01 18:56:25 -07:00

133 lines
3.0 KiB
Go

package impldashboard
import (
"context"
"net/http"
"time"
"github.com/hanzoai/o11y/pkg/errors"
"github.com/hanzoai/o11y/pkg/http/binding"
"github.com/hanzoai/o11y/pkg/http/render"
"github.com/hanzoai/o11y/pkg/types/authtypes"
"github.com/hanzoai/o11y/pkg/types/coretypes"
"github.com/hanzoai/o11y/pkg/types/dashboardtypes"
"github.com/hanzoai/o11y/pkg/valuer"
)
func (handler *handler) CreateView(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
}
orgID := valuer.MustNewUUID(claims.OrgID)
var req dashboardtypes.PostableDashboardView
if err := binding.JSON.BindBody(r.Body, &req); err != nil {
render.Error(rw, err)
return
}
view, err := handler.module.CreateView(ctx, orgID, req)
if err != nil {
render.Error(rw, err)
return
}
render.Success(rw, http.StatusCreated, view)
}
func (handler *handler) ListViews(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
}
orgID := valuer.MustNewUUID(claims.OrgID)
out, err := handler.module.ListViews(ctx, orgID)
if err != nil {
render.Error(rw, err)
return
}
render.Success(rw, http.StatusOK, out)
}
func (handler *handler) UpdateView(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
}
orgID := valuer.MustNewUUID(claims.OrgID)
id := coretypes.Param(r, "id")
if id == "" {
render.Error(rw, errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "id is missing in the path"))
return
}
viewID, err := valuer.NewUUID(id)
if err != nil {
render.Error(rw, err)
return
}
var req dashboardtypes.UpdatableDashboardView
if err := binding.JSON.BindBody(r.Body, &req); err != nil {
render.Error(rw, err)
return
}
view, err := handler.module.UpdateView(ctx, orgID, viewID, req)
if err != nil {
render.Error(rw, err)
return
}
render.Success(rw, http.StatusOK, view)
}
func (handler *handler) DeleteView(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
}
orgID := valuer.MustNewUUID(claims.OrgID)
id := coretypes.Param(r, "id")
if id == "" {
render.Error(rw, errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "id is missing in the path"))
return
}
viewID, err := valuer.NewUUID(id)
if err != nil {
render.Error(rw, err)
return
}
if err := handler.module.DeleteView(ctx, orgID, viewID); err != nil {
render.Error(rw, err)
return
}
render.Success(rw, http.StatusNoContent, nil)
}