Sfoglia il codice sorgente

Добавлена страница Status с выводом IP, ФИО и текущего времени

Смолин Алексей Михайлович 4 mesi fa
parent
commit
2c72c1b311
1 ha cambiato i file con 56 aggiunte e 0 eliminazioni
  1. 56 0
      main.go

+ 56 - 0
main.go

@@ -2,7 +2,10 @@ package main
 
 import (
 	"fmt"
+	"net"
 	"net/http"
+	"strings"
+	"time"
 )
 
 type StudentInfo struct {
@@ -10,6 +13,28 @@ type StudentInfo struct {
 	Group    string `json:"group"`
 }
 
+func GetLocalIP() string {
+	addrs, err := net.InterfaceAddrs()
+	if err != nil {
+		return "Не удалось определить IP"
+	}
+
+	for _, addr := range addrs {
+		if ipNet, ok := addr.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
+			if ipNet.IP.To4() != nil {
+				ip := ipNet.IP.String()
+				parts := strings.Split(ip, ".")
+				if len(parts) == 4 {
+					parts[1] = "***"
+					parts[2] = "***"
+					return strings.Join(parts, ".")
+				}
+			}
+		}
+	}
+	return "IP не найден"
+}
+
 func main() {
 	http.HandleFunc("/ok", func(w http.ResponseWriter, r *http.Request) {
 		w.WriteHeader(http.StatusOK)
@@ -41,6 +66,37 @@ func main() {
 		fmt.Fprintln(w, html)
 	})
 
+	http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
+		student := StudentInfo{
+			FullName: "Смолин Алексей Михайлович",
+			Group:    "712",
+		}
+
+		localIP := GetLocalIP()
+
+		now := time.Now().Format("2006-01-02 3:4:5 pm")
+
+		html := fmt.Sprintf(`
+			<!DOCTYPE html>
+			<html lang="en">
+			<head>
+				<meta charset="UTF-8">
+				<meta name="viewport" content="width=device-width, initial-scale=1.0">
+				<title>Status Page</title>
+			</head>
+			<body>
+				<h1>Статус</h1>
+				<p><strong>IP-адрес компьютера:</strong> %s</p>
+				<p><strong>ФИО:</strong> %s</p>
+				<p><strong>Текущее время:</strong> %s</p>
+			</body>
+			</html>
+		`, localIP, student.FullName, now)
+
+		w.Header().Set("Content-Type", "text/html")
+		fmt.Fprintln(w, html)
+	})
+
 	fmt.Println("Server is running on http://localhost:8080")
 	if err := http.ListenAndServe(":8080", nil); err != nil {
 		fmt.Println("Error starting server:", err)