瀏覽代碼

Добавить 'laba2'

gr714_anidm 4 月之前
父節點
當前提交
aaaf4b7b97
共有 1 個文件被更改,包括 225 次插入0 次删除
  1. 225 0
      laba2

+ 225 - 0
laba2

@@ -0,0 +1,225 @@
+package main
+
+import (
+	"encoding/json"
+	"fmt"
+	"net/http"
+	"strings"
+	"time"
+)
+
+const (
+	studentName  = "Аникин Илья Дмитриевич"
+	groupNumber  = "Группа 714"
+	ipServiceURL = "https://api.ipify.org?format=json"
+)
+
+type IPResponse struct {
+	IP string `json:"ip"`
+}
+
+func main() {
+	http.HandleFunc("/", homeHandler) // Главная страница
+	http.HandleFunc("/ok", okHandler)
+	http.HandleFunc("/info", infoHandler)
+	http.HandleFunc("/status", statusHandler)
+
+	fmt.Println("Сервер запущен на порту 8080...")
+	if err := http.ListenAndServe(":8080", nil); err != nil {
+		fmt.Println("Ошибка при запуске сервера:", err)
+	}
+}
+
+func homeHandler(w http.ResponseWriter, r *http.Request) {
+	fmt.Fprintln(w, `
+		<!DOCTYPE html>
+		<html lang="ru">
+		<head>
+			<meta charset="UTF-8">
+			<meta name="viewport" content="width=device-width, initial-scale=1.0">
+			<title>Главная страница</title>
+			<style>
+				body { font-family: Arial, sans-serif; text-align: center; }
+				nav { margin-top: 20px; }
+				a {
+					text-decoration: none;
+					color: white;
+					background-color: #007BFF;
+					padding: 10px 15px;
+					border-radius: 5px;
+					margin: 0 5px;
+					transition: background-color 0.3s;
+				}
+				a:hover {
+					background-color: #0056b3;
+				}
+			</style>
+		</head>
+		<body>
+			<h1>Добро пожаловать!</h1>
+			<p>Выберите страницу:</p>
+			<nav>
+				<a href="/ok">OK</a> 
+				<a href="/info">Info</a> 
+				<a href="/status">Status</a>
+			</nav>
+		</body>
+		</html>
+	`)
+}
+
+func okHandler(w http.ResponseWriter, r *http.Request) {
+	w.WriteHeader(http.StatusOK)
+	fmt.Fprintln(w, `
+		<!DOCTYPE html>
+		<html lang="ru">
+		<head>
+			<meta charset="UTF-8">
+			<meta name="viewport" content="width=device-width, initial-scale=1.0">
+			<title>Статус OK</title>
+			<style>
+				body { font-family: Arial, sans-serif; }
+				nav { margin-top: 20px; }
+				a {
+					text-decoration: none;
+					color: white;
+					background-color: #007BFF;
+					padding: 10px 15px;
+					border-radius: 5px;
+					margin: 0 5px;
+					transition: background-color 0.3s;
+				}
+				a:hover {
+					background-color: #0056b3;
+				}
+			</style>
+		</head>
+		<body>
+			<h1>Статус: OK</h1>
+			<nav>
+				<a href="/">Главная</a> 
+				<a href="/ok">OK</a> 
+				<a href="/info">Info</a> 
+				<a href="/status">Status</a>
+			</nav>
+		</body>
+		</html>
+	`)
+}
+
+func infoHandler(w http.ResponseWriter, r *http.Request) {
+	fmt.Fprintf(w, `
+		<!DOCTYPE html>
+		<html lang="ru">
+		<head>
+			<meta charset="UTF-8">
+			<meta name="viewport" content="width=device-width, initial-scale=1.0">
+			<title>Информация о студенте</title>
+			<style>
+				body { font-family: Arial, sans-serif; }
+				nav { margin-top: 20px; }
+				a {
+					text-decoration: none;
+					color: white;
+					background-color: #007BFF;
+					padding: 10px 15px;
+					border-radius: 5px;
+					margin: 0 5px;
+					transition: background-color 0.3s;
+				}
+				a:hover {
+					background-color: #0056b3;
+				}
+			</style>
+		</head>
+		<body>
+			<h1>Информация о студенте</h1>
+ <p>Студент: %s</p>
+			<p>Номер группы: %s</p>
+			<nav>
+				<a href="/">Главная</a> 
+				<a href="/ok">OK</a> 
+				<a href="/info">Info</a> 
+				<a href="/status">Status</a>
+			</nav>
+		</body>
+		</html>
+	`, studentName, groupNumber)
+}
+
+func statusHandler(w http.ResponseWriter, r *http.Request) {
+	ip, err := getExternalIP()
+	if err != nil {
+		http.Error(w, "Не удалось получить внешний IP-адрес", http.StatusInternalServerError)
+		return
+	}
+
+	// Скрываем 2 и 3 октеты IP-адреса
+	ipParts := splitIP(ip)
+	if len(ipParts) == 4 {
+		ip = fmt.Sprintf("%s.*.*.%s", ipParts[0], ipParts[3])
+	}
+
+	currentTime := time.Now().Format("2006-01-02 3:4:5 pm")
+	fmt.Fprintf(w, `
+		<!DOCTYPE html>
+		<html lang="ru">
+		<head>
+			<meta charset="UTF-8">
+			<meta name="viewport" content="width=device-width, initial-scale=1.0">
+			<title>Статус</title>
+			<style>
+				body { font-family: Arial, sans-serif; }
+				nav { margin-top: 20px; }
+				a {
+					text-decoration: none;
+					color: white;
+					background-color: #007BFF;
+					padding: 10px 15px;
+					border-radius: 5px;
+					margin: 0 5px;
+					transition: background-color 0.3s;
+				}
+				a:hover {
+					background-color: #0056b3;
+				}
+			</style>
+		</head>
+		<body>
+			<h1>Статус</h1>
+			<p>IP-адрес: %s</p>
+			<p>ФИО студента: %s</p>
+			<p>Текущее время: %s</p>
+			<nav>
+				<a href="/">Главная</a> 
+				<a href="/ok">OK</a> 
+				<a href="/info">Info</a> 
+				<a href="/status">Status</a>
+			</nav>
+		</body>
+		</html>
+	`, ip, studentName, currentTime)
+}
+
+func getExternalIP() (string, error) {
+	resp, err := http.Get(ipServiceURL)
+	if err != nil {
+		return "", err
+	}
+	defer resp.Body.Close()
+
+	if resp.StatusCode != http.StatusOK {
+		return "", fmt.Errorf("ошибка при получении IP: %s", resp.Status)
+	}
+
+	var ipResponse IPResponse
+	if err := json.NewDecoder(resp.Body).Decode(&ipResponse); err != nil {
+		return "", err
+	}
+
+	return ipResponse.IP, nil
+}
+
+func splitIP(ip string) []string {
+	return strings.Split(ip, ".")
+}