Documentation
¶
Index ¶
- Variables
- func CollectFileChangeEvents(watcher *Watcher, mut *sync.Mutex, events TimeEventMap, maxAge time.Duration, ...)
- func EventServer(path, allowed, eventAddr, eventPath string, refreshDuration time.Duration, ...)
- func Flush(w http.ResponseWriter) bool
- func GenFileChangeEvents(events TimeEventMap, mut *sync.Mutex, maxAge time.Duration, allowed string) http.HandlerFunc
- 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 EventBatcher
- type EventFilter
- func (f *EventFilter) SetExcludePatterns(patterns string)
- func (f *EventFilter) SetIgnoreEvents(events string)
- func (f *EventFilter) SetIncludeEvents(events string)
- func (f *EventFilter) SetIncludePatterns(patterns string)
- func (f *EventFilter) ShouldIncludePath(path string) bool
- func (f *EventFilter) ShouldProcessEvent(event fsnotify.Event) bool
- type EventStreamer
- type FilterOption
- type MultiStreamer
- type Option
- func WithFilter(filter *EventFilter) Option
- func WithStreamMethod(method StreamMethod) Option
- func WithWebhook(url, method string, headers map[string]string, ...) Option
- func WithWebhookDebounce(duration time.Duration) Option
- func WithWebhookHeaders(headers map[string]string) Option
- func WithWebhookRetries(retries int) Option
- func WithWebhookTimeout(timeout time.Duration) Option
- type Options
- type RecursiveWatcher
- type SSEStreamer
- type StreamMethod
- type StreamerOptions
- type TimeEventMap
- type Watcher
- type WatcherConfig
- type WebSocketClient
- type WebSocketStreamer
- type WebhookConfig
- type WebhookManager
- type WebhookPayload
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 *Watcher, mut *sync.Mutex, events TimeEventMap, maxAge time.Duration, filter *EventFilter, webhookManager *WebhookManager)
CollectFileChangeEvents collects file change events from the watcher
func EventServer ¶
func EventServer(path, allowed, eventAddr, eventPath string, refreshDuration time.Duration, options ...Option)
EventServer starts a server that serves events over SSE and/or WebSockets
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
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 or directory should be ignored based on its name. This is a performance-critical function as it's called for every file and directory. Performance improvement: Using a pre-defined list of prefixes to check against.
func Subfolders ¶
Subfolders returns a list of all subdirectories in the given path. It follows symbolic links and ignores directories that match the ignore criteria. Performance improvements: 1. Pre-allocates the paths slice with a reasonable capacity 2. Uses a concurrent approach with worker pools for large directory structures 3. Implements early termination for ignored directories
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 EventBatcher ¶ added in v0.4.0
type EventBatcher struct {
// contains filtered or unexported fields
}
EventBatcher batches file system events to reduce redundant processing
func NewEventBatcher ¶ added in v0.4.0
func NewEventBatcher(handlerDelay time.Duration) *EventBatcher
NewEventBatcher creates a new event batcher
func (*EventBatcher) Add ¶ added in v0.4.0
func (b *EventBatcher) Add(event fsnotify.Event)
Add adds an event to the batch
func (*EventBatcher) AddBatch ¶ added in v0.4.0
func (b *EventBatcher) AddBatch(events []fsnotify.Event)
AddBatch adds multiple events to the batch
func (*EventBatcher) Close ¶ added in v0.4.0
func (b *EventBatcher) Close()
Close closes the event batcher
func (*EventBatcher) Events ¶ added in v0.4.0
func (b *EventBatcher) Events() <-chan []fsnotify.Event
Events returns the channel that receives batched events
type EventFilter ¶ added in v0.3.0
type EventFilter struct {
// contains filtered or unexported fields
}
EventFilter provides filtering capabilities for file system events
func NewEventFilter ¶ added in v0.3.0
func NewEventFilter() *EventFilter
NewEventFilter creates a new event filter
func (*EventFilter) SetExcludePatterns ¶ added in v0.3.0
func (f *EventFilter) SetExcludePatterns(patterns string)
SetExcludePatterns sets the patterns for files to exclude
func (*EventFilter) SetIgnoreEvents ¶ added in v0.3.0
func (f *EventFilter) SetIgnoreEvents(events string)
SetIgnoreEvents sets the event types to ignore
func (*EventFilter) SetIncludeEvents ¶ added in v0.3.0
func (f *EventFilter) SetIncludeEvents(events string)
SetIncludeEvents sets the event types to include
func (*EventFilter) SetIncludePatterns ¶ added in v0.3.0
func (f *EventFilter) SetIncludePatterns(patterns string)
SetIncludePatterns sets the patterns for files to include
func (*EventFilter) ShouldIncludePath ¶ added in v0.4.0
func (f *EventFilter) ShouldIncludePath(path string) bool
ShouldIncludePath checks if a path should be included based on patterns
func (*EventFilter) ShouldProcessEvent ¶ added in v0.4.0
func (f *EventFilter) ShouldProcessEvent(event fsnotify.Event) bool
ShouldProcessEvent checks if an event should be processed based on event type and path
type EventStreamer ¶ added in v0.4.0
type EventStreamer interface {
// Start initializes and starts the streamer
Start(ctx context.Context) error
// Stop gracefully shuts down the streamer
Stop() error
// Send delivers an event to all connected clients
Send(event fsnotify.Event) error
}
EventStreamer is an interface for streaming events to clients
type FilterOption ¶ added in v0.3.0
type FilterOption func(*EventFilter)
FilterOption is a function that configures an EventFilter
func WithExcludePatterns ¶ added in v0.3.0
func WithExcludePatterns(patterns string) FilterOption
WithExcludePatterns creates a FilterOption that sets the exclude patterns
func WithIgnoreEvents ¶ added in v0.3.0
func WithIgnoreEvents(events string) FilterOption
WithIgnoreEvents creates a FilterOption that sets the ignore event types
func WithIncludeEvents ¶ added in v0.3.0
func WithIncludeEvents(events string) FilterOption
WithIncludeEvents creates a FilterOption that sets the include event types
func WithIncludePatterns ¶ added in v0.3.0
func WithIncludePatterns(patterns string) FilterOption
WithIncludePatterns creates a FilterOption that sets the include patterns
type MultiStreamer ¶ added in v0.4.0
type MultiStreamer struct {
// contains filtered or unexported fields
}
MultiStreamer combines multiple EventStreamers
func NewMultiStreamer ¶ added in v0.4.0
func NewMultiStreamer(streamers ...EventStreamer) *MultiStreamer
NewMultiStreamer creates a new multi-streamer
func (*MultiStreamer) Send ¶ added in v0.4.0
func (m *MultiStreamer) Send(event fsnotify.Event) error
Send delivers an event to all streamers
func (*MultiStreamer) Start ¶ added in v0.4.0
func (m *MultiStreamer) Start(ctx context.Context) error
Start initializes and starts all streamers
func (*MultiStreamer) Stop ¶ added in v0.4.0
func (m *MultiStreamer) Stop() error
Stop gracefully shuts down all streamers
type Option ¶ added in v0.3.0
type Option func(*Options)
Option is a function that configures Options
func WithFilter ¶ added in v0.3.0
func WithFilter(filter *EventFilter) Option
WithFilter creates an Option that sets the event filter
func WithStreamMethod ¶ added in v0.4.0
func WithStreamMethod(method StreamMethod) Option
WithStreamMethod creates an Option that sets the stream method
func WithWebhook ¶ added in v0.3.0
func WithWebhook(url, method string, headers map[string]string, timeout, debounceDuration time.Duration, maxRetries int) Option
WithWebhook creates an Option that sets the webhook URL and method
func WithWebhookDebounce ¶ added in v0.3.0
WithWebhookDebounce creates an Option that sets the webhook debounce duration
func WithWebhookHeaders ¶ added in v0.3.0
WithWebhookHeaders creates an Option that sets the webhook headers
func WithWebhookRetries ¶ added in v0.3.0
WithWebhookRetries creates an Option that sets the webhook max retries
func WithWebhookTimeout ¶ added in v0.3.0
WithWebhookTimeout creates an Option that sets the webhook timeout
type Options ¶ added in v0.3.0
type Options struct {
// Filter to apply to events
Filter *EventFilter
// Webhook URL to send events to
WebhookURL string
// HTTP method to use for webhooks
WebhookMethod string
// Headers to include in webhook requests
WebhookHeaders map[string]string
// Timeout for webhook requests
WebhookTimeout time.Duration
// Debounce duration for webhooks
WebhookDebounceDuration time.Duration
// Maximum number of retries for webhook requests
WebhookMaxRetries int
// Stream method to use
StreamMethod StreamMethod
}
Options contains all options for the EventServer
type RecursiveWatcher ¶
type RecursiveWatcher struct {
*fsnotify.Watcher
Files chan string // Channel for file events
Folders chan string // Channel for folder events
// contains filtered or unexported fields
}
RecursiveWatcher keeps the data for watching files and directories. It embeds fsnotify.Watcher and adds channels for tracking files and folders.
func NewRecursiveWatcher ¶
func NewRecursiveWatcher(path string) (*RecursiveWatcher, error)
NewRecursiveWatcher creates a new RecursiveWatcher. Takes a path to a directory to watch recursively. Performance improvements: 1. Uses a worker pool for adding folders in parallel 2. Pre-allocates the paths slice with a reasonable capacity 3. Uses a mutex for thread-safe operations
func (*RecursiveWatcher) AddFolder ¶
func (watcher *RecursiveWatcher) AddFolder(folder string) error
AddFolder adds a directory to watch, non-recursively. It's thread-safe due to the mutex lock.
func (*RecursiveWatcher) Close ¶
func (watcher *RecursiveWatcher) Close() error
Close properly closes the watcher and its channels. This method should be called when the watcher is no longer needed to prevent resource leaks.
type SSEStreamer ¶ added in v0.4.0
type SSEStreamer struct {
// contains filtered or unexported fields
}
SSEStreamer implements EventStreamer using Server-Sent Events
func NewSSEStreamer ¶ added in v0.4.0
func NewSSEStreamer(opts StreamerOptions) *SSEStreamer
NewSSEStreamer creates a new SSE streamer
func (*SSEStreamer) Send ¶ added in v0.4.0
func (s *SSEStreamer) Send(event fsnotify.Event) error
Send delivers an event to all connected clients
func (*SSEStreamer) Start ¶ added in v0.4.0
func (s *SSEStreamer) Start(ctx context.Context) error
Start initializes and starts the SSE streamer
func (*SSEStreamer) Stop ¶ added in v0.4.0
func (s *SSEStreamer) Stop() error
Stop gracefully shuts down the SSE streamer
type StreamMethod ¶ added in v0.4.0
type StreamMethod string
StreamMethod defines the method used for streaming events
const ( // StreamMethodSSE uses Server-Sent Events for streaming StreamMethodSSE StreamMethod = "sse" // StreamMethodWebSocket uses WebSockets for streaming StreamMethodWebSocket StreamMethod = "websocket" // StreamMethodBoth uses both SSE and WebSockets for streaming StreamMethodBoth StreamMethod = "both" )
type StreamerOptions ¶ added in v0.4.0
type StreamerOptions struct {
// Address to listen on ([host][:port])
Address string
// Path for the event stream
Path string
// AllowedOrigin for CORS (Access-Control-Allow-Origin)
AllowedOrigin string
// RefreshDuration for SSE events
RefreshDuration time.Duration
// Filter for events
Filter *EventFilter
}
StreamerOptions contains configuration for event streamers
type Watcher ¶ added in v0.4.0
type Watcher struct {
// contains filtered or unexported fields
}
Watcher provides improved file watching capabilities
func NewWatcher ¶ added in v0.4.0
func NewWatcher(config WatcherConfig) (*Watcher, error)
NewWatcher creates a new file watcher
type WatcherConfig ¶ added in v0.4.0
type WatcherConfig struct {
// Root directory to watch
RootPath string
// Patterns to include/exclude
IncludePatterns []string
ExcludePatterns []string
// Event types to include/ignore
IncludeEvents []string
IgnoreEvents []string
// Whether to watch recursively
Recursive bool
// Delay before handling events (for batching)
HandlerDelay time.Duration
// Polling interval for checking new files
PollInterval time.Duration
}
WatcherConfig holds configuration for the watcher
type WebSocketClient ¶ added in v0.4.0
WebSocketClient represents a connected WebSocket client
type WebSocketStreamer ¶ added in v0.4.0
type WebSocketStreamer struct {
// contains filtered or unexported fields
}
WebSocketStreamer implements EventStreamer using WebSockets
func NewWebSocketStreamer ¶ added in v0.4.0
func NewWebSocketStreamer(opts StreamerOptions) *WebSocketStreamer
NewWebSocketStreamer creates a new WebSocket streamer
func (*WebSocketStreamer) Send ¶ added in v0.4.0
func (ws *WebSocketStreamer) Send(event fsnotify.Event) error
Send delivers an event to all connected clients
func (*WebSocketStreamer) Start ¶ added in v0.4.0
func (ws *WebSocketStreamer) Start(ctx context.Context) error
Start initializes and starts the WebSocket streamer
func (*WebSocketStreamer) Stop ¶ added in v0.4.0
func (ws *WebSocketStreamer) Stop() error
Stop gracefully shuts down the WebSocket streamer
type WebhookConfig ¶ added in v0.3.0
type WebhookConfig struct {
// URL to send the webhook to
URL string
// HTTP method to use (GET, POST, PUT, etc.)
Method string
// Headers to include in the request
Headers map[string]string
// Timeout for the HTTP request
Timeout time.Duration
// Debounce duration to avoid sending too many webhooks
DebounceDuration time.Duration
// Maximum number of retries for failed requests
MaxRetries int
}
WebhookConfig defines the configuration for a webhook
type WebhookManager ¶ added in v0.3.0
type WebhookManager struct {
// Configuration for the webhook
Config WebhookConfig
// contains filtered or unexported fields
}
WebhookManager manages webhooks for file system events
func NewWebhookManager ¶ added in v0.3.0
func NewWebhookManager(config WebhookConfig) *WebhookManager
NewWebhookManager creates a new webhook manager
func (*WebhookManager) HandleEvent ¶ added in v0.3.0
func (m *WebhookManager) HandleEvent(event fsnotify.Event)
HandleEvent handles a file system event
type WebhookPayload ¶ added in v0.3.0
type WebhookPayload struct {
// Path of the file that changed
Path string `json:"path"`
// Type of event (create, write, remove, rename, chmod)
EventType string `json:"event_type"`
// Time the event occurred
Time time.Time `json:"time"`
}
WebhookPayload is the JSON payload sent to the webhook URL