|
|
@ -5,6 +5,7 @@ package main
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"bytes"
|
|
|
|
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"log"
|
|
|
@ -20,11 +21,17 @@ import (
|
|
|
|
fsnotify "gopkg.in/fsnotify.v1"
|
|
|
|
fsnotify "gopkg.in/fsnotify.v1"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var toServe atomic.Value // Always contains valid []byte to serve. May be stale unless wg is zero.
|
|
|
|
var (
|
|
|
|
var wg sync.WaitGroup // Indicates how many updates are pending.
|
|
|
|
port = flag.Int("port", 8000, "Port on which to serve HTTP")
|
|
|
|
var mu sync.Mutex // Prevent multiple updates in parallel.
|
|
|
|
|
|
|
|
|
|
|
|
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() {
|
|
|
|
func main() {
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
|
|
|
|
w, err := fsnotify.NewWatcher()
|
|
|
|
w, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error making watcher: %v", err)
|
|
|
|
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
|
|
|
|
ch := make(chan struct{}, 100) // Buffered to ensure we can multiple-increment wg for pending writes
|
|
|
|
go doPopulate(ch, dir)
|
|
|
|
go doPopulate(ch, dir)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
go watchFS(ch, w)
|
|
|
|
|
|
|
|
|
|
|
|
http.HandleFunc("/", serve)
|
|
|
|
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 {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
select {
|
|
|
|
case e := <-w.Events:
|
|
|
|
case e := <-w.Events:
|
|
|
|