Skip to content

Commit

Permalink
feat: use multiple go files
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyoucao577 committed Oct 7, 2019
1 parent d6b86aa commit 2aa3157
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
9 changes: 5 additions & 4 deletions cmd/diagnosis/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 0 additions & 21 deletions diagnosis/diagnosis.go → diagnosis/fetching.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
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
Expand Down Expand Up @@ -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)
}
26 changes: 26 additions & 0 deletions diagnosis/info.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 2aa3157

Please sign in to comment.