-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
144 lines (126 loc) · 3.54 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"database/sql"
"encoding/json"
"fmt"
_ "github.com/go-sql-driver/mysql"
"log"
"net/http"
"strings"
)
func getRedisConf(w http.ResponseWriter, r *http.Request) {
r.ParseForm() //解析参数,默认是不会解析的
fmt.Println("path", r.URL.Path)
for k, v := range r.Form {
fmt.Println("key:", k)
fmt.Println("val:", strings.Join(v, ""))
}
fmt.Fprintf(w, "Get Redis Configuer Info.\n") //这个写入到w的是输出到客户端的
}
type mysqlContent struct {
dbrole string
ip string
port int
username string
passwd string
readweight int
writeweight int
dbstatus int
}
type mysqlResponse struct {
status string
appName string
groupName string
dbName string
masterNode mysqlContent
slaveNode []mysqlContent
}
var db *sql.DB
func init() {
db, _ = sql.Open("mysql", "yxb:yxb@tcp(127.0.0.1:3306)/config_center?timeout=4s&charset=utf8")
db.SetMaxOpenConns(16)
db.SetMaxIdleConns(4)
db.Ping()
}
func (myRes mysqlResponse) getMysqlResponse(appName string, groupName string) interface{} {
myRes.status = "Error"
myRes.appName = appName
myRes.groupName = groupName
sql := `SELECT
appname,groupname,dbname,dbrole,ip,port,username, passwd,
read_weight, write_weight, dbstatus
FROM config_info
WHERE appname='`
sql += appName + "' AND groupname='"
sql += groupName + "'"
rows, err := db.Query(sql)
if err != nil {
log.Println(err)
myRes.status = "Error"
return myRes
}
var appname, groupname, dbname, dbrole, ip, username, passwd string
var port, read_weight, write_weight, dbstatus int
for rows.Next() {
err := rows.Scan(&appname, &groupname, &dbname, &dbrole, &ip, &port, &username, &passwd, &read_weight, &write_weight, &dbstatus)
if err != nil {
log.Fatal(err)
}
// dbname
if len(dbname) > 0 {
myRes.dbName = dbname
} else {
myRes.status = "Error"
log.Println("dbname is null")
return myRes
}
if dbrole == "Master" && write_weight > 0 {
myRes.masterNode.ip = ip
myRes.masterNode.port = port
myRes.masterNode.username = username
myRes.masterNode.passwd = passwd
myRes.masterNode.readweight = read_weight
myRes.masterNode.writeweight = write_weight
myRes.masterNode.dbstatus = dbstatus
myRes.status = "OK"
} else if dbrole == "Master" && write_weight == 0 {
myRes.status = "Error"
log.Println("AppName:", appname, "GroupName:", groupname, "The Master ", ip, port, dbname, " write_weight is 0. Please check!")
return myRes
} else {
//slave node
mycent := new(mysqlContent)
mycent.ip = ip
mycent.port = port
mycent.username = username
mycent.passwd = passwd
mycent.readweight = read_weight
mycent.writeweight = write_weight
mycent.dbstatus = dbstatus
myRes.slaveNode = append(myRes.slaveNode, *mycent)
myRes.status = "OK"
}
}
//body, err := json.Marshal(myRes)
return myRes
}
func getMysqlConf(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
var appName, groupName string
appName = strings.Join(r.Form["appName"], "")
groupName = strings.Join(r.Form["groupName"], "")
myResponse := new(mysqlResponse)
rs := myResponse.getMysqlResponse(appName, groupName)
body, _ := json.Marshal(rs)
log.Println(string(body))
respose := "xxx"
fmt.Fprintf(w, respose)
}
func main() {
http.HandleFunc("/getMysqlConf", getMysqlConf) //设置访问的路由
http.HandleFunc("/getRedisConf", getRedisConf) //设置访问的路由
err := http.ListenAndServe(":9090", nil) //设置监听的端口
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}