From 45494f51b5c66ca4c6478a065146f29d7b713a2a Mon Sep 17 00:00:00 2001 From: Jonatan Wallmander Date: Thu, 14 Nov 2024 15:49:04 +0100 Subject: [PATCH] feat: config supports toggling HTTP and Stats servers on/off (#594) --- cmd/soft/serve/server.go | 73 +++++++++++++++++++++++++--------------- pkg/config/config.go | 20 +++++++++++ 2 files changed, 65 insertions(+), 28 deletions(-) diff --git a/cmd/soft/serve/server.go b/cmd/soft/serve/server.go index 33679091..c1379340 100644 --- a/cmd/soft/serve/server.go +++ b/cmd/soft/serve/server.go @@ -93,34 +93,51 @@ func NewServer(ctx context.Context) (*Server, error) { // Start starts the SSH server. func (s *Server) Start() error { errg, _ := errgroup.WithContext(s.ctx) - errg.Go(func() error { - s.logger.Print("Starting Git daemon", "addr", s.Config.Git.ListenAddr) - if err := s.GitDaemon.Start(); !errors.Is(err, daemon.ErrServerClosed) { - return err - } - return nil - }) - errg.Go(func() error { - s.logger.Print("Starting HTTP server", "addr", s.Config.HTTP.ListenAddr) - if err := s.HTTPServer.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) { - return err - } - return nil - }) - errg.Go(func() error { - s.logger.Print("Starting SSH server", "addr", s.Config.SSH.ListenAddr) - if err := s.SSHServer.ListenAndServe(); !errors.Is(err, ssh.ErrServerClosed) { - return err - } - return nil - }) - errg.Go(func() error { - s.logger.Print("Starting Stats server", "addr", s.Config.Stats.ListenAddr) - if err := s.StatsServer.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) { - return err - } - return nil - }) + + // optionally start the SSH server + if s.Config.SSH.Enabled { + errg.Go(func() error { + s.logger.Print("Starting SSH server", "addr", s.Config.SSH.ListenAddr) + if err := s.SSHServer.ListenAndServe(); !errors.Is(err, ssh.ErrServerClosed) { + return err + } + return nil + }) + } + + // optionally start the git daemon + if s.Config.Git.Enabled { + errg.Go(func() error { + s.logger.Print("Starting Git daemon", "addr", s.Config.Git.ListenAddr) + if err := s.GitDaemon.Start(); !errors.Is(err, daemon.ErrServerClosed) { + return err + } + return nil + }) + } + + // optionally start the HTTP server + if s.Config.HTTP.Enabled { + errg.Go(func() error { + s.logger.Print("Starting HTTP server", "addr", s.Config.HTTP.ListenAddr) + if err := s.HTTPServer.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) { + return err + } + return nil + }) + } + + // optionally start the Stats server + if s.Config.Stats.Enabled { + errg.Go(func() error { + s.logger.Print("Starting Stats server", "addr", s.Config.Stats.ListenAddr) + if err := s.StatsServer.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) { + return err + } + return nil + }) + } + errg.Go(func() error { s.Cron.Start() return nil diff --git a/pkg/config/config.go b/pkg/config/config.go index 2b650984..e47579de 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -18,6 +18,9 @@ var binPath = "soft" // SSHConfig is the configuration for the SSH server. type SSHConfig struct { + // Enabled toggles the SSH server on/off + Enabled bool `env:"SSH_ENABLED" yaml:"ssh_enabled"` + // ListenAddr is the address on which the SSH server will listen. ListenAddr string `env:"LISTEN_ADDR" yaml:"listen_addr"` @@ -39,6 +42,9 @@ type SSHConfig struct { // GitConfig is the Git daemon configuration for the server. type GitConfig struct { + // Enabled toggles the Git daemon on/off + Enabled bool `env:"GIT_ENABLED" yaml:"git_enabled"` + // ListenAddr is the address on which the Git daemon will listen. ListenAddr string `env:"LISTEN_ADDR" yaml:"listen_addr"` @@ -57,6 +63,9 @@ type GitConfig struct { // HTTPConfig is the HTTP configuration for the server. type HTTPConfig struct { + // Enabled toggles the HTTP server on/off + Enabled bool `env:"HTTP_ENABLED" yaml:"http_enabled"` + // ListenAddr is the address on which the HTTP server will listen. ListenAddr string `env:"LISTEN_ADDR" yaml:"listen_addr"` @@ -72,6 +81,9 @@ type HTTPConfig struct { // StatsConfig is the configuration for the stats server. type StatsConfig struct { + // Enabled toggles the Stats server on/off + Enabled bool `env:"STATS_ENABLED" yaml:"stats_enabled"` + // ListenAddr is the address on which the stats server will listen. ListenAddr string `env:"LISTEN_ADDR" yaml:"listen_addr"` } @@ -165,21 +177,25 @@ func (c *Config) Environ() []string { fmt.Sprintf("SOFT_SERVE_DATA_PATH=%s", c.DataPath), fmt.Sprintf("SOFT_SERVE_NAME=%s", c.Name), fmt.Sprintf("SOFT_SERVE_INITIAL_ADMIN_KEYS=%s", strings.Join(c.InitialAdminKeys, "\n")), + fmt.Sprintf("SOFT_SERVE_SSH_ENABLED=%s", c.SSH.Enabled), fmt.Sprintf("SOFT_SERVE_SSH_LISTEN_ADDR=%s", c.SSH.ListenAddr), fmt.Sprintf("SOFT_SERVE_SSH_PUBLIC_URL=%s", c.SSH.PublicURL), fmt.Sprintf("SOFT_SERVE_SSH_KEY_PATH=%s", c.SSH.KeyPath), fmt.Sprintf("SOFT_SERVE_SSH_CLIENT_KEY_PATH=%s", c.SSH.ClientKeyPath), fmt.Sprintf("SOFT_SERVE_SSH_MAX_TIMEOUT=%d", c.SSH.MaxTimeout), fmt.Sprintf("SOFT_SERVE_SSH_IDLE_TIMEOUT=%d", c.SSH.IdleTimeout), + fmt.Sprintf("SOFT_SERVE_GIT_ENABLED=%s", c.Git.Enabled), fmt.Sprintf("SOFT_SERVE_GIT_LISTEN_ADDR=%s", c.Git.ListenAddr), fmt.Sprintf("SOFT_SERVE_GIT_PUBLIC_URL=%s", c.Git.PublicURL), fmt.Sprintf("SOFT_SERVE_GIT_MAX_TIMEOUT=%d", c.Git.MaxTimeout), fmt.Sprintf("SOFT_SERVE_GIT_IDLE_TIMEOUT=%d", c.Git.IdleTimeout), fmt.Sprintf("SOFT_SERVE_GIT_MAX_CONNECTIONS=%d", c.Git.MaxConnections), + fmt.Sprintf("SOFT_SERVE_HTTP_ENABLED=%s", c.HTTP.Enabled), fmt.Sprintf("SOFT_SERVE_HTTP_LISTEN_ADDR=%s", c.HTTP.ListenAddr), fmt.Sprintf("SOFT_SERVE_HTTP_TLS_KEY_PATH=%s", c.HTTP.TLSKeyPath), fmt.Sprintf("SOFT_SERVE_HTTP_TLS_CERT_PATH=%s", c.HTTP.TLSCertPath), fmt.Sprintf("SOFT_SERVE_HTTP_PUBLIC_URL=%s", c.HTTP.PublicURL), + fmt.Sprintf("SOFT_SERVE_STATS_ENABLED=%s", c.Stats.Enabled), fmt.Sprintf("SOFT_SERVE_STATS_LISTEN_ADDR=%s", c.Stats.ListenAddr), fmt.Sprintf("SOFT_SERVE_LOG_FORMAT=%s", c.Log.Format), fmt.Sprintf("SOFT_SERVE_LOG_TIME_FORMAT=%s", c.Log.TimeFormat), @@ -318,6 +334,7 @@ func DefaultConfig() *Config { Name: "Soft Serve", DataPath: DefaultDataPath(), SSH: SSHConfig{ + Enabled: true, ListenAddr: ":23231", PublicURL: "ssh://localhost:23231", KeyPath: filepath.Join("ssh", "soft_serve_host_ed25519"), @@ -326,6 +343,7 @@ func DefaultConfig() *Config { IdleTimeout: 10 * 60, // 10 minutes }, Git: GitConfig{ + Enabled: true, ListenAddr: ":9418", PublicURL: "git://localhost", MaxTimeout: 0, @@ -333,10 +351,12 @@ func DefaultConfig() *Config { MaxConnections: 32, }, HTTP: HTTPConfig{ + Enabled: true, ListenAddr: ":23232", PublicURL: "http://localhost:23232", }, Stats: StatsConfig{ + Enabled: true, ListenAddr: "localhost:23233", }, Log: LogConfig{