laba3.go 901 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  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. err := http.ListenAndServe(":8080", nil)
  15. if err != nil {
  16. log.Fatal("ListenAndServer:", err)
  17. return
  18. }
  19. }
  20. func ok(w http.ResponseWriter, r *http.Request) {
  21. fmt.Fprint(w, "ok")
  22. }
  23. func info(w http.ResponseWriter, r *http.Request) {
  24. fmt.Fprint(w, "группа 792\n Антонов Сергей")
  25. }
  26. func status(w http.ResponseWriter, r *http.Request) {
  27. res, _ := http.Get("https://api.ipify.org/")
  28. ip, err := ioutil.ReadAll(res.Body)
  29. if err != nil {
  30. return
  31. }
  32. fmt.Fprintf(w, "%s\n", ip)
  33. today := time.Now()
  34. fmt.Fprintf(w, today.Format("2006-01-02 3:4:5 pm"))
  35. reg := regexp.MustCompile("[.].+[.]")
  36. out := string(ip)
  37. qqq := reg.ReplaceAllString(out, ".o.o.")
  38. fmt.Fprintf(w, "\n%s", qqq)
  39. }