diff --git a/.gitignore b/.gitignore index f1b7204..4099667 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /.idea/* +*.log diff --git a/cmd/config/config.go b/cmd/config/config.go new file mode 100644 index 0000000..fe92899 --- /dev/null +++ b/cmd/config/config.go @@ -0,0 +1,29 @@ +package config + +import ( + "github.com/Unknwon/goconfig" + "log" + "os" +) + +type MysqlConf struct { + User string + Passwd string + Conn string +} + +func (m *MysqlConf) GetMysqlConn() { + var cfg *goconfig.ConfigFile + cpath, _ := os.Getwd() + config, err := goconfig.LoadConfigFile(cpath + "/config/config.ini") //加载配置文件 + if err != nil { + log.Println("get config file error") + os.Exit(-1) + } + cfg = config + + glob, _ := cfg.GetSection("mysql") //读取全部mysql配置 + m.User, _ = glob["username"] + m.Passwd, _ = glob["password"] + m.Conn, _ = glob["url"] +} diff --git a/cmd/mmysql/servicelist.go b/cmd/mmysql/servicelist.go new file mode 100644 index 0000000..ce13e2f --- /dev/null +++ b/cmd/mmysql/servicelist.go @@ -0,0 +1,73 @@ +package mmysql + +import ( + "../config" + "database/sql" + "log" + "strings" + //导入mysql的驱动 + _ "github.com/go-sql-driver/mysql" +) + +type ServiceList struct { + Id int32 `service id` + Name string `service name` + Host string `hostip` + area string `area` + port int `port` + Url string `monitor url` + user string `user` + sendto string `alter message send to` +} + +func GetServiceList(area string) (ss []ServiceList) { + var myconn config.MysqlConf + myconn.GetMysqlConn() + + path := strings.Join([]string{myconn.User, ":", myconn.Passwd, "@tcp(", myconn.Conn, ")/", "services", "?charset=utf8"}, "") + + //打开数据库,前者是驱动名,所以要导入: _ "github.com/go-sql-driver/mysql" + db, _ := sql.Open("mysql", path) + log.Printf("conn db %s", path) + //设置数据库最大连接数 + db.SetConnMaxLifetime(100) + //设置上数据库最大闲置连接数 + db.SetMaxIdleConns(10) + //验证连接 + if err := db.Ping(); err != nil { + log.Print("opon database fail") + return + } + rows, err := db.Query("select id,name,host,url FROM services.services_list where area=?", area) + //获取完毕释放rows,阻止更多的列举 + defer rows.Close() + if err != nil { + log.Println("获取错误:", err) + return + } + //如果有数据记录Next指针就不为true + var v []ServiceList + for rows.Next() { + var id int32 + var name string + var host string + var url string + rows.Scan(&id, &name, &host, &url) + var s ServiceList + s.Id = id + s.Name = name + s.Host = host + s.Url = url + v = append(v, s) + //log.Println(s) + } + //Err返回可能的、在迭代时出现的错误。Err需在显式或隐式调用Close方法后调用。 + err = rows.Err() + if err != nil { + log.Println("other error:", err) + return v + } + + //log.Println("return list with area", v) + return v +} diff --git a/config/config.ini b/config/config.ini new file mode 100644 index 0000000..5823f48 --- /dev/null +++ b/config/config.ini @@ -0,0 +1,6 @@ +[mysql] +username=miles +password=aspect +url=192.168.100.30:3306 +[redis] +address=192.168.100.30:6379 \ No newline at end of file diff --git a/config/config.json b/config/config.json new file mode 100644 index 0000000..904825e --- /dev/null +++ b/config/config.json @@ -0,0 +1,13 @@ +{ + "date": "2019-04-30", + "mysql": { + "url": "192.168.100.30:3306", + "username": "miles", + "password": "aspect" + }, + "redis": { + "host": "192.168.100.30", + "port": 6379, + "passwd": "" + } +} \ No newline at end of file diff --git a/server/server.go b/server/server.go new file mode 100644 index 0000000..39a1fc6 --- /dev/null +++ b/server/server.go @@ -0,0 +1,68 @@ +package main + +import ( + "../cmd/mmysql" + "encoding/json" + "io" + "log" + "os" +) + +var ( + Info *log.Logger + Warning *log.Logger + Error *log.Logger +) + +func init() { + errFile, err := os.OpenFile("errors.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) + if err != nil { + log.Fatalln("打开日志文件失败:", err) + } + + Info = log.New(os.Stdout, "Info:", log.Ldate|log.Ltime|log.Lshortfile) + Warning = log.New(os.Stdout, "Warning:", log.Ldate|log.Ltime|log.Lshortfile) + Error = log.New(io.MultiWriter(os.Stderr, errFile), "Error:", log.Ldate|log.Ltime|log.Lshortfile) + +} + +type ServiceStatus struct { + id int32 `service id` + costtime int32 `monitor cost time second` + status bool `serivces status` + clientip string `monitor run client ip` + client_area string `client area` +} + +func GetServiceStatus(c *ServiceStatus) bool { + Info.Println("get message from client", c) + return true +} +func SaveServiceStatus() bool { + return true +} + +func main() { + Info.Println("run main") + //var m mmysql.ServiceList + vv := mmysql.GetServiceList("bj") + jsons, errs := json.Marshal(vv) + if errs != nil { + Error.Println(errs.Error()) + } + Info.Println(string(jsons)) + + //Warning.Println(string(jsons)) + //for i := 0; i < len(vv); i++ { + // Warning.Println(vv[i]) + //} + //for k, v := range vv { + // Error.Println(k) + // Error.Println(v) + // Error.Println(v.Id) + //} + //Info.Println(vv) + //var myconn config.MysqlConf + //myconn.GetMysqlConn() + //Error.Printf("mmysql user %s,passwd %s,url %s", myconn.User, myconn.Passwd, myconn.Conn) +}