Skip to content

Instantly share code, notes, and snippets.

@ewalk153
Created April 29, 2014 15:38
Show Gist options
  • Select an option

  • Save ewalk153/11403929 to your computer and use it in GitHub Desktop.

Select an option

Save ewalk153/11403929 to your computer and use it in GitHub Desktop.
Record requests and display results, in memory
package main
import (
"fmt"
"net/http"
"os"
"strings"
)
//History of recent posts
type History struct {
Recent []string
}
/*
Appends a message to history */
func (h *History) Append(msg string) {
h.Recent = append(h.Recent, msg)
}
//Displays recent elements
func (h *History) WebList() string {
return strings.Join(h.Recent, "\n---------\n")
}
func (h *History) Record(w http.ResponseWriter, r *http.Request) {
msg := r.FormValue("mandrill_events")
h.Append(msg)
fmt.Fprint(w, "ok")
}
func (h *History) Index(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, h.WebList())
if len(h.Recent) == 0 {
fmt.Fprint(w, "Empty so far")
}
}
func envOrDef(envVar, def string) string {
if val := os.Getenv(envVar); len(val) > 0 {
return val
}
return def
}
var (
port = ":" + envOrDef("PORT", "8211")
)
func main() {
hist := History{}
http.HandleFunc("/", hist.Index)
http.HandleFunc("/record", hist.Record)
http.ListenAndServe(port, nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment