package centrifugo import ( "context" "errors" "git.golem15.com/golem15/summercms/modules/backpack" "git.golem15.com/golem15/summercms/modules/bonfire" ) // HealthCommandName is the name of the Centrifugo health check command. const HealthCommandName = "websockets:health" // errHealthFailed is returned by the health command after it has printed // why the check failed, so the binary exits 1. var errHealthFailed = errors.New(HealthCommandName + ": check failed") // Commands returns the Centrifugo console commands for an application // binary: the HealthCommandName check. An application appends them to the // commands its plugin returns from Commands. func Commands(app *backpack.App) []bonfire.Command { return []bonfire.Command{healthCommand(app)} } // healthCommand ports the WinterCMS CentrifugoHealthCheck command. The // WinterCMS command only read the local configuration; this one also calls // the Centrifugo info API, so a wrong URL or key fails the check. The API // key is never printed. func healthCommand(app *backpack.App) bonfire.Command { return bonfire.Command{ Name: HealthCommandName, Description: "Check Centrifugo connection health", Run: func(ctx context.Context, _ bonfire.Input, out bonfire.Output) error { var cfg Config if app != nil { cfg = LoadConfig(app.Config) } else { cfg = LoadConfig(nil) } client := NewClient(cfg, nil) if !client.Enabled() { out.Error("Centrifugo not configured (API key missing)") out.Println("Set SUMMER_REALTIME__CENTRIFUGO__API_KEY in the environment") return errHealthFailed } out.Info("Checking Centrifugo connection...") out.Println("API URL: " + cfg.APIURL) if _, err := client.Info(ctx); err != nil { out.Error("Connection check failed: " + err.Error()) return errHealthFailed } info := client.DebugInfo() out.Success("Configuration OK") out.Table([]string{"Setting", "Value"}, [][]string{ {"API URL", info.APIURL}, {"Enabled", yesNo(info.Enabled)}, {"API Key Set", yesNo(info.APIKeySet)}, }) return nil }, } } func yesNo(b bool) string { if b { return "Yes" } return "No" }