39 lines
681 B
Go
39 lines
681 B
Go
package configs
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
)
|
|
|
|
// 數據庫配置
|
|
type DBConfig struct {
|
|
Type string `json:"type"`
|
|
Host string `json:"host"`
|
|
Port int `json:"port"`
|
|
UserName string `json:"username"`
|
|
Password string `json:"password"`
|
|
Database string `json:"database"`
|
|
}
|
|
|
|
var conf DBConfig
|
|
|
|
func init() {
|
|
data, err := ioutil.ReadFile("./data/config.json")
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
err = json.Unmarshal(data, &conf)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
}
|
|
|
|
func GetMySQL() (*sql.DB, error) {
|
|
return sql.Open(conf.Type, conf.UserName+":"+conf.Password+"@/"+conf.Database)
|
|
}
|