52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
package config
|
|
|
|
import "fmt"
|
|
|
|
const (
|
|
// MySQLDriver type
|
|
MySQLDriver = "mysql"
|
|
// SqliteDriver type
|
|
SqliteDriver = "sqlite"
|
|
)
|
|
|
|
// create database `db` default character set utf8mb4 collate utf8mb4_unicode_ci;
|
|
|
|
// DatabaseConf xxx
|
|
type DatabaseConf struct {
|
|
Driver string `yaml:"driver"` // "mysql or sqlite"
|
|
Host string `yaml:"host"`
|
|
Port int `yaml:"port"`
|
|
Username string `yaml:"username"`
|
|
Password string `yaml:"password"`
|
|
DB string `yaml:"db"`
|
|
MaxOpenConns int `yaml:"max_open_conns"`
|
|
MaxIdleConns int `yaml:"max_idle_conns"`
|
|
ConnMaxLifeSeconds int `yaml:"conn_max_life_seconds"`
|
|
Debug bool `yaml:"debug"`
|
|
}
|
|
|
|
// NewDatabaseConf with default value
|
|
func NewDatabaseConf(db string) *DatabaseConf {
|
|
return &DatabaseConf{
|
|
Driver: MySQLDriver,
|
|
Host: "127.0.0.1",
|
|
Port: 3306,
|
|
Username: "root",
|
|
Password: "",
|
|
DB: db,
|
|
Debug: false,
|
|
}
|
|
}
|
|
|
|
// DSN ...
|
|
func (c *DatabaseConf) DSN() string {
|
|
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
|
|
c.Username,
|
|
c.Password,
|
|
c.Host,
|
|
c.Port,
|
|
c.DB,
|
|
)
|
|
return dsn
|
|
}
|