89 lines
1.8 KiB
Go
89 lines
1.8 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
"net"
|
|
"net/url"
|
|
"strings"
|
|
)
|
|
|
|
// Validate reports whether cfg stores a usable Game Lobby Service process
|
|
// configuration.
|
|
func (cfg Config) Validate() error {
|
|
if cfg.ShutdownTimeout <= 0 {
|
|
return fmt.Errorf("%s must be positive", shutdownTimeoutEnvVar)
|
|
}
|
|
if err := validateSlogLevel(cfg.Logging.Level); err != nil {
|
|
return fmt.Errorf("%s: %w", logLevelEnvVar, err)
|
|
}
|
|
if err := cfg.PublicHTTP.Validate(); err != nil {
|
|
return err
|
|
}
|
|
if err := cfg.InternalHTTP.Validate(); err != nil {
|
|
return err
|
|
}
|
|
if err := cfg.Redis.Validate(); err != nil {
|
|
return err
|
|
}
|
|
if err := cfg.UserService.Validate(); err != nil {
|
|
return err
|
|
}
|
|
if err := cfg.GM.Validate(); err != nil {
|
|
return err
|
|
}
|
|
if err := cfg.EnrollmentAutomation.Validate(); err != nil {
|
|
return err
|
|
}
|
|
if err := cfg.RaceNameDirectory.Validate(); err != nil {
|
|
return fmt.Errorf("%s: %w", raceNameDirectoryBackendEnvVar, err)
|
|
}
|
|
if err := cfg.PendingRegistration.Validate(); err != nil {
|
|
return fmt.Errorf("%s: %w", raceNameExpirationIntervalEnvVar, err)
|
|
}
|
|
if err := cfg.Telemetry.Validate(); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func validateSlogLevel(level string) error {
|
|
var slogLevel slog.Level
|
|
if err := slogLevel.UnmarshalText([]byte(strings.TrimSpace(level))); err != nil {
|
|
return fmt.Errorf("invalid slog level %q: %w", level, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func isTCPAddr(value string) bool {
|
|
host, port, err := net.SplitHostPort(strings.TrimSpace(value))
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
if port == "" {
|
|
return false
|
|
}
|
|
|
|
if host == "" {
|
|
return true
|
|
}
|
|
|
|
return !strings.Contains(host, " ")
|
|
}
|
|
|
|
func isHTTPURL(value string) bool {
|
|
parsed, err := url.Parse(strings.TrimSpace(value))
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
if parsed.Scheme != "http" && parsed.Scheme != "https" {
|
|
return false
|
|
}
|
|
|
|
return parsed.Host != ""
|
|
}
|