-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
53 lines (44 loc) · 1.15 KB
/
main.go
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
package main
import (
"log"
"os"
"os/signal"
"syscall"
"evo-bot-go/internal/bot"
"evo-bot-go/internal/clients"
"evo-bot-go/internal/config"
)
func main() {
// Load configuration
appConfig, err := config.LoadConfig()
if err != nil {
log.Fatalf("Failed to load configuration: %v", err)
}
// Initialize OpenAI client
openaiClient, err := clients.NewOpenAiClient()
if err != nil {
log.Fatalf("Failed to create OpenAI client: %v", err)
}
// Create and start the bot
botClient, err := bot.NewTgBotClient(openaiClient, appConfig)
if err != nil {
log.Fatalf("Failed to create Telegram Bot Client: %v", err)
}
// Set up graceful shutdown
setupGracefulShutdown(botClient)
// Start the bot
botClient.Start()
}
// setupGracefulShutdown configures signal handling for graceful application termination
func setupGracefulShutdown(botClient *bot.TgBotClient) {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
go func() {
<-sigChan
log.Println("Received shutdown signal, closing resources...")
if err := botClient.Close(); err != nil {
log.Printf("Error closing bot client: %v", err)
}
os.Exit(0)
}()
}