feat: introduce cluster agent

This commit is contained in:
2025-05-13 21:59:44 +02:00
parent ee0489dcbb
commit 49014c951f
12 changed files with 485 additions and 18 deletions

View File

@@ -6,6 +6,7 @@ import (
"log"
"net/http"
"github.com/0x1d/rcond/pkg/cluster"
network "github.com/0x1d/rcond/pkg/network"
"github.com/0x1d/rcond/pkg/system"
"github.com/0x1d/rcond/pkg/user"
@@ -287,3 +288,24 @@ func HandleShutdown(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"status": "success"})
}
func ClusterAgentWrapper(agent *cluster.Agent) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
HandleClusterMembers(w, r, agent)
}
}
func HandleClusterMembers(w http.ResponseWriter, r *http.Request, agent *cluster.Agent) {
if agent == nil {
writeError(w, "cluster agent is not initialized", http.StatusInternalServerError)
return
}
members, err := agent.Members()
if err != nil {
writeError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(members)
}

View File

@@ -6,14 +6,16 @@ import (
"net/http"
"time"
"github.com/0x1d/rcond/pkg/cluster"
"github.com/0x1d/rcond/pkg/config"
"github.com/gorilla/mux"
)
type Server struct {
router *mux.Router
srv *http.Server
apiToken string
router *mux.Router
srv *http.Server
apiToken string
clusterAgent *cluster.Agent
}
func NewServer(cfg *config.Config) *Server {
@@ -37,6 +39,11 @@ func NewServer(cfg *config.Config) *Server {
}
}
func (s *Server) WithClusterAgent(agent *cluster.Agent) *Server {
s.clusterAgent = agent
return s
}
func (s *Server) Start() error {
return s.srv.ListenAndServe()
}
@@ -69,6 +76,7 @@ func (s *Server) RegisterRoutes() {
s.router.HandleFunc("/users/{user}/keys/{fingerprint}", s.verifyToken(HandleRemoveAuthorizedKey)).Methods(http.MethodDelete)
s.router.HandleFunc("/system/restart", s.verifyToken(HandleReboot)).Methods(http.MethodPost)
s.router.HandleFunc("/system/shutdown", s.verifyToken(HandleShutdown)).Methods(http.MethodPost)
s.router.HandleFunc("/cluster/members", s.verifyToken(ClusterAgentWrapper(s.clusterAgent))).Methods(http.MethodGet)
}
func (s *Server) healthHandler(w http.ResponseWriter, r *http.Request) {