forked from boxboat/hello-world-webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
98 lines (80 loc) · 1.67 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"context"
"fmt"
"log"
"net"
"net/http"
"os"
"os/signal"
"syscall"
)
const (
version = "v0.0.3"
)
var (
port = "8000"
)
func init() {
port = os.Getenv("PORT")
}
func main() {
log.Println("Starting helloworld application...")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
message := ""
user := r.Header.Get("X-Auth-Request-User")
if user == "" {
message = "No user header found"
} else {
message = fmt.Sprintf("Hello %s", user)
}
internalIP, err := getIP()
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
}
message = message + "\nServer internal IP is: " + internalIP.String()
w.Write([]byte(message))
return
})
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
})
http.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, version)
})
s := http.Server{Addr: ":" + port}
go func() {
log.Fatal(s.ListenAndServe())
}()
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
<-signalChan
log.Println("Shutdown signal received, exiting...")
s.Shutdown(context.Background())
}
func getIP() (net.IP, error) {
var ip net.IP
ifaces, err := net.Interfaces()
if err != nil {
fmt.Println("Something went wrong getting IP")
return ip, err
}
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
fmt.Println("Something went wrong getting IP")
return ip, err
}
for _, addr := range addrs {
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
}
}
return ip, nil
}
//KICK IT