129 lines
3.7 KiB
Go
129 lines
3.7 KiB
Go
package config
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
)
|
|
|
|
// 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"`
|
|
}
|
|
|
|
// ServerConfig holds server-specific configuration
|
|
type ServerConfig struct {
|
|
Host string `json:"host"`
|
|
Port int `json:"port"`
|
|
DefaultCountryCode string `json:"default_country_code,omitempty"`
|
|
Username string `json:"username,omitempty"`
|
|
Password string `json:"password,omitempty"`
|
|
AuthKey string `json:"auth_key,omitempty"`
|
|
}
|
|
|
|
// WhatsAppConfig holds configuration for a WhatsApp account
|
|
type WhatsAppConfig struct {
|
|
ID string `json:"id"`
|
|
PhoneNumber string `json:"phone_number"`
|
|
SessionPath string `json:"session_path"`
|
|
ShowQR bool `json:"show_qr,omitempty"`
|
|
}
|
|
|
|
// Hook represents a registered webhook
|
|
type Hook struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
URL string `json:"url"`
|
|
Method string `json:"method"`
|
|
Headers map[string]string `json:"headers,omitempty"`
|
|
Active bool `json:"active"`
|
|
Events []string `json:"events,omitempty"`
|
|
Description string `json:"description,omitempty"`
|
|
}
|
|
|
|
// DatabaseConfig holds database connection information
|
|
type DatabaseConfig struct {
|
|
Type string `json:"type"`
|
|
Host string `json:"host"`
|
|
Port int `json:"port"`
|
|
Username string `json:"username"`
|
|
Password string `json:"password"`
|
|
Database string `json:"database"`
|
|
SQLitePath string `json:"sqlite_path,omitempty"` // Path to SQLite database file
|
|
}
|
|
|
|
// MediaConfig holds media storage and delivery configuration
|
|
type MediaConfig struct {
|
|
DataPath string `json:"data_path"`
|
|
Mode string `json:"mode"` // "base64", "link", or "both"
|
|
BaseURL string `json:"base_url,omitempty"` // Base URL for media links
|
|
}
|
|
|
|
// EventLoggerConfig holds event logging configuration
|
|
type EventLoggerConfig struct {
|
|
Enabled bool `json:"enabled"`
|
|
Targets []string `json:"targets"` // "file", "sqlite", "postgres"
|
|
|
|
// File-based logging
|
|
FileDir string `json:"file_dir,omitempty"` // Base directory for event files
|
|
|
|
// Database logging (uses main Database config for connection)
|
|
TableName string `json:"table_name,omitempty"` // Table name for event logs (default: "event_logs")
|
|
}
|
|
|
|
// Load reads configuration from a file
|
|
func Load(path string) (*Config, error) {
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var cfg Config
|
|
if err := json.Unmarshal(data, &cfg); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Set defaults
|
|
if cfg.LogLevel == "" {
|
|
cfg.LogLevel = "info"
|
|
}
|
|
if cfg.Server.Host == "" {
|
|
cfg.Server.Host = "localhost"
|
|
}
|
|
if cfg.Server.Port == 0 {
|
|
cfg.Server.Port = 8080
|
|
}
|
|
if cfg.Media.DataPath == "" {
|
|
cfg.Media.DataPath = "./data/media"
|
|
}
|
|
if cfg.Media.Mode == "" {
|
|
cfg.Media.Mode = "link"
|
|
}
|
|
if cfg.EventLogger.FileDir == "" {
|
|
cfg.EventLogger.FileDir = "./data/events"
|
|
}
|
|
if cfg.EventLogger.TableName == "" {
|
|
cfg.EventLogger.TableName = "event_logs"
|
|
}
|
|
if cfg.Database.SQLitePath == "" {
|
|
cfg.Database.SQLitePath = "./data/events.db"
|
|
}
|
|
|
|
return &cfg, nil
|
|
}
|
|
|
|
// Save writes configuration to a file
|
|
func Save(path string, cfg *Config) error {
|
|
data, err := json.MarshalIndent(cfg, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return os.WriteFile(path, data, 0644)
|
|
}
|