1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- package setting
- import (
- "errors"
- "log"
- "os"
- "os/exec"
- "path/filepath"
- "runtime"
- "strings"
- "github.com/go-ini/ini"
- )
- type Database struct {
- Type string
- User string
- Password string
- Host string
- Name string
- ShowXormlog bool
- ShowGetSqllog bool
- CacheApiSql bool
- TablePrefix string
- FarmCode string
- }
- var DatabaseSetting = &Database{}
- var cfg *ini.File
- var CurrentPath string
- func GetCurrentPath() (string, error) {
- file, err := exec.LookPath(os.Args[0])
- if err != nil {
- return "", err
- }
- path, err := filepath.Abs(file)
- if err != nil {
- return "", err
- }
- //fmt.Println("path111:", path)
- if runtime.GOOS == "windows" {
- path = strings.Replace(path, "\\", "/", -1)
- }
- //fmt.Println("path222:", path)
- i := strings.LastIndex(path, "/")
- if i < 0 {
- return "", errors.New(`Can't find "/" or "\".`)
- }
- //fmt.Println("path333:", path)
- return string(path[0 : i+1]), nil
- }
- func Setup(dir string) {
- var err error
- if strings.Trim(dir, " ") == "" {
- CurrentPath, err = GetCurrentPath()
- } else {
- CurrentPath = dir
- }
- cfg, err = ini.Load(CurrentPath + "app.ini")
- if err != nil {
- log.Fatalf("setting.Setup, fail to parse 'conf/app.ini': %v", err)
- }
- mapTo("database", DatabaseSetting)
- DatabaseSetting.Host = DatabaseSetting.Host + ":3326"
- DatabaseSetting.Type = "mysql"
- DatabaseSetting.User = "root"
- DatabaseSetting.Password = "root"
- DatabaseSetting.Name = "tmrwatch2"
- }
- func mapTo(section string, v interface{}) {
- err := cfg.Section(section).MapTo(v)
- if err != nil {
- log.Fatalf("Cfg.MapTo RedisSetting err: %v", err)
- }
- }
|