add cfg
This commit is contained in:
@@ -11,23 +11,20 @@ import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
type DataConfig struct {
|
||||
UserName string
|
||||
Password string
|
||||
HostName string
|
||||
DbName string
|
||||
}
|
||||
|
||||
func (d *DataConfig) Get() string {
|
||||
return fmt.Sprintf("%s:%s@tcp(%s)/%s", d.UserName, d.Password, d.HostName, d.DbName)
|
||||
type Config struct {
|
||||
Token string `yaml:"token"`
|
||||
UserName string `yaml:"user_name"`
|
||||
Password string `yaml:"password"`
|
||||
HostName string `yaml:"host_name"`
|
||||
DbName string `yaml:"db_name"`
|
||||
}
|
||||
|
||||
type DataController struct{ DB *sql.DB }
|
||||
|
||||
func NewDB() *DataController {
|
||||
config := DataConfig{UserName: "root", Password: "12641264", HostName: "217.12.40.237", DbName: "test"}
|
||||
func NewDB(config Config) *DataController {
|
||||
dbaddr := fmt.Sprintf("%s:%s@tcp(%s)/%s", config.UserName, config.Password, config.HostName, config.DbName)
|
||||
|
||||
db, err := sql.Open("mysql", config.Get())
|
||||
db, err := sql.Open("mysql", dbaddr)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user