Skip to content

Instantly share code, notes, and snippets.

@iolalla
Created September 29, 2020 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iolalla/fcd7fb8721995ec32adcc5eb2ced660f to your computer and use it in GitHub Desktop.
Save iolalla/fcd7fb8721995ec32adcc5eb2ced660f to your computer and use it in GitHub Desktop.
I needed to view env variables in Appengine Standard. Why? Because I needed to know so I thought I could add an environment variable and I could track the value of this env variable and I could track what other env vars the code was receiving.
package main
/**
* I needed to view env variables in Appengine Standard. Why? Because I needed to know
* so I thought I could add an environment variable and I could track the value of this env variable and I could track
* what other env vars the code was receiving.
*
* So I decided to make a simple function that gets all the env vars and send them as HTML as a a response to the URL
* call
* Author: iolalla@gmail.com
*/
import (
"bytes"
"log"
"net/http"
"html/template"
"os"
)
const HTML = `
<!DOCTYPE html>
<html>
<head><title>Environment Variables</title></head>
<body>
{{range $v := .}} <br> {{.}}</br>
{{end}}
</body>
</html>
`
func system(w http.ResponseWriter, r *http.Request) {
var tpl bytes.Buffer
templatito := template.Must(template.New("createTmpl").Parse(HTML))
err := templatito.Execute(&tpl, os.Environ())
if err != nil {
log.Print(err)
}
w.Header().Set("Content-Type", " text/html; charset=UTF-8")
w.WriteHeader(http.StatusOK)
result := tpl.String()
w.Write([]byte(result))
}
func main() {
http.HandleFunc("/system", system)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("Defaulting to port %s", port)
}
log.Printf("Listening on port %s", port)
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Print(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment