diff --git a/cmd/diagnosis/main.go b/cmd/diagnosis/main.go index 72cb64b..616ae15 100644 --- a/cmd/diagnosis/main.go +++ b/cmd/diagnosis/main.go @@ -16,10 +16,11 @@ func main() { 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 + if di := diagnosis.New(); di != nil { + di.RemoteAddr = req.RemoteAddr // dynamic update for each request + fmt.Fprintf(w, "%s", di) + return + } } w.WriteHeader(http.StatusNotFound) //404 diff --git a/diagnosis/diagnosis.go b/diagnosis/fetching.go similarity index 66% rename from diagnosis/diagnosis.go rename to diagnosis/fetching.go index e155275..d76830f 100644 --- a/diagnosis/diagnosis.go +++ b/diagnosis/fetching.go @@ -1,7 +1,6 @@ package diagnosis import ( - "encoding/json" "fmt" "net" "os" @@ -9,17 +8,6 @@ import ( "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 @@ -62,12 +50,3 @@ func New() *Info { 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/diagnosis/info.go b/diagnosis/info.go new file mode 100644 index 0000000..f0b2a16 --- /dev/null +++ b/diagnosis/info.go @@ -0,0 +1,26 @@ +package diagnosis + +import ( + "encoding/json" + "fmt" +) + +// 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"` +} + +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) +}