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
1 changed files with 14 additions and 3 deletions

17
main.go
View File

@ -6,6 +6,7 @@ import (
"net" "net"
"net/http" "net/http"
"os" "os"
"strings"
"time" "time"
) )
@ -17,9 +18,10 @@ type echoResponse struct {
Headers map[string][]string `json:"headers"` Headers map[string][]string `json:"headers"`
RemoteAddr string `json:"remoteAddr"` RemoteAddr string `json:"remoteAddr"`
ServerHostname string `json:"serverHostname"` ServerHostname string `json:"serverHostname"`
ServerIP string `json:"serverIP"` ServerIP string `json:"serverIP"`
DateTime time.Time `json:"dateTime"` ServerEnv map[string]string `json:"serverEnv"`
DateTime time.Time `json:"dateTime"`
} }
func main() { func main() {
@ -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(),
} }