-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnginx-reloader.go
67 lines (53 loc) · 1.33 KB
/
nginx-reloader.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
package main
import (
"errors"
"fmt"
"github.com/trpx/nginx-reloader/utils"
"os"
"time"
)
// CLI
func main() {
pollCooldown, watchedDirs, nginxCommand, err := utils.ParseOptions(os.Args)
if err != nil {
utils.Fatalf("%v", err)
}
err = StartNginxReloader(pollCooldown, watchedDirs, nginxCommand)
if err != nil {
utils.Fatalf("%v", err)
}
}
// Programmatic API
func StartNginxReloader(pollCooldown time.Duration, watchedDirs []string, nginxCommand []string) (err error) {
defer func() {
if r := recover(); r != nil {
err = errors.New(fmt.Sprintf("%v", r))
}
}()
validateWatchedDirs(watchedDirs)
watcher := utils.MakeDirWatcher(watchedDirs, pollCooldown)
watcher.CalcChecksum()
NginxRunner := utils.MakeNginxRunner(watcher.ChangeChan, nginxCommand)
cmd := NginxRunner.StartNginx()
watcher.Watch()
err = cmd.Wait()
if err != nil {
utils.Panicf("nginx process encountered an error during execution:\n%v\n", err)
}
return err
}
func validateWatchedDirs(watchedDirs []string) {
for _, el := range watchedDirs {
stat, err := os.Stat(el)
if err != nil {
if os.IsNotExist(err) {
utils.Panicf("watched directory '%v' does not exist", el)
} else {
utils.Panicf("couldn't stat watched directory '%v'", el)
}
}
if !stat.IsDir() {
utils.Panicf("watched path '%v' is not a directory", el)
}
}
}