Explorar o código

Вторая лаба

Алексей %!s(int64=2) %!d(string=hai) anos
pai
achega
7a457e5da5
Modificáronse 2 ficheiros con 39 adicións e 27 borrados
  1. 0 27
      main.go
  2. 39 0
      main_2.go

+ 0 - 27
main.go

@@ -1,27 +0,0 @@
-package main
-
-import (
-    "fmt"
-    "net/http"
-	"log"
-)
-func main() {
-	http.HandleFunc("/", sayhello) // Устанавливаем роутер
-	http.HandleFunc("/home", sayhello1)
-	http.HandleFunc("/omegalul", sayhello2)
-	err := http.ListenAndServe(":8080", nil) // устанавливаем порт веб-сервера
-	if err != nil {
-		log.Fatal("ListenAndServe: ", err)
-	}
-	
-}
-func sayhello(w http.ResponseWriter, r *http.Request) {
-    fmt.Fprintf(w, "Привет!")
-}
-func sayhello1(w http.ResponseWriter, r *http.Request) {
-    fmt.Fprintf(w, "darova ept eto home!")
-}
-func sayhello2(w http.ResponseWriter, r *http.Request) {
-    fmt.Fprintf(w, "kekwait")
-}
-

+ 39 - 0
main_2.go

@@ -0,0 +1,39 @@
+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, "PAL\n")
+	// make GET request
+	response, error := http.Get("https://api.ipify.org/?format=text")
+	if error != nil {
+		fmt.Println(error)
+	}
+
+	// read response body
+	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"))
+}