emissary/internal/server/api/mount.go

48 lines
1.3 KiB
Go

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
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.Route("/agents", func(r chi.Router) {
r.Post("/claim", m.claimAgent)
r.With(assertGlobalReadAccess).Get("/", m.queryAgents)
r.With(assertAgentReadAccess).Get("/{agentID}", m.getAgent)
r.With(assertAgentWriteAccess).Put("/{agentID}", m.updateAgent)
r.With(assertAgentWriteAccess).Delete("/{agentID}", m.deleteAgent)
r.With(assertAgentReadAccess).Get("/{agentID}/specs", m.getAgentSpecs)
r.With(assertAgentWriteAccess).Post("/{agentID}/specs", m.updateSpec)
r.With(assertAgentWriteAccess).Delete("/{agentID}/specs", m.deleteSpec)
})
})
}
func (m *Mount) notFound(w http.ResponseWriter, r *http.Request) {
api.ErrorResponse(w, http.StatusNotFound, ErrCodeNotFound, nil)
}
func NewMount(agentRepo datastore.AgentRepository, authenticators ...auth.Authenticator) *Mount {
return &Mount{agentRepo, authenticators}
}