continuserv: Make port flag-specified
This commit is contained in:
parent
c4acee3bcb
commit
e30272733b
1 changed files with 17 additions and 5 deletions
|
@ -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)
|
||||||
|
|
||||||
http.HandleFunc("/", serve)
|
go watchFS(ch, w)
|
||||||
go http.ListenAndServe(":8000", nil)
|
|
||||||
|
|
||||||
|
http.HandleFunc("/", serve)
|
||||||
|
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:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue