mirror of
https://github.com/Theodor-Springmann-Stiftung/kgpz_web.git
synced 2025-10-29 00:55:32 +00:00
basic reload
This commit is contained in:
75
helpers/watcher.go
Normal file
75
helpers/watcher.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
)
|
||||
|
||||
type IFileWatcher interface {
|
||||
GetEvents() chan string
|
||||
}
|
||||
|
||||
type FileWatcher struct {
|
||||
path []string
|
||||
events chan string
|
||||
watcher *fsnotify.Watcher
|
||||
}
|
||||
|
||||
func NewFileWatcher(path []string) (*FileWatcher, error) {
|
||||
fw := &FileWatcher{path: path, events: make(chan string, 48)}
|
||||
err := fw.Watch(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return fw, nil
|
||||
}
|
||||
|
||||
func (fw *FileWatcher) Watch(paths []string) error {
|
||||
fw.events = make(chan string, 48)
|
||||
|
||||
watcher, err := fsnotify.NewWatcher()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fw.watcher = watcher
|
||||
|
||||
// Start listening for events.
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case event, ok := <-watcher.Events:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
log.Println("event:", event)
|
||||
if !event.Has(fsnotify.Chmod) {
|
||||
fw.events <- event.Name
|
||||
}
|
||||
case err, ok := <-watcher.Errors:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
log.Println("error:", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
for _, path := range paths {
|
||||
err = watcher.Add(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (fw *FileWatcher) GetEvents() chan string {
|
||||
return fw.events
|
||||
}
|
||||
|
||||
func (fw *FileWatcher) Close() {
|
||||
fw.watcher.Close()
|
||||
close(fw.events)
|
||||
}
|
||||
Reference in New Issue
Block a user