add mysql config moudles

This commit is contained in:
pengtao 2020-10-12 18:53:27 +08:00
parent d59d2f9aac
commit 1f68bd94f3
6 changed files with 190 additions and 0 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
/.idea/*
*.log

29
cmd/config/config.go Normal file
View File

@ -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"]
}

73
cmd/mmysql/servicelist.go Normal file
View File

@ -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
}

6
config/config.ini Normal file
View File

@ -0,0 +1,6 @@
[mysql]
username=miles
password=aspect
url=192.168.100.30:3306
[redis]
address=192.168.100.30:6379

13
config/config.json Normal file
View File

@ -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": ""
}
}

68
server/server.go Normal file
View File

@ -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)
}