minor changes in code, added better description and Kubernetes example

This commit is contained in:
Łukasz Budnik
2020-11-14 09:01:40 +01:00
parent 4ada9bfd9c
commit ac4f9b9a94
2 changed files with 125 additions and 5 deletions

View File

@@ -15,12 +15,17 @@ var showEnvs = os.Getenv("YOSOY_SHOW_ENVS")
var showFiles = os.Getenv("YOSOY_SHOW_FILES")
func handler(w http.ResponseWriter, req *http.Request) {
fmt.Printf("[%v] - %v - %v - \"%v %v\"\n", hostname, time.Now().Format(time.RFC3339), req.RemoteAddr, req.Method, req.RequestURI)
remoteAddr := req.RemoteAddr
// LastIndex works better with IPv6
if index := strings.LastIndex(remoteAddr, ":"); index > 0 {
remoteAddr = remoteAddr[0:index]
}
fmt.Printf("[%v] - %v - %v - \"%v %v\"\n", hostname, time.Now().Format(time.RFC3339), remoteAddr, req.Method, req.RequestURI)
w.WriteHeader(200)
w.Header().Add("Content-Type", "text/plain")
fmt.Fprintf(w, "Request URI: %v\n", req.RequestURI)
fmt.Fprintf(w, "Hostname: %v\n", hostname)
fmt.Fprintf(w, "Remote IP: %v\n", req.RemoteAddr)
fmt.Fprintf(w, "Remote IP: %v\n", remoteAddr)
counter++
fmt.Fprintf(w, "Called: %v\n", counter)
fmt.Fprintln(w)
@@ -30,7 +35,7 @@ func handler(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "%v: %v\n", name, h)
}
}
if showEnvs == "1" || strings.ToLower(showEnvs) == "yes" || strings.ToLower(showEnvs) == "true" {
if strings.ToLower(showEnvs) == "true" || strings.ToLower(showEnvs) == "yes" || strings.ToLower(showEnvs) == "on" || showEnvs == "1" {
fmt.Fprintln(w)
fmt.Fprintf(w, "Env variables:\n")
for _, e := range os.Environ() {
@@ -54,7 +59,7 @@ func handler(w http.ResponseWriter, req *http.Request) {
}
func main() {
fmt.Printf("[%v] - %v - YoSoy is up!\n", hostname, time.Now().Format(time.RFC3339))
fmt.Printf("[%v] - %v - yosoy is up!\n", hostname, time.Now().Format(time.RFC3339))
http.HandleFunc("/", handler)
http.ListenAndServe(":80", nil)
}