Skip to content

Instantly share code, notes, and snippets.

@drgarcia1986
Last active August 25, 2016 10:25
Show Gist options
  • Save drgarcia1986/0af4eb4e1b562f5d283db4b0ec7fa5d1 to your computer and use it in GitHub Desktop.
Save drgarcia1986/0af4eb4e1b562f5d283db4b0ec7fa5d1 to your computer and use it in GitHub Desktop.
Link redirect and click counter in Go
package main
import (
"fmt"
"net/http"
)
type RedirectHandler struct {
Counter chan string
}
func (handler RedirectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
url := getUrl(r)
go func() { handler.Counter <- url }()
http.Redirect(w, r, url, 301)
}
type StatsHandler struct {
Urls map[string]int
}
func (handler StatsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
url := getUrl(r)
fmt.Fprintf(w, "Count: %d", handler.Urls[url])
}
func feedCounter(counter chan string, urls map[string]int) {
for url := range counter {
urls[url] += 1
}
}
func getUrl(r *http.Request) string {
queryString := r.URL.Query()
return fmt.Sprintf("%s", queryString["u"])
}
func main() {
counter := make(chan string)
urls := make(map[string]int)
defer close(counter)
go feedCounter(counter, urls)
http.Handle("/", RedirectHandler{Counter: counter})
http.Handle("/stats/", StatsHandler{Urls: urls})
http.ListenAndServe(":8080", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment