-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgse.go
232 lines (195 loc) · 5.26 KB
/
gse.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// GSE - Go Show Env - see https://github.com/DBuret/gse
package main
import (
"bytes"
"fmt"
"html/template"
"io"
"log"
"net/http"
"os"
"regexp"
"sort"
"strings"
"github.com/namsral/flag"
)
var (
programVersion = "4.1"
programName = "gse"
// Trace logger: debug
Trace *log.Logger
// Info logger: standard info logs
Info *log.Logger
// Warning logger: non fatal errors
Warning *log.Logger
// Error logger: panic
Error *log.Logger
uri, mark string
port int
healthcheck, loggerEP bool
)
// Init : setup loggers
func Init(
traceHandle io.Writer,
infoHandle io.Writer,
warningHandle io.Writer,
errorHandle io.Writer) {
Trace = log.New(traceHandle,
"TRACE: ",
log.Ldate|log.Ltime)
Info = log.New(infoHandle,
"INFO: ",
log.Ldate|log.Ltime)
Warning = log.New(warningHandle,
"WARNING: ",
log.Ldate|log.Ltime)
Error = log.New(errorHandle,
"ERROR: ",
log.Ldate|log.Ltime|log.Lshortfile)
}
func check(err error) {
if err != nil {
Error.Printf("%s", err)
}
}
func sanitize(i string) string {
re := regexp.MustCompile(`[^0-9A-Za-z\{\}\:\,\[\]]`)
return fmt.Sprintf("%q\n", re.ReplaceAllString(i, ""))
}
type output struct {
Method string
Host string
Url string
Proto string
HeaderOutput []string
BodyOutput string
EnvOutput []string
}
func showEnvHandler(w http.ResponseWriter, r *http.Request) {
o := output{
Method: r.Method,
Host: r.Host,
Proto: r.Proto,
Url: fmt.Sprint(r.URL),
HeaderOutput: []string{},
BodyOutput: "",
EnvOutput: []string{}}
Trace.Printf("%s %s%s", o.Method, o.Host, o.Url)
//headers
header := r.Header
sortedHeaders := make([]string, 0, len(header))
for k := range header {
sortedHeaders = append(sortedHeaders, k)
}
sort.Strings(sortedHeaders)
for k := range sortedHeaders {
o.HeaderOutput = append(o.HeaderOutput,
fmt.Sprintf("%s=%s", sortedHeaders[k], strings.Join(header[sortedHeaders[k]], ", ")))
}
//body
buf := new(bytes.Buffer)
buf.ReadFrom(r.Body)
o.BodyOutput = buf.String()
// ENV
for _, e := range os.Environ() {
o.EnvOutput = append(o.EnvOutput, e)
}
sort.Strings(o.EnvOutput)
t, err := template.ParseFiles("template.html")
check(err)
t.Execute(w, o)
}
func versionHandler(w http.ResponseWriter, r *http.Request) {
Trace.Printf("%s %s%s", r.Method, r.Host, r.URL)
w.WriteHeader(200)
fmt.Fprintln(w, programVersion+mark)
}
func healthHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
// health status
case "GET":
if healthcheck {
w.WriteHeader(200)
fmt.Fprintln(w, "I'm alive")
} else {
w.WriteHeader(503)
fmt.Fprintln(w, "I´m sick")
}
// flip/flop health state
case "POST", "PUT":
// this is dirty - no mutex...
healthcheck = !healthcheck
w.WriteHeader(200)
fmt.Fprintln(w, fmt.Sprintf("healthcheck has been switched to %t", healthcheck))
Trace.Printf("healthcheck has been switched to %t", healthcheck)
}
}
func loggerHandler(w http.ResponseWriter, r *http.Request) {
if loggerEP {
switch r.Method {
case "GET":
w.WriteHeader(400)
fmt.Fprintln(w, "logger endpoint uncorrectly called with GET method")
Warning.Printf("logger endpoint uncorrectly called with GET method")
case "POST", "PUT":
bdy := new(bytes.Buffer)
bdy.ReadFrom(r.Body)
w.WriteHeader(200)
fmt.Fprintln(w, "data ingested.")
Trace.Printf(fmt.Sprintf("%s %s%s, LOGGER: %s",
r.Method,
r.Host,
r.URL,
sanitize(bdy.String())))
}
} else {
w.WriteHeader(400)
fmt.Fprintln(w, "logger endpoint called but not activited in configuration")
Warning.Printf("logger endpoint called but not activited in configuration")
}
}
func main() {
Init(os.Stdout, os.Stdout, os.Stdout, os.Stderr)
// env parsing
confManager := flag.NewFlagSetWithEnvPrefix(os.Args[0], "GSE", 0)
confManager.StringVar(&uri, "basepath", "/gse", "base path in the url")
confManager.IntVar(&port, "port", 28657, "default listening port")
confManager.BoolVar(&healthcheck, "healthcheck", true, "enable/disable healthckeck endpoint")
confManager.BoolVar(&loggerEP, "logger", false, "enable/disable logger endpoint")
confManager.StringVar(&mark, "stamp", "", "specify a stamp to be added to version endpoint answer")
confManager.Parse(os.Args[1:])
// get an http server
mux := http.NewServeMux()
// handlers
// /uri
mux.HandleFunc(uri, showEnvHandler)
mux.HandleFunc(uri+"/", showEnvHandler)
// /uri/version
mux.HandleFunc(uri+"/version", versionHandler)
mux.HandleFunc(uri+"/version/", versionHandler)
// /uri/health
mux.HandleFunc(uri+"/health", healthHandler)
mux.HandleFunc(uri+"/health/", healthHandler)
// /uri/logger
mux.HandleFunc(uri+"/logger", loggerHandler)
mux.HandleFunc(uri+"/logger/", loggerHandler)
// default handler for /
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
fmt.Fprintln(w, "Oops, you requested an unknown location.\n FYI, my base path is "+uri)
})
// start http server
s := &http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: mux,
}
// log config used
Info.Printf("Starting %s (%s) on port %d with basepath %s, healthtcheck=%t, and logger_endpoint=%t...\n",
programName,
programVersion+mark,
port,
uri,
healthcheck,
loggerEP)
log.Fatal(s.ListenAndServe())
}