forked from mtusi2026/mtusi2026lab2
Реализация на 4
This commit is contained in:
@@ -6,7 +6,7 @@ import (
|
||||
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/environment"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/types/environment"
|
||||
)
|
||||
|
||||
type Application struct {
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"go.uber.org/multierr"
|
||||
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/environment"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/types/environment"
|
||||
)
|
||||
|
||||
var defaultConfig = &Config{
|
||||
@@ -44,14 +43,6 @@ var defaultConfig = &Config{
|
||||
PublicKeyPath: "certs/private.pem",
|
||||
PrivateKeyPath: "certs/public.pem",
|
||||
},
|
||||
Cookies: &Cookies{
|
||||
Enabled: true,
|
||||
Domain: "",
|
||||
Path: "/",
|
||||
Secure: true,
|
||||
SameSite: http.SameSiteStrictMode,
|
||||
AllowedOrigins: []string{"http:localhost:8080"},
|
||||
},
|
||||
Application: &Application{
|
||||
LogLevel: "info",
|
||||
ShutdownTimeout: 10 * time.Second,
|
||||
@@ -65,7 +56,6 @@ type Config struct {
|
||||
Postgres *Postgres
|
||||
Redis *Redis
|
||||
JWT *JWT
|
||||
Cookies *Cookies
|
||||
Application *Application
|
||||
}
|
||||
|
||||
@@ -93,7 +83,6 @@ func (c *Config) Validate() error {
|
||||
c.Postgres.Validate(),
|
||||
c.JWT.Validate(),
|
||||
c.Redis.Validate(),
|
||||
c.Cookies.Validate(),
|
||||
c.Application.Validate(),
|
||||
)
|
||||
}
|
||||
@@ -104,7 +93,6 @@ func (c *Config) loadConfig(v *viper.Viper) error {
|
||||
c.Server.loadConfig(v),
|
||||
c.JWT.loadConfig(v),
|
||||
c.Redis.loadConfig(v),
|
||||
c.Cookies.loadConfig(v),
|
||||
c.Application.loadConfig(v),
|
||||
)
|
||||
}
|
||||
@@ -115,7 +103,6 @@ func (c *Config) copy() *Config {
|
||||
Postgres: c.Postgres.copy(),
|
||||
Redis: c.Redis.copy(),
|
||||
JWT: c.JWT.copy(),
|
||||
Cookies: c.Cookies.copy(),
|
||||
Application: c.Application.copy(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,33 +18,6 @@ type Server struct {
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user