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

@@ -8,6 +8,7 @@ import (
"log"
"os"
"github.com/0x1d/rcond/pkg/cluster"
"github.com/0x1d/rcond/pkg/config"
http "github.com/0x1d/rcond/pkg/http"
)
@@ -25,15 +26,11 @@ func main() {
os.Exit(1)
}
srv := http.NewServer(appConfig)
srv.RegisterRoutes()
clusterAgent := startClusterAgent(appConfig)
startApiServer(appConfig, clusterAgent)
log.Printf("Starting server on %s", appConfig.Rcond.Addr)
if err := srv.Start(); err != nil {
log.Fatal(err)
}
select {}
}
func loadConfig() (*config.Config, error) {
configPath := "/etc/rcond/config.yaml"
appConfig := &config.Config{}
@@ -81,6 +78,41 @@ func loadConfig() (*config.Config, error) {
return appConfig, nil
}
func startApiServer(appConfig *config.Config, clusterAgent *cluster.Agent) *http.Server {
srv := http.NewServer(appConfig)
srv.WithClusterAgent(clusterAgent)
srv.RegisterRoutes()
log.Printf("Starting API server on %s", appConfig.Rcond.Addr)
if err := srv.Start(); err != nil {
log.Fatal(err)
}
return srv
}
func startClusterAgent(appConfig *config.Config) *cluster.Agent {
clusterConfig := &appConfig.Cluster
if clusterConfig.Enabled {
log.Printf("Starting cluster agent on %s:%d", clusterConfig.BindAddr, clusterConfig.BindPort)
clusterAgent, err := cluster.NewAgent(clusterConfig)
if err != nil {
log.Fatal(err)
}
// join nodes in the cluster if the join addresses are provided
if len(clusterConfig.Join) > 0 {
clusterAgent.Join(clusterConfig.Join, true)
}
// get members in the cluster
members, err := clusterAgent.Members()
if err != nil {
log.Fatal(err)
}
log.Printf("Members: %v", members)
return clusterAgent
}
return nil
}
func overrideConfigValuesFromEnv(envMap map[string]*string) {
for varName, configValue := range envMap {
if envValue, ok := os.LookupEnv(varName); ok {