-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
40 lines (31 loc) · 962 Bytes
/
main.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
package main
import (
"embed"
"fmt"
"html/template"
"log"
"net/http"
)
type Insult struct {
InsultText string
}
//go:embed index.html
var f embed.FS
func main() {
fmt.Println("Server running: http://localhost:8000")
fmt.Println("CTRL +C to stop server")
httpGetInsult := func(w http.ResponseWriter, r *http.Request) {
// alternative to read from local filesystem
// tmpl := template.Must(template.ParseFiles("index.html"))
tmpl := template.Must(template.ParseFS(f, "index.html"))
tmpl.Execute(w, Insult{InsultText: getInsult()})
}
httpGetInsultUpdate := func(w http.ResponseWriter, r *http.Request) {
// tmpl := template.Must(template.ParseFiles("index.html"))
tmpl := template.Must(template.ParseFS(f, "index.html"))
tmpl.ExecuteTemplate(w, "insult-element", Insult{InsultText: getInsult()})
}
http.HandleFunc("/", httpGetInsult)
http.HandleFunc("/get/", httpGetInsultUpdate)
log.Fatal(http.ListenAndServe(":8000", nil))
}