lol 2 anni fa
parent
commit
0083b91339
2 ha cambiato i file con 37 aggiunte e 26 eliminazioni
  1. 0 26
      main.go
  2. 37 0
      main0.go

+ 0 - 26
main.go

@@ -1,26 +0,0 @@
-package main
-
-import (
-	"fmt"
-	"log"
-	"net/http"
-)
-
-func sayhello(w http.ResponseWriter, r *http.Request) {
-	fmt.Fprintf(w, "HTTP-статус 200")
-}
-func sayinfo(w http.ResponseWriter, r *http.Request) {
-	fmt.Fprintf(w, "НИВ 794")
-}
-func sayOK(w http.ResponseWriter, r *http.Request) {
-	fmt.Fprintf(w, "OK")
-}
-func main() {
-	http.HandleFunc("/info", sayinfo)
-	http.HandleFunc("/", sayhello)
-	http.HandleFunc("/OK", sayOK)
-	err := http.ListenAndServe(":8080", nil) // устанавливаем порт веб-сервера
-	if err != nil {
-		log.Fatal("ListenAndServe: ", err)
-	}
-}

+ 37 - 0
main0.go

@@ -0,0 +1,37 @@
+package main
+
+import (
+	"fmt"
+	"io"
+	"net/http"
+	"regexp"
+	"time"
+)
+
+func main() {
+	http.HandleFunc("/Status", status)
+	http.ListenAndServe(":8080", nil)
+
+}
+
+var re = regexp.MustCompile("(\\d+\\.)(\\d+\\.\\d+)(\\.\\d+)")
+
+func status(w http.ResponseWriter, r *http.Request) {
+	fmt.Fprintf(w, "niv794\n")
+	response, error := http.Get("https://api.ipify.org/?format=text") //API IP-адрес
+	if error != nil {
+		fmt.Println(error)
+	}
+
+	body, error := io.ReadAll(response.Body)
+	if error != nil {
+		fmt.Println(error)
+	}
+
+	response.Body.Close()
+
+	out := re.ReplaceAllString(string(body), "$1***.***$3") //сокрытие октета
+	fmt.Fprint(w, "IP-address: ", out, "\n")
+	now := time.Now()
+	fmt.Fprintf(w, now.Format("2006-01-02 3:4:5 pm")) //дата
+}