From 1a267a1bc6ea18da4565369c31b013f0ec7436a3 Mon Sep 17 00:00:00 2001 From: Naman Sood Date: Sat, 24 Apr 2021 03:03:57 -0400 Subject: [PATCH] move listener logic into separate file Signed-off-by: Naman Sood --- listener.go | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++ server.go | 68 ------------------------------------------------ 2 files changed, 75 insertions(+), 68 deletions(-) create mode 100644 listener.go diff --git a/listener.go b/listener.go new file mode 100644 index 0000000..5dc0e4c --- /dev/null +++ b/listener.go @@ -0,0 +1,75 @@ +package main + +import ( + "log" + "os" + "runtime" + "strings" + + "github.com/rjeczalik/notify" +) + +type listener struct { + folder string + update func(string) error + clean func(string) error +} + +func (l *listener) listen() { + cwd, err := os.Getwd() + if err != nil { + log.Fatal("could not get current working directory for listener!") + } + cwd = cwd + "/" + + c := make(chan notify.EventInfo, 1) + + var events []notify.Event + + // inotify events prevent double-firing of + // certain events in Linux. + if runtime.GOOS == "linux" { + events = []notify.Event{ + notify.InCloseWrite, + notify.InMovedFrom, + notify.InMovedTo, + notify.InDelete, + } + } else { + events = []notify.Event{ + notify.Create, + notify.Remove, + notify.Rename, + notify.Write, + } + } + + err = notify.Watch(l.folder, c, events...) + + if err != nil { + log.Fatalf("Could not setup watcher for folder %s: %s", l.folder, err) + } + + defer notify.Stop(c) + + for { + ei := <-c + log.Printf("event: %s", ei.Event()) + switch ei.Event() { + case notify.InCloseWrite, notify.InMovedTo, notify.Create, notify.Rename, notify.Write: + filePath := strings.TrimPrefix(ei.Path(), cwd) + log.Printf("updating file %s", filePath) + err := l.update(strings.TrimPrefix(filePath, l.folder)) + if err != nil { + log.Printf("watcher update action on %s failed: %v", filePath, err) + } + case notify.InMovedFrom, notify.InDelete, notify.Remove: + filePath := strings.TrimPrefix(ei.Path(), cwd) + log.Printf("cleaning file %s", filePath) + err := l.clean(strings.TrimPrefix(filePath, l.folder)) + if err != nil { + log.Printf("watcher clean action on %s failed: %v", filePath, err) + } + } + } +} diff --git a/server.go b/server.go index e09db33..e1fced6 100644 --- a/server.go +++ b/server.go @@ -3,13 +3,10 @@ package main import ( "log" "net/http" - "os" - "runtime" "strings" "sync" "github.com/aymerick/raymond" - "github.com/rjeczalik/notify" ) type server struct { @@ -78,71 +75,6 @@ func newServer() (*server, error) { return s, nil } -type listener struct { - folder string - update func(string) error - clean func(string) error -} - -func (l *listener) listen() { - cwd, err := os.Getwd() - if err != nil { - log.Fatal("could not get current working directory for listener!") - } - cwd = cwd + "/" - - c := make(chan notify.EventInfo, 1) - - var events []notify.Event - - // inotify events prevent double-firing of - // certain events in Linux. - if runtime.GOOS == "linux" { - events = []notify.Event{ - notify.InCloseWrite, - notify.InMovedFrom, - notify.InMovedTo, - notify.InDelete, - } - } else { - events = []notify.Event{ - notify.Create, - notify.Remove, - notify.Rename, - notify.Write, - } - } - - err = notify.Watch(l.folder, c, events...) - - if err != nil { - log.Fatalf("Could not setup watcher for folder %s: %s", l.folder, err) - } - - defer notify.Stop(c) - - for { - ei := <-c - log.Printf("event: %s", ei.Event()) - switch ei.Event() { - case notify.InCloseWrite, notify.InMovedTo, notify.Create, notify.Rename, notify.Write: - filePath := strings.TrimPrefix(ei.Path(), cwd) - log.Printf("updating file %s", filePath) - err := l.update(strings.TrimPrefix(filePath, l.folder)) - if err != nil { - log.Printf("watcher update action on %s failed: %v", filePath, err) - } - case notify.InMovedFrom, notify.InDelete, notify.Remove: - filePath := strings.TrimPrefix(ei.Path(), cwd) - log.Printf("cleaning file %s", filePath) - err := l.clean(strings.TrimPrefix(filePath, l.folder)) - if err != nil { - log.Printf("watcher clean action on %s failed: %v", filePath, err) - } - } - } -} - func (s *server) logRequest(req *http.Request) { log.Printf("%s %s from %s", req.Method, req.URL.Path, req.RemoteAddr) }