-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.go
63 lines (56 loc) · 1.18 KB
/
serve.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
59
60
61
62
63
package command
import (
"log"
"net/http"
cnst "github.com/danishprakash/kizai/constants"
"github.com/fsnotify/fsnotify"
)
func Serve() {
go watch()
http.Handle("/", http.FileServer(http.Dir(cnst.BUILD_DIR)))
http.ListenAndServe(":8000", nil)
}
func watch() {
// Create new watcher.
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
// Start listening for events.
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
log.Println("event:", event)
if event.Has(fsnotify.Write) {
log.Println("modified file:", event.Name)
Build()
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
}
}
}()
// Add a path.
// TODO: fsnotify doesn't support
// recursive watch, walk over the dirs
// and watch them
err = watcher.Add(cnst.DIR)
err = watcher.Add(cnst.DIR + "/posts")
err = watcher.Add(cnst.DIR + "/reading")
err = watcher.Add(cnst.STATIC)
err = watcher.Add(cnst.STATIC + "/css")
err = watcher.Add(cnst.TEMPLATES)
if err != nil {
log.Fatal(err)
}
// Block main goroutine forever.
<-make(chan struct{})
}