main_2.go 809 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http"
  6. "regexp"
  7. "time"
  8. )
  9. func main() {
  10. fmt.Println("starting server")
  11. http.HandleFunc("/status", status)
  12. http.ListenAndServe(":8086", nil)
  13. }
  14. var re = regexp.MustCompile("(\\d+\\.)(\\d+\\.\\d+)(\\.\\d+)")
  15. func status(w http.ResponseWriter, r *http.Request) {
  16. fmt.Fprintf(w, "PAL\n")
  17. // make GET request
  18. response, error := http.Get("https://api.ipify.org/?format=text")
  19. if error != nil {
  20. fmt.Println(error)
  21. }
  22. // read response body
  23. body, error := io.ReadAll(response.Body)
  24. if error != nil {
  25. fmt.Println(error)
  26. }
  27. response.Body.Close()
  28. out := re.ReplaceAllString(string(body), "$1***.***$3")
  29. fmt.Fprint(w, "IP-address: ", out, "\n")
  30. now := time.Now()
  31. fmt.Fprintf(w, now.Format("2006-01-02 3:4:5 pm"))
  32. }