Browse Source

Загрузить файлы ''

gr794_pus 2 years ago
parent
commit
6406c26d17
1 changed files with 47 additions and 0 deletions
  1. 47 0
      main 2.go

+ 47 - 0
main 2.go

@@ -0,0 +1,47 @@
+package main
+
+import (
+	"fmt"
+	"io"
+	"log"
+	"net/http"
+	"regexp"
+	"time"
+)
+
+func main() {
+	http.HandleFunc("/id", idFunc)
+	http.HandleFunc("/info", infoFunk)
+	http.HandleFunc("/status", statusFunk)
+	err := http.ListenAndServe(":8080", nil)
+	if err != nil {
+		log.Fatal("ListenAndServe: ", err)
+	}
+}
+
+func idFunc(w http.ResponseWriter, r *http.Request) {
+	fmt.Fprintf(w, "Pop US 792")
+}
+
+func infoFunk(w http.ResponseWriter, r *http.Request) {
+	w.WriteHeader(http.StatusOK)
+	fmt.Fprintf(w, "status: OK")
+}
+
+func statusFunk(w http.ResponseWriter, r *http.Request) {
+	fmt.Fprintf(w, "Popova Uliana\n")
+	response, error := http.Get("https://api.ipify.org/?format=text")
+	if error != nil {
+		fmt.Println(error)
+	}
+	body, error := io.ReadAll(response.Body)
+	if error != nil {
+		fmt.Println(error)
+	}
+	response.Body.Close()
+	regex := regexp.MustCompile("[.].*[.]")
+	out := regex.ReplaceAllString(string(body), ".***.***.")
+	fmt.Fprint(w, "IP: ", out, "\n")
+	now := time.Now()
+	fmt.Fprintf(w, now.Format("2006-01-02 3:4:5 pm"))
+}