浏览代码

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

gr794_pus 2 年之前
父节点
当前提交
ea50ba9659
共有 2 个文件被更改,包括 56 次插入24 次删除
  1. 9 0
      Dockerfile
  2. 47 24
      main.go

+ 9 - 0
Dockerfile

@@ -0,0 +1,9 @@
+FROM golang
+
+WORKDIR /app
+
+COPY ./app .
+
+EXPOSE 8080
+
+CMD ["go", "run", "main.go"]

+ 47 - 24
main.go

@@ -1,24 +1,47 @@
-package main
-
-import (
-	"fmt"
-	"log"
-	"net/http"
-)
-
-func main() {
-
-	http.HandleFunc("/info", info)           // Устанавливаем роутер
-	http.HandleFunc("/ok", ok)               // Устанавливаем роутер
-	err := http.ListenAndServe(":8080", nil) // устанавливаем порт веб-сервера
-	if err != nil {
-		log.Fatal("ListenAndServe: ", err)
-	}
-
-}
-func info(w http.ResponseWriter, r *http.Request) {
-	fmt.Fprintf(w, "Попова Ульяна, 792 группа")
-}
-func ok(w http.ResponseWriter, r *http.Request) {
-	fmt.Fprintf(w, "status OK ")
-}
+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"))
+}