-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreload-server.go
58 lines (50 loc) · 1.32 KB
/
reload-server.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
//go:build ignore
// This is just a test server that demonstrates using the reload
// handler.
package main
import (
"fmt"
"io"
"net/http"
"time"
"github.com/danderson/reload"
)
func main() {
rl := &reload.Reloader{}
http.HandleFunc("/", index)
http.HandleFunc("/reload", func(w http.ResponseWriter, r *http.Request) {
rl.Reload()
http.Redirect(w, r, "/reload-ui", http.StatusFound)
})
http.HandleFunc("/reload-ui", reloadUI)
http.Handle("/live", rl)
http.ListenAndServe(":8080", nil)
}
func index(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprintf(w, `<!DOCTYPE html>
<html>
<head>
<script defer type="text/javascript" src="/live"></script>
</head>
<body>
<p>At the tone, the time is %v, Coordinated Universal Time</p>
<p><a href="/reload-ui">Manual reloads</a></p>
</body>
</html>`,
time.Now().UTC().Format("03:04:05"))
}
func reloadUI(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
io.WriteString(w, `<!DOCTYPE html>
<html>
<head>
<script defer type="text/javascript" src="/live"></script>
</head>
<body>
<form action="/reload" method="get">
<input type="submit" value="Trigger reload">
</form>
</body>
</html>`)
}