Adds v46 command

This commit is contained in:
Stephen McQuay 2018-02-26 09:30:47 -08:00
parent b87e722658
commit 4b26877517
Signed by: sm
GPG Key ID: 4E4B72F479BA3CE5

41
main.go Normal file
View File

@ -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)
}
}