Add remoteAddr information (#20)

This commit is contained in:
Michael
2019-09-03 11:40:33 +02:00
committed by Ludovic Fernandez
parent 48ed709a3a
commit 5e438f6f9e

11
app.go
View File

@ -63,7 +63,7 @@ func main() {
func benchHandler(w http.ResponseWriter, _ *http.Request) { func benchHandler(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Connection", "keep-alive") w.Header().Set("Connection", "keep-alive")
w.Header().Set("Content-Type", "text/plain") w.Header().Set("Content-Type", "text/plain")
fmt.Fprint(w, "1") _, _ = fmt.Fprint(w, "1")
} }
func echoHandler(w http.ResponseWriter, r *http.Request) { func echoHandler(w http.ResponseWriter, r *http.Request) {
@ -149,7 +149,7 @@ func whoamiHandler(w http.ResponseWriter, req *http.Request) {
} }
hostname, _ := os.Hostname() hostname, _ := os.Hostname()
fmt.Fprintln(w, "Hostname:", hostname) _, _ = fmt.Fprintln(w, "Hostname:", hostname)
ifaces, _ := net.Interfaces() ifaces, _ := net.Interfaces()
for _, i := range ifaces { for _, i := range ifaces {
@ -163,10 +163,11 @@ func whoamiHandler(w http.ResponseWriter, req *http.Request) {
case *net.IPAddr: case *net.IPAddr:
ip = v.IP ip = v.IP
} }
fmt.Fprintln(w, "IP:", ip) _, _ = fmt.Fprintln(w, "IP:", ip)
} }
} }
_, _ = fmt.Fprintln(w, "RemoteAddr:", req.RemoteAddr)
if err := req.Write(w); err != nil { if err := req.Write(w); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
@ -204,7 +205,9 @@ func apiHandler(w http.ResponseWriter, req *http.Request) {
case *net.IPAddr: case *net.IPAddr:
ip = v.IP ip = v.IP
} }
data.IP = append(data.IP, ip.String()) if ip != nil {
data.IP = append(data.IP, ip.String())
}
} }
} }