2023-06-15 21:22:51 +08:00
|
|
|
//
|
|
|
|
// db.go
|
|
|
|
// Copyright (C) 2022 tiglog <me@tiglog.com>
|
|
|
|
//
|
|
|
|
// Distributed under terms of the MIT license.
|
|
|
|
//
|
|
|
|
|
|
|
|
package gconfig
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
type DbConfig struct {
|
2023-08-17 19:05:46 +08:00
|
|
|
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"`
|
2023-06-15 21:22:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2023-08-17 19:05:46 +08:00
|
|
|
// 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 ""
|
|
|
|
// }
|
2023-06-15 21:22:51 +08:00
|
|
|
|
|
|
|
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"`
|
|
|
|
}
|