From 5defafe5f6bf2173db3368573c87d20fc2df32db Mon Sep 17 00:00:00 2001 From: Jay Zhang Date: Mon, 7 Oct 2019 15:13:00 +0800 Subject: [PATCH] feat: first implementation --- cmd/diagnosis/main.go | 30 +++++++++++++++++ diagnosis/diagnosis.go | 73 ++++++++++++++++++++++++++++++++++++++++++ go.mod | 5 +++ go.sum | 2 ++ 4 files changed, 110 insertions(+) create mode 100644 cmd/diagnosis/main.go create mode 100644 diagnosis/diagnosis.go create mode 100644 go.mod create mode 100644 go.sum diff --git a/cmd/diagnosis/main.go b/cmd/diagnosis/main.go new file mode 100644 index 0000000..72cb64b --- /dev/null +++ b/cmd/diagnosis/main.go @@ -0,0 +1,30 @@ +package main + +import ( + "fmt" + "log" + "net/http" + + "github.com/wangyoucao577/go-project-layout/diagnosis" +) + +func main() { + + mux := http.NewServeMux() + + //Sample Request: http://localhost:8000/diagnosis?diagnosis=ping + mux.HandleFunc("/diagnosis", func(w http.ResponseWriter, req *http.Request) { + item := req.URL.Query().Get("diagnosis") + if item == "ping" { + diagnosisInfo := diagnosis.New() + diagnosisInfo.RemoteAddr = req.RemoteAddr // dynamic update for each request + fmt.Fprintf(w, "%s", diagnosisInfo) + return + } + + w.WriteHeader(http.StatusNotFound) //404 + fmt.Fprintf(w, "no diagnosis command %s", req.URL) + }) + + log.Fatal(http.ListenAndServe(":8000", mux)) +} diff --git a/diagnosis/diagnosis.go b/diagnosis/diagnosis.go new file mode 100644 index 0000000..e155275 --- /dev/null +++ b/diagnosis/diagnosis.go @@ -0,0 +1,73 @@ +package diagnosis + +import ( + "encoding/json" + "fmt" + "net" + "os" + + "github.com/matishsiao/goInfo" +) + +// Info includes information for diagnosis. +type Info struct { + //static info + Hostname string `json:"Hostname"` + IPAddresses []string `json:"IP Addresses"` //readable format, like x.x.x.x or ::1 + CPUs int `json:"CPUs"` + + //dynamic info for per request + RemoteAddr string `json:"Remote Endpoint"` +} + +// New fetchs information for diagnosis. +func New() *Info { + var info Info + + //hostname + hostname, err := os.Hostname() + if err != nil { + fmt.Printf("get hostname failed, err %v\n", err) + } else { + fmt.Printf("Hostname: %s\n", hostname) + info.Hostname = hostname + } + + //ip addresses + addrs, err := net.InterfaceAddrs() + if err != nil { + fmt.Printf("lookup network interface addrs failed, err %v\n", err) + } else { + for _, addr := range addrs { + ip, _, err := net.ParseCIDR(addr.String()) + if err != nil { + fmt.Printf("ParseCIDR addrs failed, err %v\n", err) + continue + } + + if ip.IsLoopback() { + fmt.Printf("ignore Loopback ip address-->%s\n", addr.String()) + continue + } + + fmt.Printf("%s ip address-->%s\n", addr.Network(), addr.String()) + info.IPAddresses = append(info.IPAddresses, addr.String()) + } + } + + //from goInfo + gi := goInfo.GetInfo() + gi.VarDump() + info.CPUs = gi.CPUs + + return &info +} + +func (info Info) String() string { + jsonstr, err := json.Marshal(info) + if err != nil { + fmt.Printf("to json failed, err %v\n", err) + return err.Error() + } + return string(jsonstr) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8328705 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/wangyoucao577/go-project-layout + +go 1.13 + +require github.com/matishsiao/goInfo v0.0.0-20170803142006-617e6440957e diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..01bfd9b --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/matishsiao/goInfo v0.0.0-20170803142006-617e6440957e h1:Y+GY+bv5vf1gssphFsGiq6R8qdHxnpDZvYljFnXfhD8= +github.com/matishsiao/goInfo v0.0.0-20170803142006-617e6440957e/go.mod h1:yLZrFIhv+Z20hxHvcZpEyKVQp9HMsOJkXAxx7yDqtvg=