92 lines
2.5 KiB
Go
92 lines
2.5 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 listen port")
|
|
dev := flag.Bool("dev", false, "serve from filesystem instead of embedded assets")
|
|
llmProvider := flag.String("llm-provider", "", "LLM provider: anthropic|openai|gemini|ollama|none")
|
|
llmModel := flag.String("llm-model", "", "model name (overrides config file)")
|
|
llmEndpoint := flag.String("llm-endpoint", "", "API endpoint (e.g. http://localhost:11434 for Ollama)")
|
|
|
|
flag.Usage = func() {
|
|
fmt.Fprintf(os.Stderr, "HeatGuard — personalized heat preparedness server\n\n")
|
|
fmt.Fprintf(os.Stderr, "Usage:\n heatguard [flags]\n\n")
|
|
fmt.Fprintf(os.Stderr, "Flags:\n")
|
|
flag.PrintDefaults()
|
|
fmt.Fprintf(os.Stderr, "\nExamples:\n")
|
|
fmt.Fprintf(os.Stderr, " heatguard # default, port 8080, no LLM\n")
|
|
fmt.Fprintf(os.Stderr, " heatguard -port 3000 # custom port\n")
|
|
fmt.Fprintf(os.Stderr, " heatguard -llm-provider ollama -llm-model llama3.2 # local Ollama\n")
|
|
fmt.Fprintf(os.Stderr, " heatguard -dev # development mode\n")
|
|
}
|
|
flag.Parse()
|
|
|
|
cfg := config.Load()
|
|
|
|
if *llmProvider != "" {
|
|
cfg.LLM.Provider = *llmProvider
|
|
}
|
|
if *llmModel != "" {
|
|
cfg.LLM.Model = *llmModel
|
|
}
|
|
if *llmEndpoint != "" {
|
|
cfg.LLM.Endpoint = *llmEndpoint
|
|
}
|
|
|
|
// 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")
|
|
}
|