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

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
+64
View File
@@ -0,0 +1,64 @@
package config
import (
"errors"
"fmt"
"net"
"github.com/redis/go-redis/v9"
"github.com/spf13/viper"
)
type Redis struct {
Host string
Port int
Username string
Password string
DB int
}
func (c *Redis) Validate() error {
switch {
case c.Host == "":
return wrapValidationError("redis", errors.New("host cannot be empty"))
case c.Port == 0:
return wrapValidationError("redis", errors.New("port cannot be empty"))
default:
return nil
}
}
func (c *Redis) BuildRedisOptions() *redis.Options {
if c == nil {
return nil
}
return &redis.Options{
Addr: net.JoinHostPort(c.Host, fmt.Sprintf("%d", c.Port)),
Username: c.Username,
Password: c.Password,
DB: c.DB,
}
}
func (c *Redis) loadConfig(v *viper.Viper) error {
v.SetEnvPrefix("REDIS")
c.Host = v.GetString("HOST")
c.Port = v.GetInt("PORT")
c.DB = v.GetInt("DB")
c.Password = v.GetString("PASSWORD")
c.Username = v.GetString("USERNAME")
return nil
}
func (c *Redis) copy() *Redis {
if c == nil {
return nil
}
return &Redis{
Host: c.Host,
Port: c.Port,
Username: c.Username,
Password: c.Password,
DB: c.DB,
}
}