From 7bb2450d471a38620d5f2539afd148d54c30e557 Mon Sep 17 00:00:00 2001 From: damir Date: Sat, 4 Apr 2026 17:40:37 +0000 Subject: [PATCH] Update main.go --- main.go | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 593ba19..ffea116 100644 --- a/main.go +++ b/main.go @@ -2,19 +2,43 @@ package main import ( "fmt" + "html/template" "net/http" "time" ) func main() { + // Главная страница http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "Hello from Dockerized Go App!") + tmpl := template.Must(template.ParseFiles("templates/index.html")) + tmpl.Execute(w, nil) }) - http.HandleFunc("/time", func(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "Current time: %s", time.Now().Format(time.RFC1123)) + // 1. Возвращаем фрагмент времени + http.HandleFunc("/get-time", func(w http.ResponseWriter, r *http.Request) { + currentTime := time.Now().Format("15:04:05") + fmt.Fprintf(w, "Текущее время: %s", currentTime) }) - fmt.Println("Server starting on :8080...") + // 2. Обработка формы (возвращаем только новый элемент списка) + http.HandleFunc("/add-item", func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + return + } + item := r.FormValue("item") + // Возвращаем фрагмент
  • + fmt.Fprintf(w, "
  • %s (добавлено в %s)
  • ", item, time.Now().Format("15:04")) + }) + + // 3. Удаление (возвращаем пустоту или сообщение об успехе) + http.HandleFunc("/delete-danger", func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodDelete { + w.WriteHeader(http.StatusOK) + fmt.Fprintf(w, "
    Объект успешно аннигилирован
    ") + } + }) + + fmt.Println("Сервер запущен на :8080...") http.ListenAndServe(":8080", nil) } \ No newline at end of file