From 4b26877517f802ef0338f5264946e0be4171adec Mon Sep 17 00:00:00 2001 From: "Stephen McQuay (smcquay)" Date: Mon, 26 Feb 2018 09:30:47 -0800 Subject: [PATCH] Adds v46 command --- main.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 main.go diff --git a/main.go b/main.go new file mode 100644 index 0000000..a680571 --- /dev/null +++ b/main.go @@ -0,0 +1,41 @@ +// command v46 serves http on various network types +package main + +import ( + "flag" + "fmt" + "log" + "net" + "net/http" + "time" +) + +const resp = `{"ok": true} +` + +var port = flag.Int("port", 8400, "listening port") +var both = flag.Bool("both", false, "listen on ipv4 and ipv6") +var dur = flag.Duration("dur", 10*time.Second, "how long to sleep before giving a request") + +func main() { + flag.Parse() + http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { + time.Sleep(*dur) + w.Header().Set("Content-Type", "application/json") + fmt.Fprintf(w, resp) + }) + + a := fmt.Sprintf(":%d", *port) + log.Printf("listening on: %+v", a) + proto := "tcp4" + if *both { + proto = "tcp" + } + l, err := net.Listen(proto, a) + if err != nil { + log.Fatalf("failed to listen: %v", err) + } + if err := http.Serve(l, nil); err != nil { + panic(err) + } +}