fix: mount only mounts directories if they exist

dev
Antonio De Lucreziis 2 years ago
parent b6f0b5d0a2
commit 3f491e3e8f

@ -0,0 +1,25 @@
# Notes
(new notes at the top)
## TODO: Incremental Watcher
The DAG based incremental rebuilding will require a better file watcher that can whats entire directories and automatically recheck build rules when a new file is created.
```go
type WatchListener func(path string) error
type Watcher interface {
// OnFileChange will register a file change listener
OnFileChange(l WatchListener)
// OnFileAdded will register a file creation listener
OnFileAdded(l WatchListener)
// Watch will register "path"
Watch(path string) error
// Watch will register files matching "pattern" and all directories along the way for new files
WatchPattern(pattern string) error
}
```

@ -5,6 +5,7 @@ import (
"log" "log"
"net" "net"
"net/http" "net/http"
"os"
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
@ -73,7 +74,7 @@ func serve(c serveConfig) error {
if event.Has(fsnotify.Write) { if event.Has(fsnotify.Write) {
log.Printf(`[Watcher] event: %s`, event) log.Printf(`[Watcher] event: %s`, event)
if lastRebuild.Add(50 * time.Millisecond).After(time.Now()) { if lastRebuild.Add(200 * time.Millisecond).After(time.Now()) {
log.Printf(`[Watcher] too fast, skipping`) log.Printf(`[Watcher] too fast, skipping`)
continue continue
} }
@ -144,6 +145,11 @@ func serve(c serveConfig) error {
for _, m := range c.mounts { for _, m := range c.mounts {
if strings.HasSuffix(m.route, "/") { if strings.HasSuffix(m.route, "/") {
s, _ := os.Stat(m.path)
if s == nil || !s.IsDir() {
continue
}
log.Printf(`[FileServer] mounting directory "%s" on "%s"`, m.path, m.route) log.Printf(`[FileServer] mounting directory "%s" on "%s"`, m.path, m.route)
r.Handle(m.route+"*", http.StripPrefix(m.route, http.FileServer(http.Dir(m.path)))) r.Handle(m.route+"*", http.StripPrefix(m.route, http.FileServer(http.Dir(m.path))))
} else { } else {

Loading…
Cancel
Save