跳至主要内容

[pkg] configor

configor

範例程式碼

// https://github.com/gotify/server/blob/master/config/config.go

type Server struct {
ListenAddr string `default:""`
Port int `default:"80"`
SSL struct {
Enabled *bool `default:"false"`
RedirectToHTTPS *bool `default:"true"`
ListenAddr string `default:""`
Port int `default:"443"`
CertFile string `default:""`
CertKey string `default:""`
LetsEncrypt struct {
Enabled *bool `default:"false"`
AcceptTOS *bool `default:"false"`
Cache string `default:"data/certs"`
Hosts []string
}
}
ResponseHeaders map[string]string
Cors struct {
AllowOrigins []string
AllowMethods []string
AllowHeaders []string
}
}

type Database struct {
PrivateKey []byte
PublicKey []byte
DSN string `default:"sslmode=disable host=localhost port=5432 user=postgres dbname=postgres"`
}

type Configuration struct {
AppName string `default:"jubo-canada-backend"`
Server
Database
}

func main() {
config := Get()

fmt.Println(config.AppName)
}

func Get() *Configuration {
config := &Configuration{}
if err := configor.New(&configor.Config{Debug: true, Environment: "development"}).Load(config, "config.yaml"); err != nil {
log.Fatalln("load config fail", err)
}

return config
}