-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
151 lines (124 loc) · 3.82 KB
/
main.go
File metadata and controls
151 lines (124 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
"time"
"guide/templates"
)
func main() {
http.HandleFunc("/", handleIndex)
http.HandleFunc("/sse", handleSSE)
http.HandleFunc("/trigger-toast", handleTriggerToast)
http.HandleFunc("/spam-toasts", handleSpamToasts)
http.HandleFunc("/random-quote", handleRandomQuote)
http.HandleFunc("/delete-item", handleDeleteItem)
http.HandleFunc("/form-submit", handleFormSubmit)
log.Println("Server running at http://localhost:8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
func handleIndex(w http.ResponseWriter, r *http.Request) {
templates.Index().Render(r.Context(), w)
}
func handleTriggerToast(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
toastType := r.FormValue("type")
if toastType == "" {
toastType = "success"
}
messages := map[string]string{
"success": "Operation completed successfully!",
"error": "Something went wrong!",
"info": "Here's some information for you.",
"warning": "Please be careful with this action.",
}
msg := messages[toastType]
if msg == "" {
msg = messages["info"]
}
toast := map[string]string{
"type": toastType,
"message": msg,
}
data, _ := json.Marshal(toast)
broadcast.Send(string(data))
w.WriteHeader(http.StatusOK)
}
func handleDeleteItem(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Mock 2 second delay to show loading state
time.Sleep(2 * time.Second)
toast := map[string]string{
"type": "success",
"message": "Item deleted successfully!",
}
data, _ := json.Marshal(toast)
broadcast.Send(string(data))
w.WriteHeader(http.StatusOK)
}
func handleFormSubmit(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
name := r.FormValue("name")
email := r.FormValue("email")
// Mock 2 second delay to show loading state
time.Sleep(2 * time.Second)
toast := map[string]string{
"type": "success",
"message": "Form submitted! Name: " + name + ", Email: " + email,
}
data, _ := json.Marshal(toast)
broadcast.Send(string(data))
// Return empty response - toast handles feedback
w.WriteHeader(http.StatusOK)
}
func handleSpamToasts(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Send 5 toasts with small delays to demo queue
types := []string{"success", "error", "info", "warning", "success"}
messages := []string{
"First toast incoming!",
"Oops, an error appeared!",
"Here's some info for you.",
"Warning: toast spam detected!",
"And we're done!",
}
go func() {
for i := 0; i < 5; i++ {
toast := map[string]string{
"type": types[i],
"message": messages[i],
}
data, _ := json.Marshal(toast)
broadcast.Send(string(data))
time.Sleep(300 * time.Millisecond)
}
}()
w.WriteHeader(http.StatusOK)
}
var quotes = []string{
"The best way to predict the future is to invent it. — Alan Kay",
"Simplicity is the ultimate sophistication. — Leonardo da Vinci",
"First, solve the problem. Then, write the code. — John Johnson",
"Code is like humor. When you have to explain it, it's bad. — Cory House",
"Make it work, make it right, make it fast. — Kent Beck",
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand. — Martin Fowler",
}
func handleRandomQuote(w http.ResponseWriter, r *http.Request) {
quote := quotes[rand.Intn(len(quotes))]
w.Header().Set("Content-Type", "text/html")
fmt.Fprintf(w, `<blockquote class="text-lg italic">"%s"</blockquote>`, quote)
}