123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- 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, ".")
- }
|