Files
go_htmx_app/main.go
2026-04-04 17:47:43 +00:00

44 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"fmt"
"html/template"
"net/http"
"time"
)
func main() {
// Главная страница
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("template/index.html"))
tmpl.Execute(w, nil)
})
// 1. Возвращаем фрагмент времени
http.HandleFunc("/get-time", func(w http.ResponseWriter, r *http.Request) {
currentTime := time.Now().Format("15:04:05")
fmt.Fprintf(w, "Текущее время: %s", currentTime)
})
// 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")
// Возвращаем фрагмент <li>
fmt.Fprintf(w, "<li class='text-gray-700'>%s (добавлено в %s)</li>", 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, "<div class='text-gray-400 italic text-sm'>Объект успешно аннигилирован</div>")
}
})
fmt.Println("Сервер запущен на :8080...")
http.ListenAndServe(":8080", nil)
}