-
Notifications
You must be signed in to change notification settings - Fork 0
/
route_main.go
50 lines (40 loc) · 843 Bytes
/
route_main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"fmt"
"net/http"
"encoding/json"
)
type Header struct {
IpAddress string
Language string
Software string
}
func Home(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Welcome to the header parser api!\n" +
"From your browser visit the url 0.0.0.0:8080/api/whoami\n" +
"You should receive your Header information returned as JSON.")
}
func WhoAmI(w http.ResponseWriter, r *http.Request) {
h := r.Header
ip := h.Get("X-Real-Ip")
if ip == "" {
ip = h.Get("X-Forward-For")
}
if ip == "" {
ip = r.RemoteAddr
}
lang := h.Get("Accept-Language")
software := h.Get("User-Agent")
header := Header {
IpAddress: ip,
Language: lang,
Software: software,
}
json,err := json.MarshalIndent(&header,"","\t\t")
if err != nil {
return
}
w.Write(json)
return
fmt.Fprintln(w, header)
}