config.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package config
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "time"
  7. )
  8. type Config struct {
  9. Server ServerConfig `json:"server"`
  10. Gateway GatewayConfig `json:"gateway"`
  11. Auth AuthConfig `json:"auth"`
  12. }
  13. type ServerConfig struct {
  14. HTTPListen string `json:"httpListen"`
  15. ReadTimeoutSeconds int `json:"readTimeoutSeconds"`
  16. WriteTimeoutSeconds int `json:"writeTimeoutSeconds"`
  17. IdleTimeoutSeconds int `json:"idleTimeoutSeconds"`
  18. ShutdownTimeoutSeconds int `json:"shutdownTimeoutSeconds"`
  19. }
  20. type GatewayConfig struct {
  21. MaxPayloadBytes int `json:"maxPayloadBytes"`
  22. WriteWaitSeconds int `json:"writeWaitSeconds"`
  23. PongWaitSeconds int `json:"pongWaitSeconds"`
  24. PingIntervalSeconds int `json:"pingIntervalSeconds"`
  25. MaxLatestStateEntries int `json:"maxLatestStateEntries"`
  26. }
  27. type AuthConfig struct {
  28. ProducerTokens []string `json:"producerTokens"`
  29. ConsumerTokens []string `json:"consumerTokens"`
  30. ControllerTokens []string `json:"controllerTokens"`
  31. AllowAnonymousConsumers bool `json:"allowAnonymousConsumers"`
  32. }
  33. func Load(path string) (Config, error) {
  34. data, err := os.ReadFile(path)
  35. if err != nil {
  36. return Config{}, fmt.Errorf("read config: %w", err)
  37. }
  38. var cfg Config
  39. if err := json.Unmarshal(data, &cfg); err != nil {
  40. return Config{}, fmt.Errorf("parse config: %w", err)
  41. }
  42. cfg.applyDefaults()
  43. if err := cfg.validate(); err != nil {
  44. return Config{}, err
  45. }
  46. return cfg, nil
  47. }
  48. func (c *Config) applyDefaults() {
  49. if c.Server.HTTPListen == "" {
  50. c.Server.HTTPListen = ":8080"
  51. }
  52. if c.Server.ReadTimeoutSeconds <= 0 {
  53. c.Server.ReadTimeoutSeconds = 15
  54. }
  55. if c.Server.WriteTimeoutSeconds <= 0 {
  56. c.Server.WriteTimeoutSeconds = 15
  57. }
  58. if c.Server.IdleTimeoutSeconds <= 0 {
  59. c.Server.IdleTimeoutSeconds = 60
  60. }
  61. if c.Server.ShutdownTimeoutSeconds <= 0 {
  62. c.Server.ShutdownTimeoutSeconds = 10
  63. }
  64. if c.Gateway.MaxPayloadBytes <= 0 {
  65. c.Gateway.MaxPayloadBytes = 64 * 1024
  66. }
  67. if c.Gateway.WriteWaitSeconds <= 0 {
  68. c.Gateway.WriteWaitSeconds = 10
  69. }
  70. if c.Gateway.PongWaitSeconds <= 0 {
  71. c.Gateway.PongWaitSeconds = 60
  72. }
  73. if c.Gateway.PingIntervalSeconds <= 0 {
  74. c.Gateway.PingIntervalSeconds = 25
  75. }
  76. if c.Gateway.MaxLatestStateEntries <= 0 {
  77. c.Gateway.MaxLatestStateEntries = 10000
  78. }
  79. }
  80. func (c Config) validate() error {
  81. if c.Gateway.PingInterval() >= c.Gateway.PongWait() {
  82. return fmt.Errorf("gateway.pingIntervalSeconds must be smaller than gateway.pongWaitSeconds")
  83. }
  84. return nil
  85. }
  86. func (c ServerConfig) ReadTimeout() time.Duration {
  87. return time.Duration(c.ReadTimeoutSeconds) * time.Second
  88. }
  89. func (c ServerConfig) WriteTimeout() time.Duration {
  90. return time.Duration(c.WriteTimeoutSeconds) * time.Second
  91. }
  92. func (c ServerConfig) IdleTimeout() time.Duration {
  93. return time.Duration(c.IdleTimeoutSeconds) * time.Second
  94. }
  95. func (c ServerConfig) ShutdownTimeout() time.Duration {
  96. return time.Duration(c.ShutdownTimeoutSeconds) * time.Second
  97. }
  98. func (c GatewayConfig) WriteWait() time.Duration {
  99. return time.Duration(c.WriteWaitSeconds) * time.Second
  100. }
  101. func (c GatewayConfig) PongWait() time.Duration {
  102. return time.Duration(c.PongWaitSeconds) * time.Second
  103. }
  104. func (c GatewayConfig) PingInterval() time.Duration {
  105. return time.Duration(c.PingIntervalSeconds) * time.Second
  106. }