server.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "time"
  6. )
  7. // Start the web server for reciving test request
  8. // in Zoraxy, point test.localhost to this server at the given port in the start variables
  9. func startWebServer() {
  10. http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  11. // Print the request details to console
  12. fmt.Printf("Timestamp: %s\n", time.Now().Format(time.RFC1123))
  13. fmt.Printf("Request type: %s\n", r.Method)
  14. fmt.Printf("Payload size: %d bytes\n", r.ContentLength)
  15. fmt.Printf("Request URI: %s\n", r.RequestURI)
  16. fmt.Printf("User Agent: %s\n", r.UserAgent())
  17. fmt.Printf("Remote Address: %s\n", r.RemoteAddr)
  18. fmt.Println("----------------------------------------")
  19. //Set header to text
  20. w.Header().Set("Content-Type", "text/plain")
  21. // Send response, print the request details to web page
  22. w.Write([]byte("----------------------------------------\n"))
  23. w.Write([]byte("Request type: " + r.Method + "\n"))
  24. w.Write([]byte(fmt.Sprintf("Payload size: %d bytes\n", r.ContentLength)))
  25. w.Write([]byte("Request URI: " + r.RequestURI + "\n"))
  26. w.Write([]byte("User Agent: " + r.UserAgent() + "\n"))
  27. w.Write([]byte("Remote Address: " + r.RemoteAddr + "\n"))
  28. w.Write([]byte("----------------------------------------\n"))
  29. })
  30. go func() {
  31. err := http.ListenAndServe(fmt.Sprintf(":%d", benchmarkWebserverListeningPort), nil)
  32. if err != nil {
  33. fmt.Printf("Failed to start server: %v\n", err)
  34. stopchan <- true
  35. }
  36. }()
  37. }