-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
100 lines (84 loc) · 1.89 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"flag"
"log"
"net/http"
"os"
"path"
"path/filepath"
"github.com/howeyc/fsnotify"
"github.com/mitchellh/cli"
)
// Goroutine to watch for file changes and regenerate the site
// TODO: clean up the error handling in this function
func watch() {
log.Println("Watching for changes...")
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
err = watcher.Watch(DefaultContentDir)
if err != nil {
log.Fatal(err)
}
err = watcher.Watch(path.Join(DefaultContentDir, "posts"))
if err != nil {
log.Fatal(err)
}
err = watcher.Watch(DefaultTemplateDir)
if err != nil {
log.Fatal(err)
}
err = filepath.Walk(DefaultStaticDir, func(p string, info os.FileInfo, err error) error {
if info.IsDir() {
err = watcher.Watch(p)
if err != nil {
return err
}
}
return nil
})
if err != nil {
log.Fatal(err)
}
for {
select {
case <-watcher.Event:
log.Println("Change detected. Regenerating site...")
gc := GenerateCommand{nil}
gc.Run([]string{})
case err := <-watcher.Error:
log.Println("error:", err)
}
}
}
// ServerCommand code
type ServerCommand struct {
Ui cli.Ui
}
func (c *ServerCommand) Help() string {
helpText := `
Usage: solarwind server [options]
This will watch for changes to files and regenereate the site when those
changes are detected.
Options:
-bind ":8090"
Binds to a specific address
`
return helpText
}
func (c *ServerCommand) Synopsis() string {
return "Run a development server."
}
func (c *ServerCommand) Run(args []string) int {
var defaultBind string
flag.StringVar(&defaultBind, "bind", "localhost:8090", "Set an address to bind to")
log.Println("About to start development server")
go watch()
log.Printf("Server listening on http://%s", defaultBind)
err := http.ListenAndServe(defaultBind, http.FileServer(http.Dir(DefaultDestinationDir)))
if err != nil {
log.Fatal(err)
}
return 0
}