Created
January 16, 2017 22:25
-
-
Save sitano/2f8ce171177e185c1fd4a9cb835e62c3 to your computer and use it in GitHub Desktop.
Test web server written in go
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" | |
"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