Skip to content

Commit

Permalink
feat: first implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyoucao577 committed Oct 7, 2019
1 parent 722a00b commit 5defafe
Showing 4 changed files with 110 additions and 0 deletions.
30 changes: 30 additions & 0 deletions cmd/diagnosis/main.go
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))
}
73 changes: 73 additions & 0 deletions diagnosis/diagnosis.go
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)
}
5 changes: 5 additions & 0 deletions go.mod
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
2 changes: 2 additions & 0 deletions go.sum
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=

0 comments on commit 5defafe

Please sign in to comment.