Newer
Older
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
package main
import (
"embed"
"html/template"
"log"
"net/http"
)
//go:embed templates/*.tpl
var templateFS embed.FS
var templates = template.Must(template.ParseFS(templateFS, "templates/*.tpl"))
//go:embed static/*
var staticFS embed.FS
func main() {
http.Handle("/static/", http.FileServer(http.FS(staticFS)))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Println("handling", r.Method, r.URL, "from", r.RemoteAddr)
if r.Method == "GET" {
w.Header().Add("content-type", "text/html")
err := templates.ExecuteTemplate(w, "index.tpl", nil)
if err != nil {
log.Println("can't execute index template:", err)
}
return
}
})
const listenAddr = "127.0.0.1:7878"
log.Printf("here we go, listening on http://%s", listenAddr)
err := http.ListenAndServe(listenAddr, nil)
if err != nil {
log.Println("http handler failed:", err)
}
}