Files
atomctl/templates/project/providers/postgres/config.go.tpl
2025-01-22 14:50:54 +08:00

80 lines
1.6 KiB
Smarty
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package postgres
import (
"fmt"
"{{.ModuleName}}/pkg/atom/container"
"{{.ModuleName}}/pkg/atom/opt"
)
const DefaultPrefix = "Database"
func DefaultProvider() container.ProviderContainer {
return container.ProviderContainer{
Provider: Provide,
Options: []opt.Option{
opt.Prefix(DefaultPrefix),
},
}
}
type Config struct {
Username string
Password string
Database string
Schema string
Host string
Port uint
SslMode string
TimeZone string
Prefix string //
Singular bool // true表示开启
MaxIdleConns int //
MaxOpenConns int //
}
func (m *Config) checkDefault() {
if m.MaxIdleConns == 0 {
m.MaxIdleConns = 10
}
if m.MaxOpenConns == 0 {
m.MaxOpenConns = 100
}
if m.Username == "" {
m.Username = "postgres"
}
if m.SslMode == "" {
m.SslMode = "disable"
}
if m.TimeZone == "" {
m.TimeZone = "Asia/Shanghai"
}
if m.Port == 0 {
m.Port = 5432
}
if m.Schema == "" {
m.Schema = "public"
}
}
func (m *Config) EmptyDsn() string {
dsnTpl := "host=%s user=%s password=%s port=%d dbname=%s sslmode=%s TimeZone=%s"
m.checkDefault()
return fmt.Sprintf(dsnTpl, m.Host, m.Username, m.Password, m.Port, m.Database, m.SslMode, m.TimeZone)
}
// DSN connection dsn
func (m *Config) DSN() string {
dsnTpl := "host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s"
m.checkDefault()
return fmt.Sprintf(dsnTpl, m.Host, m.Username, m.Password, m.Database, m.Port, m.SslMode, m.TimeZone)
}