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
|
package main
import (
"fmt"
"net/http"
)
func main() {
fmt.Println("foo")
mux := http.NewServeMux()
fileServer := http.FileServer(http.Dir("./public"))
mux.Handle("GET /public/", http.StripPrefix("/public/", fileServer))
mux.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) {
HomePage().Render(r.Context(), w)
})
mux.HandleFunc("GET /about", func(w http.ResponseWriter, r *http.Request) {
AboutPage().Render(r.Context(), w)
})
mux.HandleFunc("GET /items", func(w http.ResponseWriter, r *http.Request) {
data := []Item{
{Name: "Foo", Active: true},
{Name: "Bar", Active: true},
{Name: "Baz", Active: false},
}
ItemsPage(data).Render(r.Context(), w)
})
fmt.Println("Server running at http://localhost:8080")
if err := http.ListenAndServe(":8080", mux); err != nil {
panic(err)
}
}
|