-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
58 lines (48 loc) · 1.15 KB
/
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
package main
import (
"fmt"
"log"
"net/http"
"time"
"github.com/danprince/sietch/internal/builder"
"github.com/danprince/sietch/internal/errors"
"github.com/danprince/sietch/internal/livereload"
)
func serve(b *builder.Builder) {
var buildErr error
lr := livereload.New()
server := http.FileServer(http.Dir(b.OutDir))
http.Handle("/ws", lr)
http.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if buildErr != nil {
w.Header().Add("Content-type", "text/html")
w.WriteHeader(500)
w.Write([]byte(errors.Html(buildErr)))
w.Write([]byte(fmt.Sprintf("<script>%s</script>", livereload.JS)))
} else {
server.ServeHTTP(w, r)
}
}))
watcher := watch(b.PagesDir, []string{b.OutDir})
go func() {
for {
fmt.Printf("\x1bc") // clear
start := time.Now()
buildErr = b.Build()
duration := time.Since(start)
if buildErr != nil {
fmt.Println(buildErr)
} else {
fmt.Printf("built site (%s)\n", duration)
}
lr.Notify()
<-watcher
b.Reset()
}
}()
fmt.Println("serving site at http://localhost:8000...")
err := http.ListenAndServe(":8000", nil)
if err != nil {
log.Fatal(err)
}
}