lab2.go 879 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", ok)
  12. http.HandleFunc("/info", info)
  13. http.HandleFunc("/status", status)
  14. http.Handle("/", http.FileServer(http.Dir("./ip")))
  15. err := http.ListenAndServe(":8080", nil)
  16. if err != nil {
  17. log.Fatal("ListenAndServe: ", err)
  18. }
  19. }
  20. func ok(w http.ResponseWriter, r *http.Request) {
  21. fmt.Fprintf(w, "ok")
  22. w.WriteHeader(200)
  23. }
  24. func info(w http.ResponseWriter, r *http.Request) {
  25. fmt.Fprintf(w, "Морозов М.А. 792")
  26. }
  27. func status(w http.ResponseWriter, r *http.Request) {
  28. time := time.Now()
  29. ip, _, _ := net.SplitHostPort(r.RemoteAddr)
  30. re := regexp.MustCompile(`[.].*[.]`)
  31. fmt.Fprintln(w, re.ReplaceAllString(ip, ".*.*."))
  32. fmt.Fprintf(w, "Морозов М.А."+"\n")
  33. fmt.Fprintln(w, time.Format("2006-01-02 3:4:5 pm"))
  34. }