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/http"
"os"
"strings"
"time"
)
@ -17,9 +18,10 @@ type echoResponse struct {
Headers map[string][]string `json:"headers"`
RemoteAddr string `json:"remoteAddr"`
ServerHostname string `json:"serverHostname"`
ServerIP string `json:"serverIP"`
DateTime time.Time `json:"dateTime"`
ServerHostname string `json:"serverHostname"`
ServerIP string `json:"serverIP"`
ServerEnv map[string]string `json:"serverEnv"`
DateTime time.Time `json:"dateTime"`
}
func main() {
@ -28,6 +30,14 @@ func main() {
defer conn.Close()
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) {
resp := echoResponse{
RequestPath: r.URL.String(),
@ -38,6 +48,7 @@ func main() {
RemoteAddr: r.RemoteAddr,
ServerHostname: hostname,
ServerIP: ipAddress,
ServerEnv: serverEnv,
DateTime: time.Now(),
}