main.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "log"
  6. "net/http"
  7. "regexp"
  8. "time"
  9. )
  10. func main() {
  11. http.HandleFunc("/id", idFunc)
  12. http.HandleFunc("/info", infoFunk)
  13. http.HandleFunc("/status", statusFunk)
  14. err := http.ListenAndServe(":8080", nil)
  15. if err != nil {
  16. log.Fatal("ListenAndServe: ", err)
  17. }
  18. }
  19. func idFunc(w http.ResponseWriter, r *http.Request) {
  20. fmt.Fprintf(w, "Pop US 792")
  21. }
  22. func infoFunk(w http.ResponseWriter, r *http.Request) {
  23. w.WriteHeader(http.StatusOK)
  24. fmt.Fprintf(w, "status: OK")
  25. }
  26. func statusFunk(w http.ResponseWriter, r *http.Request) {
  27. fmt.Fprintf(w, "Popova Uliana\n")
  28. response, error := http.Get("https://api.ipify.org/?format=text")
  29. if error != nil {
  30. fmt.Println(error)
  31. }
  32. body, error := io.ReadAll(response.Body)
  33. if error != nil {
  34. fmt.Println(error)
  35. }
  36. response.Body.Close()
  37. regex := regexp.MustCompile("[.].*[.]")
  38. out := regex.ReplaceAllString(string(body), ".***.***.")
  39. fmt.Fprint(w, "IP: ", out, "\n")
  40. now := time.Now()
  41. fmt.Fprintf(w, now.Format("2006-01-02 3:4:5 pm"))
  42. }