-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
78 lines (64 loc) · 1.83 KB
/
main.go
File metadata and controls
78 lines (64 loc) · 1.83 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
73
74
75
76
77
78
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/gin-gonic/gin"
sdkginext "github.com/larksuite/oapi-sdk-gin"
"github.com/larksuite/oapi-sdk-go/v3/event/dispatcher"
"go.uber.org/zap"
"feishuBot/internal/conf"
"feishuBot/internal/handlers"
"feishuBot/internal/services"
"feishuBot/utils/logger"
)
func main() {
// Initialize configuration
if err := conf.InitConf(); err != nil {
logger.Fatal("Failed to initialize configuration", zap.Error(err))
}
logger.Info("Configuration initialized successfully")
// Initialize services
services.InitLark()
services.InitOpenAI()
logger.Info("Services initialized successfully")
// Set up Feishu event handler
handler := dispatcher.NewEventDispatcher(conf.GConfig.Lark.VerifyToken, "")
handler = handler.OnP2MessageReceiveV1(handlers.ReceiveMsgHandler)
// Set up gin router
gin.SetMode(gin.ReleaseMode)
g := gin.Default()
api := g.Group("/api/v1")
{
api.POST("/feishu/webhook", sdkginext.NewEventHandlerFunc(handler))
}
// Create HTTP server
srv := &http.Server{
Addr: ":8081",
Handler: g,
}
// Start server in a goroutine
go func() {
logger.Info(fmt.Sprintf("Server started successfully, listening on port: %s", srv.Addr))
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Fatal("Failed to start server", zap.Error(err))
}
}()
// Wait for interrupt signal
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
logger.Info("Shutting down server...")
// Set shutdown timeout
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// Gracefully shutdown server
if err := srv.Shutdown(ctx); err != nil {
logger.Fatal("Server shutdown error", zap.Error(err))
}
logger.Info("Server shutdown completed")
}