Test server for connecting to different network types.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
836B

  1. // command v46 serves http on various network types
  2. package main
  3. import (
  4. "flag"
  5. "fmt"
  6. "log"
  7. "net"
  8. "net/http"
  9. "time"
  10. )
  11. const resp = `{"ok": true}
  12. `
  13. var port = flag.Int("port", 8400, "listening port")
  14. var both = flag.Bool("both", false, "listen on ipv4 and ipv6")
  15. var dur = flag.Duration("dur", 10*time.Second, "how long to sleep before giving a request")
  16. func main() {
  17. flag.Parse()
  18. http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
  19. time.Sleep(*dur)
  20. w.Header().Set("Content-Type", "application/json")
  21. fmt.Fprintf(w, resp)
  22. })
  23. a := fmt.Sprintf(":%d", *port)
  24. log.Printf("listening on: %+v", a)
  25. proto := "tcp4"
  26. if *both {
  27. proto = "tcp"
  28. }
  29. l, err := net.Listen(proto, a)
  30. if err != nil {
  31. log.Fatalf("failed to listen: %v", err)
  32. }
  33. if err := http.Serve(l, nil); err != nil {
  34. panic(err)
  35. }
  36. }