// // db.go // Copyright (C) 2022 tiglog // // Distributed under terms of the MIT license. // package gconfig import "fmt" type DbConfig struct { Type string `yaml:"type"` Dsn string `yaml:"dsn"` // Host string `yaml:"host"` // Username string `yaml:"user"` // Password string `yaml:"pass"` // Port int `yaml:"port"` // Name string `yaml:"name"` MaxIdle int `yaml:"max_idle"` MaxOpen int `yaml:"max_open"` } type MongoConfig struct { Host string `yaml:"host"` Port int `yaml:"port"` Username string `yaml:"user"` Password string `yaml:"pass"` Name string `yaml:"name"` PoolSize int `yaml:"pool_size"` } // func (c *DbConfig) GetUri() string { // switch c.Type { // case "postgres": // return fmt.Sprintf("%s://host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", c.Type, c.Host, c.Port, c.Username, c.Password, c.Name) // case "mysql": // return fmt.Sprintf("%s://%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=true&loc=Local", c.Type, c.Username, c.Password, c.Host, c.Port, c.Name) // } // return "" // } func (c *MongoConfig) GetUri() string { if c.Host == "" { return "" } if c.Username == "" { return fmt.Sprintf("mongodb://%s:%d/%s", c.Host, c.Port, c.Name) } else { return fmt.Sprintf("mongodb://%s:%s@%s:%d/%s", c.Username, c.Password, c.Host, c.Port, c.Name) } } type RedisConfig struct { Addr string `yaml:"addr"` Username string `yaml:"user"` Password string `yaml:"pass"` Database int `yaml:"db"` }