forked from traefik/whoami
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
156 lines (130 loc) · 3.6 KB
/
app.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* from: https://github.com/emilevauge/whoamI/blob/master/app.go
* howto:
* go get app.go
* go run app.go
* go install app.go
* CGO_ENABLED=0 go build -a --installsuffix cgo --ldflags="-s" -o whoamI
*
* pc@2017/9/25
*
*/
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"sync"
"net/http"
"net/url"
"os"
"time"
)
var port string
func init() {
flag.StringVar(&port, "port", "80", "listen to the given port.")
}
func main() {
flag.Parse()
// url: index
http.HandleFunc("/", index)
// url: others
http.HandleFunc("/api", api)
http.HandleFunc("/health", healthHandler)
http.HandleFunc("/test", testHandler)
fmt.Println("Listening on port *:" + port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
/*
* curl 127.0.0.1/test
*
*/
func testHandler(w http.ResponseWriter, r *http.Request) {
body := "[test] Hello World\n"
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Content-Type", "text/plain")
fmt.Fprint(w, body)
}
/*
* curl 127.0.0.1/
* curl 127.0.0.1/?wait=2s
*
*/
func index(w http.ResponseWriter, req *http.Request) {
u, _ := url.Parse(req.URL.String())
queryParams := u.Query()
wait := queryParams.Get("wait")
if len(wait) > 0 {
duration, err := time.ParseDuration(wait)
if err == nil {
fmt.Println("request will be delayed for :", duration)
time.Sleep(duration)
} else {
log.Println("[E] ", err.Error())
//fmt.Fprintln(w, "[E] ", err.Error())
}
}
w.Header().Set("Connection", "close")
hostname, _ := os.Hostname()
fmt.Fprintln(w, "Hostname:", hostname)
fmt.Fprintln(w, "\n---- Http Request Headers ----\n")
req.Write(w)
fmt.Fprintln(w, "\n---- Active Endpoint ----\n")
fmt.Fprintln(w, "[howto] version: 0.9 \n",
" curl 127.0.0.1/ \n",
" curl 127.0.0.1/?wait=2s \n",
" curl 127.0.0.1/test \n",
" curl 127.0.0.1/api \n",
" curl 127.0.0.1/health \n",
" curl 127.0.0.1/health -d '302' \n",
"\n")
}
/*
* curl 127.0.0.1/api
*
*/
func api(w http.ResponseWriter, req *http.Request) {
hostname, _ := os.Hostname()
data := struct {
Hostname string `json:"hostname,omitempty"`
Headers http.Header `json:"headers,omitempty"`
}{
hostname,
req.Header,
}
json.NewEncoder(w).Encode(data)
}
/*
* curl 127.0.0.1/health
* curl 127.0.0.1/health -d '404'
*
*/
type healthState struct {
StatusCode int
}
var currentHealthState = healthState{200}
var mutexHealthState = &sync.RWMutex{}
func healthHandler(w http.ResponseWriter, req *http.Request) {
if req.Method == http.MethodPost {
var statusCode int
err := json.NewDecoder(req.Body).Decode(&statusCode)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
log.Println(err.Error())
} else {
mutexHealthState.Lock()
defer mutexHealthState.Unlock()
currentHealthState.StatusCode = statusCode
fmt.Println("Update 'health check' status code =", statusCode)
fmt.Fprintln(w, statusCode)
}
} else {
mutexHealthState.RLock()
defer mutexHealthState.RUnlock()
w.WriteHeader(currentHealthState.StatusCode)
fmt.Println("Current 'health check' status code =", currentHealthState.StatusCode)
fmt.Fprintln(w, currentHealthState.StatusCode)
}
}