Skip to content

Instantly share code, notes, and snippets.

@andreadipersio
Created November 15, 2013 23:29
Show Gist options
  • Save andreadipersio/7493471 to your computer and use it in GitHub Desktop.
Save andreadipersio/7493471 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"flag"
"net/http"
)
var (
port int
)
func visitServer(visitChan chan bool) {
var visits int
for {
if <-visitChan {
visits++
log.Printf("%v visits", visits)
}
}
}
func rootHandler(visitChan chan bool, w http.ResponseWriter, r *http.Request) {
visitChan <-true
fmt.Fprint(w, "Hello, World!")
}
func init() {
flag.IntVar(&port, "port", 8080, "HTTP Server Port")
flag.Parse()
}
func main() {
var visitChan = make(chan bool)
go visitServer(visitChan)
httpAddr := fmt.Sprintf(":%v", port)
log.Printf("Listening to %v", httpAddr)
http.HandleFunc("/", func (w http.ResponseWriter, r *http.Request) {
rootHandler(visitChan, w, r)
})
log.Fatal(http.ListenAndServe(httpAddr, nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment