Created
January 14, 2014 05:25
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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