This commit is contained in:
2026-04-04 12:52:08 +02:00
parent 02fec72c43
commit 1e5423c4db
17 changed files with 618 additions and 21 deletions

View File

@@ -5,6 +5,7 @@ import (
"log"
"net/http"
"strconv"
"time"
"git.rouggy.com/rouggy/ShackMaster/internal/config"
"github.com/gorilla/websocket"
@@ -15,13 +16,15 @@ type Server struct {
hub *Hub
config *config.Config
upgrader websocket.Upgrader
shutdownChan chan struct{}
}
func NewServer(dm *DeviceManager, hub *Hub, cfg *config.Config) *Server {
func NewServer(dm *DeviceManager, hub *Hub, cfg *config.Config, shutdownChan chan struct{}) *Server {
return &Server{
deviceManager: dm,
hub: hub,
config: cfg,
shutdownChan: shutdownChan,
upgrader: websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
@@ -74,6 +77,9 @@ func (s *Server) SetupRoutes() *http.ServeMux {
mux.HandleFunc("/api/power/fanmode", s.handlePowerFanMode)
mux.HandleFunc("/api/power/operate", s.handlePowerOperate)
// Shutdown endpoint
mux.HandleFunc("/api/shutdown", s.handleShutdown)
// Note: Static files are now served from embedded FS in main.go
return mux
@@ -510,6 +516,21 @@ func (s *Server) handleUltrabeamDirection(w http.ResponseWriter, r *http.Request
s.sendJSON(w, map[string]string{"status": "ok"})
}
func (s *Server) handleShutdown(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
s.sendJSON(w, map[string]string{"status": "shutting down"})
go func() {
time.Sleep(200 * time.Millisecond)
log.Println("Shutdown requested via API")
close(s.shutdownChan)
}()
}
func (s *Server) sendJSON(w http.ResponseWriter, data interface{}) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(data)