main_2.go 776 B

123456789101112131415161718192021222324252627282930313233343536373839
  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, "PAL\n")
  16. // make GET request
  17. response, error := http.Get("https://api.ipify.org/?format=text")
  18. if error != nil {
  19. fmt.Println(error)
  20. }
  21. // read response body
  22. body, error := io.ReadAll(response.Body)
  23. if error != nil {
  24. fmt.Println(error)
  25. }
  26. response.Body.Close()
  27. out := re.ReplaceAllString(string(body), "$1***.***$3")
  28. fmt.Fprint(w, "IP-address: ", out, "\n")
  29. now := time.Now()
  30. fmt.Fprintf(w, now.Format("2006-01-02 3:4:5 pm"))
  31. }