-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
63 lines (51 loc) · 1.41 KB
/
Copy pathmain.go
File metadata and controls
63 lines (51 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/mdshack/ipmi-api/pkg/server"
)
func main() {
// Create server instance
srv := server.New()
// Create HTTP server
httpServer := &http.Server{
Addr: fmt.Sprintf("%s:%s", getConfig("IPMI_API_HOST", "0.0.0.0"), getConfig("IPMI_API_PORT", "8080")),
Handler: srv.Router,
}
// Channel to listen for interrupt signal
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
// Run server in a goroutine
go func() {
log.Printf("Starting server on %s", httpServer.Addr)
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Could not listen on %s: %v", httpServer.Addr, err)
}
}()
// Wait for interrupt signal
<-quit
log.Println("Shutting down server...")
// Create a context with timeout for graceful shutdown
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
// Attempt graceful shutdown
if err := httpServer.Shutdown(ctx); err != nil {
log.Fatalf("Server forced to shutdown: %v", err)
}
// Close all IPMI connections
srv.Close()
log.Println("Server exited")
}
// getConfig retrieves configuration from environment variables with a default value
func getConfig(key, defaultValue string) string {
if value := os.Getenv(key); value != "" {
return value
}
return defaultValue
}