-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.go
More file actions
42 lines (34 loc) · 787 Bytes
/
router.go
File metadata and controls
42 lines (34 loc) · 787 Bytes
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
package main
import (
"net/http"
"text/template"
)
type Router struct {
mux *http.ServeMux
}
func NewRouter() *Router {
return &Router{
mux: http.NewServeMux(),
}
}
func (r *Router) ListenAndServe(address string) error {
return http.ListenAndServe(address, r.mux)
}
type ViewsHandler struct {
Index *template.Template
}
func NewTemplatesHandler() *ViewsHandler {
return &ViewsHandler{
Index: template.Must(template.New("index").Delims("[[", "]]").ParseFiles("client/dist/index.html")),
}
}
type IndexParams struct {
Name string
}
func (h *ViewsHandler) IndexView(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
h.Index.ExecuteTemplate(w, "index.html", IndexParams{Name: "Go Astro"})
return
}
http.ServeFile(w, r, "client/dist"+r.URL.Path)
}