Начало реализации сервиса.
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
const (
|
||||
uri = "postgres://%s:%s@%s:%d/%s?sslmode=disable"
|
||||
)
|
||||
|
||||
type Postgres struct {
|
||||
Host string
|
||||
Port int
|
||||
User string
|
||||
Password string
|
||||
Database string
|
||||
VersionTable string
|
||||
RunMigrations bool
|
||||
MaxConnections int
|
||||
}
|
||||
|
||||
func (c *Postgres) Validate() error {
|
||||
switch {
|
||||
case c.Host == "":
|
||||
return wrapValidationError("postgres", errors.New("host cannot be empty"))
|
||||
case c.Port == 0:
|
||||
return wrapValidationError("postgres", errors.New("port cannot be empty"))
|
||||
case c.User == "":
|
||||
return wrapValidationError("postgres", errors.New("user cannot be empty"))
|
||||
case c.Password == "":
|
||||
return wrapValidationError("postgres", errors.New("password cannot be empty"))
|
||||
case c.Database == "":
|
||||
return wrapValidationError("postgres", errors.New("database cannot be empty"))
|
||||
case c.VersionTable == "":
|
||||
return wrapValidationError("postgres", errors.New("version_table name cannot be empty"))
|
||||
case c.MaxConnections == 0:
|
||||
return wrapValidationError("postgres", errors.New("max_connections cannot be empty"))
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Postgres) GetVersionTableName() string {
|
||||
if c == nil {
|
||||
return ""
|
||||
}
|
||||
return c.VersionTable
|
||||
}
|
||||
|
||||
func (c *Postgres) GetURI() string {
|
||||
if c == nil {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf(uri, c.User, c.Password, c.Host, c.Port, c.Database)
|
||||
}
|
||||
|
||||
func (c *Postgres) GetRunMigrations() bool {
|
||||
if c == nil {
|
||||
return false
|
||||
}
|
||||
return c.RunMigrations
|
||||
}
|
||||
|
||||
func (c *Postgres) loadConfig(v *viper.Viper) error {
|
||||
v.SetEnvPrefix("POSTGRES")
|
||||
c.Host = v.GetString("HOST")
|
||||
c.Port = v.GetInt("PORT")
|
||||
c.User = v.GetString("USER")
|
||||
c.Password = v.GetString("PASSWORD")
|
||||
c.Database = v.GetString("DATABASE")
|
||||
c.VersionTable = v.GetString("VERSION_TABLE")
|
||||
c.RunMigrations = v.GetBool("RUN_MIGRATIONS")
|
||||
c.MaxConnections = v.GetInt("MAX_CONNECTIONS")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Postgres) copy() *Postgres {
|
||||
if c == nil {
|
||||
return nil
|
||||
}
|
||||
return &Postgres{
|
||||
Host: c.Host,
|
||||
Port: c.Port,
|
||||
User: c.User,
|
||||
Password: c.Password,
|
||||
Database: c.Database,
|
||||
MaxConnections: c.MaxConnections,
|
||||
RunMigrations: c.RunMigrations,
|
||||
VersionTable: c.VersionTable,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user