-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.go
167 lines (144 loc) · 3.99 KB
/
routes.go
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package routes
import (
"errors"
"fmt"
"html/template"
"io"
"net/http"
"time"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/peyzor/live-broadcast-chat/broadcast"
cMiddleware "github.com/peyzor/live-broadcast-chat/routes/middleware"
)
var bc *broadcast.Broadcast
type TemplateRegistry struct {
templates map[string]*template.Template
}
func (t *TemplateRegistry) Render(w io.Writer, name string, data any, c echo.Context) error {
tmpl, ok := t.templates[name]
if !ok {
err := errors.New("Template not found -> " + name)
return err
}
// if we are loading a partial base will be missing
base := tmpl.Lookup("base.html")
if base == nil {
return tmpl.ExecuteTemplate(w, name, data)
}
return tmpl.ExecuteTemplate(w, "base.html", data)
}
func Setup(e *echo.Echo) error {
if bc == nil {
bc = broadcast.NewBroadcast()
}
SetupStaticAssets(e)
templates := make(map[string]*template.Template)
templates["home.html"] = template.Must(
template.New("").ParseFiles(
"templates/pages/home.html",
"templates/base.html",
),
)
templates["404.html"] = template.Must(
template.New("").ParseFiles(
"templates/pages/404.html",
"templates/base.html",
),
)
templates["live_chat.html"] = template.Must(
template.New("").ParseFiles(
"templates/pages/live_chat.html",
"templates/base.html",
"templates/partials/chat_input.html",
),
)
templates["about.html"] = template.Must(
template.New("").ParseFiles("templates/pages/about.html", "templates/base.html"),
)
templates["clicked.html"] = template.Must(
template.New("").ParseFiles("templates/partials/clicked.html"),
)
templates["chat_msg.html"] = template.Must(
template.New("").ParseFiles("templates/partials/chat_msg.html"),
)
templates["chat_input.html"] = template.Must(
template.New("").ParseFiles("templates/partials/chat_input.html"),
)
e.Renderer = &TemplateRegistry{
templates: templates,
}
root := e.Group("/", cMiddleware.CacheControl(0))
root.GET("", func(c echo.Context) error {
// Perform the redirect
return c.Redirect(http.StatusFound, "/live-chat")
})
root.GET("404", func(c echo.Context) error {
return c.Render(http.StatusOK, "404.html", map[string]any{})
})
root.GET("about", func(c echo.Context) error {
return c.Render(http.StatusOK, "about.html", map[string]any{})
})
root.GET("chatroom", func(c echo.Context) error {
handler := handleSSE(c, e.Renderer)
handler(c.Response().Writer, c.Request())
return nil
})
root.GET("live-chat", func(c echo.Context) error {
return c.Render(http.StatusOK, "live_chat.html", map[string]any{})
})
e.POST("sendChat", func(c echo.Context) error {
msg := c.FormValue("msg")
if bc != nil && msg != "" {
errs := bc.Send(msg)
for id, err := range errs {
e.Logger.Errorf("listener: %s %s", id, err)
}
}
return c.Render(http.StatusOK, "chat_input.html", map[string]any{})
})
return nil
}
func handleSSE(c echo.Context, t echo.Renderer) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Access-Control-Allow-Origin", "*")
flusher, _ := w.(http.Flusher)
list := bc.AddListener()
defer bc.RemoveListener(list)
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
for {
select {
case msg := <-list.Chan:
err := t.Render(w, "chat_msg.html", map[string]any{
"msg": msg,
}, c)
if err != nil {
fmt.Println(err)
}
_, err = fmt.Fprintf(w, "\n\n")
if err != nil {
fmt.Println(err)
}
flusher.Flush()
case <-ticker.C:
_, err := fmt.Fprintf(w, "keepalive: \n\n")
if err != nil {
fmt.Println(err)
}
flusher.Flush()
case <-r.Context().Done():
return
}
}
}
}
func SetupStaticAssets(e *echo.Echo) {
e.Use(cMiddleware.CacheControl(0), middleware.StaticWithConfig(middleware.StaticConfig{
Root: "static",
Browse: false,
}))
}