Skip to content

Instantly share code, notes, and snippets.

@sitano
Created January 16, 2017 22: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 sitano/2f8ce171177e185c1fd4a9cb835e62c3 to your computer and use it in GitHub Desktop.
Save sitano/2f8ce171177e185c1fd4a9cb835e62c3 to your computer and use it in GitHub Desktop.
Test web server written in go
package main
import (
"flag"
"log"
"math/rand"
"net/http"
"time"
)
var bind = flag.String("bind", ":8000", "bind address (--bind 127.0.0.1:8000)")
var wait = flag.Duration("wait", time.Millisecond*500, "--wait 500ms")
var debug = flag.Bool("debug", false, "Enable verbose logging")
var random = flag.Bool("rand", false, "Enable random")
func main() {
flag.Parse()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
if *random {
time.Sleep(time.Duration(rand.Intn(int(*wait))))
} else {
time.Sleep(*wait)
}
w.Write([]byte("OK"))
if *debug {
log.Println("request", r.URL.String(), "time", time.Now().Sub(start))
}
})
log.Println("Server bind to", *bind)
log.Fatal(http.ListenAndServe(*bind, nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment