Documentation
¶
Overview ¶
Package recwatch provides a way to watch directories recursively, using fsnotify
Index ¶
- Variables
- func CollectFileChangeEvents(watcher *RecursiveWatcher, mut *sync.Mutex, events TimeEventMap, ...)
- func EventHandler(path, allowed, eventPath string, refreshDuration time.Duration) (http.Handler, error)
- func EventServer(path, allowed, eventAddr, eventPath string, refreshDuration time.Duration)
- func EventServerHandler(path, allowed, eventPath string, refreshDuration time.Duration) (http.Handler, error)
- func Flush(w http.ResponseWriter) bool
- func GenFileChangeEvents(events TimeEventMap, mut *sync.Mutex, maxAge time.Duration, allowed string) http.HandlerFunc
- func GenRelFileChangeEvents(events TimeEventMap, mut *sync.Mutex, maxAge time.Duration, ...) http.HandlerFunc
- func RelEventHandler(path, allowed, eventPath string, refreshDuration time.Duration) (http.Handler, error)
- func RemoveOldEvents(events *TimeEventMap, maxAge time.Duration)
- func SetVerbose(enabled bool)
- func ShouldIgnoreFile(name string) bool
- func Subfolders(path string) (paths []string)
- func WriteEvent(w http.ResponseWriter, id *uint64, message string, flush bool)
- type Event
- type RecursiveWatcher
- type TimeEventMap
Constants ¶
This section is empty.
Variables ¶
Exists checks if the given path exists, using os.Stat
var FatalExit = func(err error) { log.Fatalln(err) }
FatalExit ends the program after logging a message
var LogError = func(err error) { log.Println(err.Error()) }
LogError logs a message as an error, but does not end the program
var LogInfo = func(msg string) { log.Println(msg) }
LogInfo logs a message as information
Functions ¶
func CollectFileChangeEvents ¶
func CollectFileChangeEvents(watcher *RecursiveWatcher, mut *sync.Mutex, events TimeEventMap, maxAge time.Duration)
CollectFileChangeEvents gathers filesystem events in a way that web handle functions can use
func EventHandler ¶ added in v1.2.1
func EventHandler(path, allowed, eventPath string, refreshDuration time.Duration) (http.Handler, error)
EventHandler returns an http.Handler that serves SSE file-change events for the given path. Unlike EventServer, it does not start its own http.Server — the caller can mount the handler on any mux. Event data contains absolute file paths (same as EventServer).
func EventServer ¶
EventServer serves events on a dedicated port. addr is the host address ([host][:port]) The filesystem events are gathered independently of that. Allowed can be "*" or a hostname and sets a header in the SSE stream.
func EventServerHandler ¶ added in v1.2.1
func EventServerHandler(path, allowed, eventPath string, refreshDuration time.Duration) (http.Handler, error)
EventServerHandler is like EventServer but returns the http.Handler instead of starting a server. It also emits relative paths. This is the recommended replacement for EventServer when embedding the SSE endpoint into an existing server.
func Flush ¶
func Flush(w http.ResponseWriter) bool
Flush can flush the given ResponseWriter. Returns false if it wasn't an http.Flusher.
func GenFileChangeEvents ¶
func GenFileChangeEvents(events TimeEventMap, mut *sync.Mutex, maxAge time.Duration, allowed string) http.HandlerFunc
GenFileChangeEvents creates an SSE event whenever a file in the server directory changes.
Uses the following HTTP headers:
Content-Type: text/event-stream;charset=utf-8 Cache-Control: no-cache Connection: keep-alive Access-Control-Allow-Origin: (custom value)
The "Access-Control-Allow-Origin" header uses the value that is passed in the "allowed" argument.
func GenRelFileChangeEvents ¶ added in v1.2.1
func GenRelFileChangeEvents(events TimeEventMap, mut *sync.Mutex, maxAge time.Duration, allowed, watchDir string) http.HandlerFunc
GenRelFileChangeEvents is like GenFileChangeEvents but emits paths relative to watchDir instead of absolute filesystem paths.
func RelEventHandler ¶ added in v1.2.1
func RelEventHandler(path, allowed, eventPath string, refreshDuration time.Duration) (http.Handler, error)
RelEventHandler is like EventHandler but emits paths relative to the watched directory instead of absolute filesystem paths.
func RemoveOldEvents ¶
func RemoveOldEvents(events *TimeEventMap, maxAge time.Duration)
RemoveOldEvents can remove old filesystem events, after a certain duration. Needs to be called within a mutex!
func SetVerbose ¶
func SetVerbose(enabled bool)
SetVerbose can be used to enable or disable logging of incoming events
func ShouldIgnoreFile ¶
ShouldIgnoreFile determines if a file should be ignored. File names that begin with "." or "_" are ignored by the go tool.
func Subfolders ¶
Subfolders returns a slice of subfolders (recursive), including the provided path
func WriteEvent ¶
func WriteEvent(w http.ResponseWriter, id *uint64, message string, flush bool)
WriteEvent writes SSE events to the given ResponseWriter. id can be nil.
Types ¶
type Event ¶
Event is a custom type, so that packages that depends on recwatch does not also have to import fstnotify
type RecursiveWatcher ¶
RecursiveWatcher keeps the data for watching files and directories
func NewRecursiveWatcher ¶
func NewRecursiveWatcher(path string) (*RecursiveWatcher, error)
NewRecursiveWatcher creates a new RecursiveWatcher. Takes a path to a directory to watch.
func (*RecursiveWatcher) AddFolder ¶
func (watcher *RecursiveWatcher) AddFolder(folder string) error
AddFolder adds a directory to watch, non-recursively