generated from damir/go_app_template
Update main.go
This commit is contained in:
32
main.go
32
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")
|
||||
// Возвращаем фрагмент <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)
|
||||
}
|
||||
Reference in New Issue
Block a user