feat(cache): 🎉 add message caching functionality
* Implement MessageCache to store events when no webhooks are available. * Add configuration options for enabling cache, setting data path, max age, and max events. * Create API endpoints for managing cached events, including listing, replaying, and deleting. * Integrate caching into the hooks manager to store events when no active webhooks are found. * Enhance logging for better traceability of cached events and operations.
This commit is contained in:
@@ -7,13 +7,14 @@ import (
|
||||
|
||||
// Config represents the application configuration
|
||||
type Config struct {
|
||||
Server ServerConfig `json:"server"`
|
||||
WhatsApp []WhatsAppConfig `json:"whatsapp"`
|
||||
Hooks []Hook `json:"hooks"`
|
||||
Database DatabaseConfig `json:"database,omitempty"`
|
||||
Media MediaConfig `json:"media"`
|
||||
EventLogger EventLoggerConfig `json:"event_logger,omitempty"`
|
||||
LogLevel string `json:"log_level"`
|
||||
Server ServerConfig `json:"server"`
|
||||
WhatsApp []WhatsAppConfig `json:"whatsapp"`
|
||||
Hooks []Hook `json:"hooks"`
|
||||
Database DatabaseConfig `json:"database,omitempty"`
|
||||
Media MediaConfig `json:"media"`
|
||||
EventLogger EventLoggerConfig `json:"event_logger,omitempty"`
|
||||
MessageCache MessageCacheConfig `json:"message_cache,omitempty"`
|
||||
LogLevel string `json:"log_level"`
|
||||
}
|
||||
|
||||
// ServerConfig holds server-specific configuration
|
||||
@@ -122,6 +123,14 @@ type MQTTConfig struct {
|
||||
Subscribe bool `json:"subscribe,omitempty"` // Enable subscription for sending messages
|
||||
}
|
||||
|
||||
// MessageCacheConfig holds message cache configuration
|
||||
type MessageCacheConfig struct {
|
||||
Enabled bool `json:"enabled"` // Enable message caching
|
||||
DataPath string `json:"data_path,omitempty"` // Directory to store cached events
|
||||
MaxAgeDays int `json:"max_age_days,omitempty"` // Maximum age in days before purging (default: 7)
|
||||
MaxEvents int `json:"max_events,omitempty"` // Maximum number of events to cache (default: 10000)
|
||||
}
|
||||
|
||||
// Load reads configuration from a file
|
||||
func Load(path string) (*Config, error) {
|
||||
data, err := os.ReadFile(path)
|
||||
@@ -186,6 +195,17 @@ func Load(path string) (*Config, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// Set message cache defaults
|
||||
if cfg.MessageCache.DataPath == "" {
|
||||
cfg.MessageCache.DataPath = "./data/message_cache"
|
||||
}
|
||||
if cfg.MessageCache.MaxAgeDays == 0 {
|
||||
cfg.MessageCache.MaxAgeDays = 7
|
||||
}
|
||||
if cfg.MessageCache.MaxEvents == 0 {
|
||||
cfg.MessageCache.MaxEvents = 10000
|
||||
}
|
||||
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user