From e30272733b48a3afca3c2e5e6fd87ef1a0ff9727 Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Thu, 10 Sep 2015 11:29:16 +0100 Subject: [PATCH] continuserv: Make port flag-specified --- scripts/continuserv/main.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/scripts/continuserv/main.go b/scripts/continuserv/main.go index 26dfc4a1..c5d623a4 100644 --- a/scripts/continuserv/main.go +++ b/scripts/continuserv/main.go @@ -5,6 +5,7 @@ package main import ( "bytes" + "flag" "fmt" "io/ioutil" "log" @@ -20,11 +21,17 @@ import ( fsnotify "gopkg.in/fsnotify.v1" ) -var toServe atomic.Value // Always contains valid []byte to serve. May be stale unless wg is zero. -var wg sync.WaitGroup // Indicates how many updates are pending. -var mu sync.Mutex // Prevent multiple updates in parallel. +var ( + port = flag.Int("port", 8000, "Port on which to serve HTTP") + + toServe atomic.Value // Always contains valid []byte to serve. May be stale unless wg is zero. + wg sync.WaitGroup // Indicates how many updates are pending. + mu sync.Mutex // Prevent multiple updates in parallel. +) func main() { + flag.Parse() + w, err := fsnotify.NewWatcher() if err != nil { log.Fatalf("Error making watcher: %v", err) @@ -48,9 +55,14 @@ func main() { ch := make(chan struct{}, 100) // Buffered to ensure we can multiple-increment wg for pending writes go doPopulate(ch, dir) + go watchFS(ch, w) + http.HandleFunc("/", serve) - go http.ListenAndServe(":8000", nil) + log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)) + +} +func watchFS(ch chan struct{}, w *fsnotify.Watcher) { for { select { case e := <-w.Events: