main0.go 759 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http"
  6. "regexp"
  7. "time"
  8. )
  9. func main() {
  10. http.HandleFunc("/Status", status)
  11. http.ListenAndServe(":8080", nil)
  12. }
  13. var re = regexp.MustCompile("(\\d+\\.)(\\d+\\.\\d+)(\\.\\d+)")
  14. func status(w http.ResponseWriter, r *http.Request) {
  15. fmt.Fprintf(w, "niv794\n")
  16. response, error := http.Get("https://api.ipify.org/?format=text") //API IP-адрес
  17. if error != nil {
  18. fmt.Println(error)
  19. }
  20. body, error := io.ReadAll(response.Body)
  21. if error != nil {
  22. fmt.Println(error)
  23. }
  24. response.Body.Close()
  25. out := re.ReplaceAllString(string(body), "$1***.***$3") //сокрытие октета
  26. fmt.Fprint(w, "IP-address: ", out, "\n")
  27. now := time.Now()
  28. fmt.Fprintf(w, now.Format("2006-01-02 3:4:5 pm")) //дата
  29. }