2_laba.go 821 B

123456789101112131415161718192021222324252627282930313233343536
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net"
  6. "net/http"
  7. "regexp"
  8. "time"
  9. )
  10. func main() {
  11. http.HandleFunc("/ok", sayhello)
  12. http.HandleFunc("/info", sayhello1)
  13. http.HandleFunc("/status", status)
  14. err := http.ListenAndServe(":8080", nil)
  15. if err != nil {
  16. log.Fatal("ListenAndServe: ", err)
  17. }
  18. }
  19. func sayhello(w http.ResponseWriter, r *http.Request) {
  20. fmt.Fprintf(w, "ok")
  21. w.WriteHeader(200)
  22. }
  23. func sayhello1(w http.ResponseWriter, r *http.Request) {
  24. fmt.Fprintf(w, "Незговоров Н.В.")
  25. }
  26. func status(w http.ResponseWriter, r *http.Request) {
  27. time := time.Now()
  28. ip, _, _ := net.SplitHostPort(r.RemoteAddr)
  29. re := regexp.MustCompile(`[.].*[.]`)
  30. fmt.Fprintln(w, re.ReplaceAllString(ip, ".*.*."))
  31. fmt.Fprintf(w, "Незговоров Н.В."+"\n")
  32. fmt.Fprintln(w, time.Format("2006-01-02 3:4:5 pm"))
  33. }