66 lines
1.8 KiB
Go
66 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"git.warky.dev/wdevs/whatshooked/internal/logging"
|
|
)
|
|
|
|
// setupRoutes configures all HTTP routes for the server
|
|
func (s *Server) setupRoutes() *http.ServeMux {
|
|
mux := http.NewServeMux()
|
|
|
|
// Health check (no auth required)
|
|
mux.HandleFunc("/health", s.handleHealth)
|
|
|
|
// Hook management (with auth)
|
|
mux.HandleFunc("/api/hooks", s.authMiddleware(s.handleHooks))
|
|
mux.HandleFunc("/api/hooks/add", s.authMiddleware(s.handleAddHook))
|
|
mux.HandleFunc("/api/hooks/remove", s.authMiddleware(s.handleRemoveHook))
|
|
|
|
// Account management (with auth)
|
|
mux.HandleFunc("/api/accounts", s.authMiddleware(s.handleAccounts))
|
|
mux.HandleFunc("/api/accounts/add", s.authMiddleware(s.handleAddAccount))
|
|
|
|
// Send messages (with auth)
|
|
mux.HandleFunc("/api/send", s.authMiddleware(s.handleSendMessage))
|
|
mux.HandleFunc("/api/send/image", s.authMiddleware(s.handleSendImage))
|
|
mux.HandleFunc("/api/send/video", s.authMiddleware(s.handleSendVideo))
|
|
mux.HandleFunc("/api/send/document", s.authMiddleware(s.handleSendDocument))
|
|
|
|
// Serve media files (with auth)
|
|
mux.HandleFunc("/api/media/", s.handleServeMedia)
|
|
|
|
return mux
|
|
}
|
|
|
|
// startHTTPServer starts the HTTP server for CLI communication
|
|
func (s *Server) startHTTPServer() {
|
|
mux := s.setupRoutes()
|
|
|
|
addr := fmt.Sprintf("%s:%d", s.config.Server.Host, s.config.Server.Port)
|
|
s.httpServer = &http.Server{
|
|
Addr: addr,
|
|
Handler: mux,
|
|
}
|
|
|
|
go func() {
|
|
logging.Info("Starting HTTP server",
|
|
"host", s.config.Server.Host,
|
|
"port", s.config.Server.Port,
|
|
"address", addr,
|
|
)
|
|
logging.Info("HTTP server endpoints available",
|
|
"health", "/health",
|
|
"hooks", "/api/hooks",
|
|
"accounts", "/api/accounts",
|
|
"send", "/api/send",
|
|
)
|
|
|
|
if err := s.httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
logging.Error("HTTP server error", "error", err)
|
|
}
|
|
}()
|
|
}
|