-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.go
More file actions
72 lines (57 loc) · 1.84 KB
/
Copy pathmain.go
File metadata and controls
72 lines (57 loc) · 1.84 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
64
65
66
67
68
69
70
71
72
package main
import (
"context"
"log"
"os"
"os/signal"
"syscall"
"time"
"openchamp/server/config"
"openchamp/server/portmanager"
"openchamp/server/webapi"
"openchamp/server/wsm"
"github.com/joho/godotenv"
)
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
print(os.Getenv("DB_USER"))
log.Println("Starting OpenChamp server...")
/* === Database Config === */
DBPool, err := config.InitDBPool()
if err != nil {
panic("Failed to initialize database pool: " + err.Error())
}
if err := config.SetupDatabase(DBPool); err != nil {
log.Printf("Warning: Database setup issue: %v", err)
}
defer DBPool.Close()
/* === WebSocket and Web Server Setup === */
portmanager.InitPortManager(config.BaseGamePort, config.MaxGamePort)
WSClientManager := wsm.NewClientManager()
WebServer := webapi.NewServer(WSClientManager, DBPool, config.ListenAddr)
// Start the HTTP server with graceful shutdown
go webapi.StartServer(WebServer)
go WSClientManager.Start()
go portmanager.Manager.StartQueueProcessor()
log.Printf("Server is running on %s", config.ListenAddr)
log.Printf("Game server ports range: %d-%d", config.BaseGamePort, config.MaxGamePort)
// Set up signal handling
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
// Wait for termination signal
sig := <-sigChan
log.Printf("Received signal %v, initiating graceful shutdown...", sig)
// Create a timeout context for shutdown
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 30*time.Second)
defer shutdownCancel()
// Initiate graceful shutdown
if err := WebServer.Shutdown(shutdownCtx); err != nil {
log.Printf("Error during server shutdown: %v", err)
}
// Close all active WebSocket connections
WSClientManager.Shutdown()
log.Println("Server shutdown complete")
}