Skip to content

Instantly share code, notes, and snippets.

@asim
Created September 28, 2015 09:47
Show Gist options
  • Save asim/7f72e3eee22d64ebbb2a to your computer and use it in GitHub Desktop.
Save asim/7f72e3eee22d64ebbb2a to your computer and use it in GitHub Desktop.
HTTP server that displays environment information
package main
import (
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"net/http"
"os"
"strings"
"text/template"
)
var (
tmpl = `
<html>
<body>
<center>
<h1>Hostname: {{.Hostname}}</h1>
<h3>Request: {{.Request}}</h3>
<table>
<thead>
<th>KEY</th>
<th>VALUE</th>
</thead>
<tbody>
{{range $k,$v := .Env}}
<tr>
<td>{{$k}}</td>
<td>{{$v}}</td>
</tr>
{{end}}
</tbody>
</table>
</center>
</body>
</html>
`
env = map[string]string{}
)
func init() {
for _, v := range os.Environ() {
parts := strings.Split(v, "=")
if len(parts) < 2 {
continue
}
env[parts[0]] = strings.Join(parts[1:], "=")
}
}
type Data struct {
Hostname string
Request string
Env map[string]string
}
func handle(w http.ResponseWriter, r *http.Request) {
t := template.New("t")
t, err := t.Parse(tmpl)
if err != nil {
w.Write([]byte(err.Error()))
return
}
data := Data{
Hostname: r.Host,
Request: r.RequestURI,
Env: env,
}
err = t.Execute(w, data)
if err != nil {
w.Write([]byte(err.Error()))
return
}
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/", handle)
loggedRouter := handlers.CombinedLoggingHandler(os.Stdout, r)
http.ListenAndServe(":80", loggedRouter)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment