feat: added support for env vars

This commit is contained in:
Marcus Noble 2021-08-08 16:38:19 +01:00
parent cea1ae23ac
commit 647c9a8b0c

11
main.go
View File

@ -6,6 +6,7 @@ import (
"net" "net"
"net/http" "net/http"
"os" "os"
"strings"
"time" "time"
) )
@ -19,6 +20,7 @@ type echoResponse struct {
ServerHostname string `json:"serverHostname"` ServerHostname string `json:"serverHostname"`
ServerIP string `json:"serverIP"` ServerIP string `json:"serverIP"`
ServerEnv map[string]string `json:"serverEnv"`
DateTime time.Time `json:"dateTime"` DateTime time.Time `json:"dateTime"`
} }
@ -28,6 +30,14 @@ func main() {
defer conn.Close() defer conn.Close()
ipAddress := conn.LocalAddr().String() ipAddress := conn.LocalAddr().String()
serverEnv := map[string]string{}
for _, env := range os.Environ() {
if strings.HasPrefix(env, "ECHO_") {
parts := strings.Split(env, "=")
serverEnv[strings.TrimPrefix(parts[0], "ECHO_")] = parts[1]
}
}
http.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
resp := echoResponse{ resp := echoResponse{
RequestPath: r.URL.String(), RequestPath: r.URL.String(),
@ -38,6 +48,7 @@ func main() {
RemoteAddr: r.RemoteAddr, RemoteAddr: r.RemoteAddr,
ServerHostname: hostname, ServerHostname: hostname,
ServerIP: ipAddress, ServerIP: ipAddress,
ServerEnv: serverEnv,
DateTime: time.Now(), DateTime: time.Now(),
} }