package api import ( "net/http" "forge.cadoles.com/Cadoles/emissary/internal/auth" "forge.cadoles.com/Cadoles/emissary/internal/datastore" "github.com/go-chi/chi/v5" "gitlab.com/wpetit/goweb/api" ) type Mount struct { agentRepo datastore.AgentRepository tenantRepo datastore.TenantRepository authenticators []auth.Authenticator } func (m *Mount) Mount(r chi.Router) { r.NotFound(m.notFound) r.Post("/register", m.registerAgent) r.Group(func(r chi.Router) { r.Use(auth.Middleware(m.authenticators...)) r.Get("/session", m.getSession) r.Route("/agents", func(r chi.Router) { r.With(assertUserWithWriteAccess).Post("/claim", m.claimAgent) r.With(assertQueryAccess).Get("/", m.queryAgents) r.With(assertAgentOrUserWithReadAccess).Get("/{agentID}", m.getAgent) r.With(assertAgentOrUserWithWriteAccess).Put("/{agentID}", m.updateAgent) r.With(assertUserWithWriteAccess).Delete("/{agentID}", m.deleteAgent) r.With(assertAgentOrUserWithReadAccess).Get("/{agentID}/specs", m.getAgentSpecs) r.With(assertUserWithWriteAccess).Post("/{agentID}/specs", m.updateSpec) r.With(assertUserWithWriteAccess).Delete("/{agentID}/specs", m.deleteSpec) }) r.Route("/tenants", func(r chi.Router) { r.With(assertQueryAccess).Get("/", m.queryTenants) r.With(assertAdminAccess).Post("/", m.createTenant) r.With(assertAdminOrTenantReadAccess).Get("/{tenantID}", m.getTenant) r.With(assertAdminOrTenantWriteAccess).Put("/{tenantID}", m.updateTenant) r.With(assertAdminAccess).Delete("/{tenantID}", m.deleteTenant) }) }) } func (m *Mount) notFound(w http.ResponseWriter, r *http.Request) { api.ErrorResponse(w, http.StatusNotFound, ErrCodeNotFound, nil) } func NewMount(agentRepo datastore.AgentRepository, tenantRepo datastore.TenantRepository, authenticators ...auth.Authenticator) *Mount { return &Mount{agentRepo, tenantRepo, authenticators} }