laba2 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "strings"
  7. "time"
  8. )
  9. const (
  10. studentName = "Аникин Илья Дмитриевич"
  11. groupNumber = "Группа 714"
  12. ipServiceURL = "https://api.ipify.org?format=json"
  13. )
  14. type IPResponse struct {
  15. IP string `json:"ip"`
  16. }
  17. func main() {
  18. http.HandleFunc("/", homeHandler) // Главная страница
  19. http.HandleFunc("/ok", okHandler)
  20. http.HandleFunc("/info", infoHandler)
  21. http.HandleFunc("/status", statusHandler)
  22. fmt.Println("Сервер запущен на порту 8080...")
  23. if err := http.ListenAndServe(":8080", nil); err != nil {
  24. fmt.Println("Ошибка при запуске сервера:", err)
  25. }
  26. }
  27. func homeHandler(w http.ResponseWriter, r *http.Request) {
  28. fmt.Fprintln(w, `
  29. <!DOCTYPE html>
  30. <html lang="ru">
  31. <head>
  32. <meta charset="UTF-8">
  33. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  34. <title>Главная страница</title>
  35. <style>
  36. body { font-family: Arial, sans-serif; text-align: center; }
  37. nav { margin-top: 20px; }
  38. a {
  39. text-decoration: none;
  40. color: white;
  41. background-color: #007BFF;
  42. padding: 10px 15px;
  43. border-radius: 5px;
  44. margin: 0 5px;
  45. transition: background-color 0.3s;
  46. }
  47. a:hover {
  48. background-color: #0056b3;
  49. }
  50. </style>
  51. </head>
  52. <body>
  53. <h1>Добро пожаловать!</h1>
  54. <p>Выберите страницу:</p>
  55. <nav>
  56. <a href="/ok">OK</a>
  57. <a href="/info">Info</a>
  58. <a href="/status">Status</a>
  59. </nav>
  60. </body>
  61. </html>
  62. `)
  63. }
  64. func okHandler(w http.ResponseWriter, r *http.Request) {
  65. w.WriteHeader(http.StatusOK)
  66. fmt.Fprintln(w, `
  67. <!DOCTYPE html>
  68. <html lang="ru">
  69. <head>
  70. <meta charset="UTF-8">
  71. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  72. <title>Статус OK</title>
  73. <style>
  74. body { font-family: Arial, sans-serif; }
  75. nav { margin-top: 20px; }
  76. a {
  77. text-decoration: none;
  78. color: white;
  79. background-color: #007BFF;
  80. padding: 10px 15px;
  81. border-radius: 5px;
  82. margin: 0 5px;
  83. transition: background-color 0.3s;
  84. }
  85. a:hover {
  86. background-color: #0056b3;
  87. }
  88. </style>
  89. </head>
  90. <body>
  91. <h1>Статус: OK</h1>
  92. <nav>
  93. <a href="/">Главная</a>
  94. <a href="/ok">OK</a>
  95. <a href="/info">Info</a>
  96. <a href="/status">Status</a>
  97. </nav>
  98. </body>
  99. </html>
  100. `)
  101. }
  102. func infoHandler(w http.ResponseWriter, r *http.Request) {
  103. fmt.Fprintf(w, `
  104. <!DOCTYPE html>
  105. <html lang="ru">
  106. <head>
  107. <meta charset="UTF-8">
  108. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  109. <title>Информация о студенте</title>
  110. <style>
  111. body { font-family: Arial, sans-serif; }
  112. nav { margin-top: 20px; }
  113. a {
  114. text-decoration: none;
  115. color: white;
  116. background-color: #007BFF;
  117. padding: 10px 15px;
  118. border-radius: 5px;
  119. margin: 0 5px;
  120. transition: background-color 0.3s;
  121. }
  122. a:hover {
  123. background-color: #0056b3;
  124. }
  125. </style>
  126. </head>
  127. <body>
  128. <h1>Информация о студенте</h1>
  129. <p>Студент: %s</p>
  130. <p>Номер группы: %s</p>
  131. <nav>
  132. <a href="/">Главная</a>
  133. <a href="/ok">OK</a>
  134. <a href="/info">Info</a>
  135. <a href="/status">Status</a>
  136. </nav>
  137. </body>
  138. </html>
  139. `, studentName, groupNumber)
  140. }
  141. func statusHandler(w http.ResponseWriter, r *http.Request) {
  142. ip, err := getExternalIP()
  143. if err != nil {
  144. http.Error(w, "Не удалось получить внешний IP-адрес", http.StatusInternalServerError)
  145. return
  146. }
  147. // Скрываем 2 и 3 октеты IP-адреса
  148. ipParts := splitIP(ip)
  149. if len(ipParts) == 4 {
  150. ip = fmt.Sprintf("%s.*.*.%s", ipParts[0], ipParts[3])
  151. }
  152. currentTime := time.Now().Format("2006-01-02 3:4:5 pm")
  153. fmt.Fprintf(w, `
  154. <!DOCTYPE html>
  155. <html lang="ru">
  156. <head>
  157. <meta charset="UTF-8">
  158. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  159. <title>Статус</title>
  160. <style>
  161. body { font-family: Arial, sans-serif; }
  162. nav { margin-top: 20px; }
  163. a {
  164. text-decoration: none;
  165. color: white;
  166. background-color: #007BFF;
  167. padding: 10px 15px;
  168. border-radius: 5px;
  169. margin: 0 5px;
  170. transition: background-color 0.3s;
  171. }
  172. a:hover {
  173. background-color: #0056b3;
  174. }
  175. </style>
  176. </head>
  177. <body>
  178. <h1>Статус</h1>
  179. <p>IP-адрес: %s</p>
  180. <p>ФИО студента: %s</p>
  181. <p>Текущее время: %s</p>
  182. <nav>
  183. <a href="/">Главная</a>
  184. <a href="/ok">OK</a>
  185. <a href="/info">Info</a>
  186. <a href="/status">Status</a>
  187. </nav>
  188. </body>
  189. </html>
  190. `, ip, studentName, currentTime)
  191. }
  192. func getExternalIP() (string, error) {
  193. resp, err := http.Get(ipServiceURL)
  194. if err != nil {
  195. return "", err
  196. }
  197. defer resp.Body.Close()
  198. if resp.StatusCode != http.StatusOK {
  199. return "", fmt.Errorf("ошибка при получении IP: %s", resp.Status)
  200. }
  201. var ipResponse IPResponse
  202. if err := json.NewDecoder(resp.Body).Decode(&ipResponse); err != nil {
  203. return "", err
  204. }
  205. return ipResponse.IP, nil
  206. }
  207. func splitIP(ip string) []string {
  208. return strings.Split(ip, ".")
  209. }