-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
722a00b
commit 5defafe
Showing
4 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/wangyoucao577/go-project-layout | ||
|
||
go 1.13 | ||
|
||
require github.com/matishsiao/goInfo v0.0.0-20170803142006-617e6440957e |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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= |