Skip to content

Instantly share code, notes, and snippets.

@dustin
Created January 14, 2014 05:25
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 dustin/8413525 to your computer and use it in GitHub Desktop.
Save dustin/8413525 to your computer and use it in GitHub Desktop.
Simple tool for being a web server and and reporting what's happening. I use this for debugging client apps mostly.
package main
import (
"flag"
"log"
"net/http"
"time"
)
var addr = flag.String("addr", ":3232", "binding address")
func reporter(ch <-chan string) {
t := time.Tick(5 * time.Second)
m := map[string]int{}
for {
select {
case s := <-ch:
m[s] = m[s] + 1
case <-t:
if len(m) > 0 {
log.Printf("Got some things")
}
for k, v := range m {
log.Printf(" %v: %v", k, v)
}
m = map[string]int{}
}
}
}
func main() {
ch := make(chan string)
go reporter(ch)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
s := r.Method + ":" + r.URL.Path
if r.URL.RawQuery != "" {
s += "?" + r.URL.RawQuery
}
ch <- s
w.WriteHeader(201)
})
log.Printf("Listening and serving on %v", *addr)
log.Fatal(http.ListenAndServe(*addr, nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment