Last active
December 28, 2015 10:39
-
-
Save andreadipersio/7488305 to your computer and use it in GitHub Desktop.
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 ( | |
"fmt" | |
"log" | |
"flag" | |
"time" | |
"net/http" | |
) | |
const ( | |
readTimeout = time.Duration(1 * time.Second) | |
writeTimeout = readTimeout | |
) | |
var ( | |
port int | |
) | |
func rootHandler(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprint(w, "Hello, World!") | |
} | |
func grumpyHandler(w http.ResponseWriter, r *http.Request) { | |
time.Sleep(writeTimeout) | |
fmt.Fprintf(w, "You should not see this, I'm too slow!") | |
} | |
func init() { | |
flag.IntVar(&port, "port", 8080, "HTTP Server Port") | |
flag.Parse() | |
} | |
func main() { | |
httpAddr := fmt.Sprintf(":%v", port) | |
log.Printf("Listening to %v", httpAddr) | |
http.HandleFunc("/grumpy", grumpyHandler) | |
http.HandleFunc("/", rootHandler) | |
// http://golang.org/pkg/net/http/#Server | |
srv := http.Server { | |
Addr: httpAddr, | |
ReadTimeout: readTimeout, | |
WriteTimeout: writeTimeout, | |
} | |
log.Fatal(srv.ListenAndServe()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment