client func

This commit is contained in:
pengtao 2020-10-16 19:08:50 +08:00
parent 7b8585fa0e
commit cb510e0650
6 changed files with 233 additions and 15 deletions

138
client/client.go Normal file
View File

@ -0,0 +1,138 @@
package main
import (
"../cmd/httpclient"
"encoding/json"
"fmt"
"github.com/Unknwon/goconfig"
"github.com/fatih/structs"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"time"
)
func init() {
//
}
func getLocalIp() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
for _, address := range addrs {
// 检查ip地址判断是否回环地址
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
//fmt.Println(ipnet.IP.String())
return ipnet.IP.String()
}
}
}
return ""
}
func checkUrl(list *httpclient.List) *httpclient.Status {
var s httpclient.Status
s.Area = list.Area
s.Id = list.Id
s.Ip = getLocalIp()
t1 := time.Now()
_, err := http.Get(list.Url)
if err != nil {
s.Status = false
fmt.Println(list.Url, err)
} else {
s.Status = true
}
//defer resp.Body.Close()
elapsed := time.Since(t1)
elapsedNan := elapsed.String()
s.Cost = elapsedNan
//s.Cost = strconv.FormatInt(int64(elapsedNan),10)
return &s
}
func main() {
var cfg *goconfig.ConfigFile
cpath, _ := os.Getwd()
config, err := goconfig.LoadConfigFile(cpath + "/config/client.ini") //加载配置文件
if err != nil {
log.Println("get config file error")
os.Exit(-1)
}
cfg = config
client, _ := cfg.GetSection("client")
u, _ := client["get_list_url"]
a, _ := client["area"]
//get list via get
resp, err := http.Get(u + a)
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
}
var dat []httpclient.List
err = json.Unmarshal(body, &dat)
var vv []*httpclient.Status
if err != nil {
fmt.Println("error", err)
} else {
for _, v := range dat {
ss := checkUrl(&v)
vv = append(vv, ss)
}
}
if vv != nil {
fmt.Printf("%+v", vv)
//postUrl := client["post_list_url"]
data := structs.Map(vv)
fmt.Println(data)
//bb := url.Values{data}
//
//body := strings.NewReader(bb.Encode())
//clt := http.Client{}
//resp, err := clt.Post(postUrl, "application/x-www-form-urlencoded", body)
//if err != nil {
// fmt.Println(err)
//} else {
// fmt.Println(resp)
//}
//b, err := json.Marshal(vv)
//if err != nil {
//log.Println("json format error:", err)
//return
//}
//
// body := bytes.NewBuffer(b)
// resp, err := http.Post(postUrl, contentType, body)
// if err != nil {
// log.Println("Post failed:", err)
// return
//}
//
// defer resp.Body.Close()
//
// content, err := ioutil.ReadAll(resp.Body)
// if err != nil {
// log.Println("Read failed:", err)
// return
//}
//
// log.Println("content:", string(content))
//}
}
}

31
cmd/config/clientCfg.go Normal file
View File

@ -0,0 +1,31 @@
package config
import (
"fmt"
"github.com/Unknwon/goconfig"
"log"
"os"
)
type ClientCfg struct {
Area string
GetListUrl string
PostStatusUrl string
}
func (c *ClientCfg) GetCfg() {
var cfg *goconfig.ConfigFile
cpath, _ := os.Getwd()
config, err := goconfig.LoadConfigFile(cpath + "/config/client.ini") //加载配置文件
if err != nil {
log.Println("get config file error")
os.Exit(-1)
}
cfg = config
client, _ := cfg.GetSection("client")
cc, _ := client["get_list_url"]
fmt.Print(cc)
c.PostStatusUrl, _ = client["post_list_url"]
c.Area, _ = client["area"]
}

20
cmd/httpclient/data.go Normal file
View File

@ -0,0 +1,20 @@
package httpclient
type List struct {
Id int32 `json:"id"`
Name string `json:"name"`
Host string `json:"host"`
Area string `json:"area"`
Port int32 `json:"port"`
Url string `json:"url"`
User string `json:"user"`
Sendto string `json:"sendto"`
}
type Status struct {
Id int32 `json:"id"`
Cost string `json:"cost"`
Status bool `json:"status"`
Ip string `json:"ip"`
Area string `json:"area"`
}

32
cmd/mmysql/data.go Normal file
View File

@ -0,0 +1,32 @@
package mmysql
import "fmt"
type Ilist interface {
GetList(area string) ([]*List, error)
}
type List struct {
Id int32 `json:"id"`
Name string `json:"name"`
Host string `json:"host"`
Area string `json:"area"`
Port int32 `json:"port"`
Url string `json:"url"`
User string `json:"user"`
Sendto string `json:"sendto"`
}
func NewList() *List {
var list *List
return list
}
func (s *List) GetList(area string) ([]*List, error) {
var vv []*List
vv = append(vv, NewList())
return vv, nil
}
func test_main() {
fmt.Print("test run!")
}

4
config/client.ini Normal file
View File

@ -0,0 +1,4 @@
[client]
get_list_url=http://127.0.0.1:8000/servicelist?area=
post_list_url=http://127.0.0.1:8000/servicestatus
area=bj

View File

@ -18,14 +18,6 @@ var format = logging.MustStringFormatter(
)
func init() {
//errFile, err := os.OpenFile("errors.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
//if err != nil {
// panic(err)
//}
//defer errFile.Close()
//log.SetFlags(log.Ldate|log.Lshortfile)
//log.SetOutput(io.MultiWriter(os.Stdout, errFile))
////log := log.New(io.MultiWriter(os.Stderr, errFile), "前缀", log.Ldate|log.Ltime|log.Lshortfile)
logFile, err := os.OpenFile("errors.log", os.O_WRONLY, 0666)
if err != nil {
@ -48,7 +40,7 @@ func main() {
router := gin.Default()
router.GET("/healcheck", func(c *gin.Context) {
// 方法二:使用结构体
// 使用结构体
var msg struct {
Name string `json:"user"`
Message string
@ -65,12 +57,13 @@ func main() {
if err != nil {
log.Error(err)
}
jsons, errs := json.Marshal(vv)
log.Info(string(jsons))
if errs != nil {
log.Error(errs.Error())
}
c.JSON(http.StatusOK, string(jsons))
//jsons, errs := json.Marshal(vv)
//log.Info(string(jsons))
//if errs != nil {
// log.Error(errs.Error())
//}
//body := bytes.NewBuffer(jsons)
c.JSON(http.StatusOK, vv)
})