Начало реализации сервиса.

This commit is contained in:
Grbisba
2026-10-30 12:25:56 +03:00
parent 65003b6119
commit 20f2f00a60
52 changed files with 4351 additions and 1 deletions
+65
View File
@@ -0,0 +1,65 @@
package config
import (
"errors"
"net/http"
"strings"
"github.com/spf13/viper"
)
type Cookies struct {
Enabled bool
Domain string
Path string
Secure bool
SameSite http.SameSite
AllowedOrigins []string
}
func (c *Cookies) Validate() error {
switch {
case c.Domain == "":
return wrapValidationError("cookies", errors.New("domain cannot be empty"))
case c.Path == "":
return wrapValidationError("cookies", errors.New("path cannot be empty"))
case c.SameSite == http.SameSiteDefaultMode:
return wrapValidationError("cookies", errors.New("same_site_set_to_none_mode cannot be empty"))
//case c.Enabled && !c.Secure:
// return wrapValidationError("cookies", errors.New("if cookies_enabled equals true then cookies_secure must be true"))
case c.AllowedOrigins == nil || len(c.AllowedOrigins) == 0:
return wrapValidationError("cookies", errors.New("allowed_origins cannot be empty"))
default:
return nil
}
}
func (c *Cookies) loadConfig(v *viper.Viper) error {
v.SetEnvPrefix("COOKIES")
c.Enabled = v.GetBool("ENABLED")
c.Domain = v.GetString("DOMAIN")
c.Path = v.GetString("PATH")
c.Secure = v.GetBool("SECURE")
c.AllowedOrigins = v.GetStringSlice("ALLOWED_ORIGINS")
sameSite := v.GetString("SAME_SITE")
switch strings.ToLower(strings.TrimSpace(sameSite)) {
case "strict":
c.SameSite = http.SameSiteStrictMode
case "none":
c.SameSite = http.SameSiteNoneMode
default:
c.SameSite = http.SameSiteLaxMode
}
return nil
}
func (c *Cookies) copy() *Cookies {
return &Cookies{
Enabled: c.Enabled,
Domain: c.Domain,
Path: c.Path,
Secure: c.Secure,
SameSite: c.SameSite,
AllowedOrigins: c.AllowedOrigins,
}
}