Add health endpoints (/healthz, /readyz), graceful shutdown with SIGTERM/SIGINT handling, multi-stage Dockerfile with distroless runtime, and a full Helm chart with security-hardened defaults.
67 lines
1.4 KiB
Go
67 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/cnachtigall/heatwave-autopilot/internal/config"
|
|
"github.com/cnachtigall/heatwave-autopilot/internal/server"
|
|
"github.com/cnachtigall/heatwave-autopilot/web"
|
|
)
|
|
|
|
func main() {
|
|
port := flag.Int("port", 8080, "HTTP port")
|
|
dev := flag.Bool("dev", false, "development mode (serve from filesystem)")
|
|
flag.Parse()
|
|
|
|
cfg := config.Load()
|
|
|
|
// Set the embedded filesystem for the server package
|
|
server.WebFS = web.FS
|
|
|
|
srv, err := server.New(server.Options{
|
|
Port: *port,
|
|
DevMode: *dev,
|
|
Config: cfg,
|
|
})
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
addr := fmt.Sprintf(":%d", *port)
|
|
httpServer := &http.Server{
|
|
Addr: addr,
|
|
Handler: srv.Handler(),
|
|
}
|
|
|
|
// Graceful shutdown on SIGTERM/SIGINT
|
|
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
|
|
defer stop()
|
|
|
|
go func() {
|
|
log.Printf("HeatGuard listening on http://localhost%s", addr)
|
|
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
log.Fatalf("listen: %v", err)
|
|
}
|
|
}()
|
|
|
|
<-ctx.Done()
|
|
log.Println("Shutting down...")
|
|
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
|
|
if err := httpServer.Shutdown(shutdownCtx); err != nil {
|
|
log.Fatalf("shutdown: %v", err)
|
|
}
|
|
log.Println("Shutdown complete")
|
|
}
|