forked from mtusi2026/mtusi2026lab2
Начало реализации сервиса.
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
Host string
|
||||
Port int
|
||||
ReadTimeout time.Duration
|
||||
WriteTimeout time.Duration
|
||||
IdleTimeout time.Duration
|
||||
ShutdownTimeout time.Duration
|
||||
Enabled bool
|
||||
}
|
||||
|
||||
// LoadServer reads settings from environment variables and applies lab1 defaults.
|
||||
func LoadServer() (*Server, error) {
|
||||
v := viper.New()
|
||||
v.SetEnvPrefix("APP")
|
||||
v.AutomaticEnv()
|
||||
v.SetDefault("HOST", "0.0.0.0")
|
||||
v.SetDefault("PORT", 8080)
|
||||
v.SetDefault("READ_TIMEOUT", "5s")
|
||||
v.SetDefault("WRITE_TIMEOUT", "10s")
|
||||
v.SetDefault("IDLE_TIMEOUT", "60s")
|
||||
v.SetDefault("SHUTDOWN_TIMEOUT", "10s")
|
||||
v.SetDefault("ENABLED", true)
|
||||
|
||||
if err := v.BindEnv("HOST", "APP_HOST", "SERVER_HOST"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := v.BindEnv("PORT", "APP_PORT", "SERVER_PORT"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cfg := new(Server)
|
||||
if err := cfg.loadConfig(v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func (c *Server) Validate() error {
|
||||
if !c.Enabled {
|
||||
return nil
|
||||
}
|
||||
switch {
|
||||
case c.Host == "":
|
||||
return wrapValidationError("server", errors.New("host cannot be empty"))
|
||||
case c.Port == 0:
|
||||
return wrapValidationError("server", errors.New("port cannot be empty"))
|
||||
case c.ReadTimeout == 0:
|
||||
return wrapValidationError("server", errors.New("read_timeout cannot be empty"))
|
||||
case c.WriteTimeout == 0:
|
||||
return wrapValidationError("server", errors.New("write_timeout cannot be empty"))
|
||||
case c.IdleTimeout == 0:
|
||||
return wrapValidationError("server", errors.New("idle_timeout cannot be empty"))
|
||||
case c.ShutdownTimeout == 0:
|
||||
return wrapValidationError("server", errors.New("shutdown_timeout name cannot be empty"))
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Server) loadConfig(v *viper.Viper) error {
|
||||
v.SetEnvPrefix("APP")
|
||||
c.Host = v.GetString("HOST")
|
||||
c.Port = v.GetInt("PORT")
|
||||
c.ReadTimeout = v.GetDuration("READ_TIMEOUT")
|
||||
c.WriteTimeout = v.GetDuration("WRITE_TIMEOUT")
|
||||
c.IdleTimeout = v.GetDuration("IDLE_TIMEOUT")
|
||||
c.ShutdownTimeout = v.GetDuration("SHUTDOWN_TIMEOUT")
|
||||
c.Enabled = v.GetBool("ENABLED")
|
||||
return c.Validate()
|
||||
}
|
||||
|
||||
func (c *Server) BindAddress() string {
|
||||
return fmt.Sprintf("%s:%d", c.Host, c.Port)
|
||||
}
|
||||
|
||||
func (c *Server) copy() *Server {
|
||||
return &Server{
|
||||
Host: c.Host,
|
||||
Port: c.Port,
|
||||
ReadTimeout: c.ReadTimeout,
|
||||
WriteTimeout: c.WriteTimeout,
|
||||
IdleTimeout: c.IdleTimeout,
|
||||
ShutdownTimeout: c.ShutdownTimeout,
|
||||
Enabled: c.Enabled,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user