Files
upstream-ory-keto/internal/namespace/namespacehandler/handler.go
upstream-mirror 9456a4e922
Some checks failed
CI / Run lints and checks (push) Failing after 47s
CI / Run tests (push) Failing after 1s
CI / Run race tests (push) Failing after 5s
CI / Build CLI docs (push) Has been skipped
CI / Run buf actions (push) Failing after 5s
CodeQL / Analyze (go) (push) Failing after 3s
CodeQL / Analyze (javascript) (push) Failing after 2s
Docker Image Scanners / scanners (push) Failing after 3s
Format / format (push) Failing after 3s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Draft newsletter (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
snapshot of https://github.com/ory/keto.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:08:34 +05:00

104 lines
2.9 KiB
Go

// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package namespacehandler
import (
"context"
"net/http"
"connectrpc.com/connect"
"google.golang.org/protobuf/reflect/protoreflect"
"github.com/ory/herodot"
"github.com/ory/x/httprouterx"
"github.com/ory/x/httpx"
"github.com/ory/x/logrusx"
rts "github.com/ory/keto/gen/go/ory/keto/relation_tuples/v1alpha2"
"github.com/ory/keto/gen/go/ory/keto/relation_tuples/v1alpha2/relationtuplesconnect"
"github.com/ory/keto/internal/driver/config"
"github.com/ory/keto/internal/x"
)
type (
handlerDeps interface {
logrusx.Provider
httpx.WriterProvider
config.Provider
x.HandlerOptionsProvider
}
Handler struct {
relationtuplesconnect.UnimplementedNamespacesServiceHandler
d handlerDeps
}
)
const (
RouteBase = "/namespaces"
)
func New(d handlerDeps) *Handler { return &Handler{d: d} }
func (h *Handler) RegisterReadRoutes(r *httprouterx.RouterPublic) {
r.GET(RouteBase, h.getNamespaces)
listNamespacesHandler := connect.NewUnaryHandler(
relationtuplesconnect.NamespacesServiceListNamespacesProcedure,
h.ListNamespaces,
connect.WithSchema(rts.File_ory_keto_relation_tuples_v1alpha2_namespaces_service_proto.
Services().ByName("NamespacesService").
Methods().ByName("ListNamespaces")),
connect.WithHandlerOptions(h.d.HandlerOptions()...),
)
r.Handle(relationtuplesconnect.NamespacesServiceListNamespacesProcedure, listNamespacesHandler)
}
func (h *Handler) ProtoFiles() []protoreflect.FileDescriptor {
return []protoreflect.FileDescriptor{rts.File_ory_keto_relation_tuples_v1alpha2_namespaces_service_proto}
}
func (h *Handler) ListNamespaces(ctx context.Context, _ *connect.Request[rts.ListNamespacesRequest]) (*connect.Response[rts.ListNamespacesResponse], error) {
m, err := h.d.Config(ctx).NamespaceManager()
if err != nil {
h.d.Logger().WithError(err).Errorf("could not get namespace manager")
return nil, herodot.ErrInternalServerError()
}
namespaces, err := m.Namespaces(ctx)
if err != nil {
h.d.Logger().WithError(err).Errorf("could not get namespaces")
return nil, herodot.ErrInternalServerError()
}
apiNamespaces := make([]*rts.Namespace, len(namespaces))
for i, n := range namespaces {
apiNamespaces[i] = &rts.Namespace{Name: n.Name}
}
return connect.NewResponse(&rts.ListNamespacesResponse{Namespaces: apiNamespaces}), nil
}
// swagger:route GET /namespaces relationship listRelationshipNamespaces
//
// # Query namespaces
//
// Get all namespaces
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 200: relationshipNamespaces
// default: errorGeneric
//
// Extensions:
// x-ory-ratelimit-bucket: keto-admin-low
func (h *Handler) getNamespaces(w http.ResponseWriter, r *http.Request) {
res, err := h.ListNamespaces(r.Context(), nil)
if err != nil {
h.d.Writer().WriteError(w, r, err)
return
}
h.d.Writer().Write(w, r, res.Msg)
}