Files
whatshooked/pkg/handlers/handlers.go
Hein a3eca09502
Some checks failed
CI / Lint (push) Has been cancelled
CI / Build (push) Has been cancelled
CI / Test (1.23) (push) Has been cancelled
CI / Test (1.22) (push) Has been cancelled
Lint fixes and testing workflow actions
2025-12-29 10:24:18 +02:00

73 lines
1.8 KiB
Go

package handlers
import (
"encoding/json"
"net/http"
"git.warky.dev/wdevs/whatshooked/pkg/config"
"git.warky.dev/wdevs/whatshooked/pkg/hooks"
"git.warky.dev/wdevs/whatshooked/pkg/logging"
"git.warky.dev/wdevs/whatshooked/pkg/whatsapp"
)
// Handlers holds all HTTP handlers with their dependencies
type Handlers struct {
whatsappMgr *whatsapp.Manager
hookMgr *hooks.Manager
config *config.Config
configPath string
// Auth configuration
authConfig *AuthConfig
}
// AuthConfig configures authentication behavior
type AuthConfig struct {
// Validator is a custom auth validator function
// If nil, uses built-in auth (API key, basic auth)
Validator func(r *http.Request) bool
// Built-in auth settings
APIKey string
Username string
Password string
// Skip auth entirely (not recommended for production)
Disabled bool
}
// New creates a new Handlers instance
func New(mgr *whatsapp.Manager, hookMgr *hooks.Manager, cfg *config.Config, configPath string) *Handlers {
return &Handlers{
whatsappMgr: mgr,
hookMgr: hookMgr,
config: cfg,
configPath: configPath,
authConfig: &AuthConfig{
APIKey: cfg.Server.AuthKey,
Username: cfg.Server.Username,
Password: cfg.Server.Password,
},
}
}
// WithAuthConfig sets custom auth configuration
func (h *Handlers) WithAuthConfig(cfg *AuthConfig) *Handlers {
h.authConfig = cfg
return h
}
// writeJSON is a helper that writes JSON response and logs errors
func writeJSON(w http.ResponseWriter, v interface{}) {
if err := json.NewEncoder(w).Encode(v); err != nil {
logging.Error("Failed to encode JSON response", "error", err)
}
}
// writeBytes is a helper that writes bytes and logs errors
func writeBytes(w http.ResponseWriter, data []byte) {
if _, err := w.Write(data); err != nil {
logging.Error("Failed to write response", "error", err)
}
}