Parcourir la source

Initial commit: Создание веб-сервера

Смолин Алексей Михайлович il y a 4 mois
commit
82d8b34210
2 fichiers modifiés avec 51 ajouts et 0 suppressions
  1. 3 0
      go.mod
  2. 48 0
      main.go

+ 3 - 0
go.mod

@@ -0,0 +1,3 @@
+module webserver
+
+go 1.23.4

+ 48 - 0
main.go

@@ -0,0 +1,48 @@
+package main
+
+import (
+	"fmt"
+	"net/http"
+)
+
+type StudentInfo struct {
+	FullName string `json:"full_name"`
+	Group    string `json:"group"`
+}
+
+func main() {
+	http.HandleFunc("/ok", func(w http.ResponseWriter, r *http.Request) {
+		w.WriteHeader(http.StatusOK)
+		fmt.Fprintln(w, "OK")
+	})
+
+	http.HandleFunc("/info", func(w http.ResponseWriter, r *http.Request) {
+		student := StudentInfo{
+			FullName: "Смолин Алексей Михайлович",
+			Group:    "712",
+		}
+		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>Info Page</title>
+			</head>
+			<body>
+				<h1>Информация о студенте</h1>
+				<p><strong>ФИО:</strong> %s</p>
+				<p><strong>Группа:</strong> %s</p>
+			</body>
+			</html>
+		`, student.FullName, student.Group)
+
+		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)
+	}
+}