-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
74 lines (59 loc) · 1.85 KB
/
main.go
File metadata and controls
74 lines (59 loc) · 1.85 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
package main
import (
"api-systemd/internal/pkg/config"
"api-systemd/internal/pkg/logger"
"api-systemd/internal/router"
"context"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
)
var serverPort string
func main() {
flag.StringVar(&serverPort, "port", ":8080", "server port")
flag.Parse()
// 加载配置
cfg := config.Load()
// 如果命令行指定了端口,优先使用命令行参数
if serverPort != ":8080" {
cfg.Server.Port = serverPort
} else {
serverPort = cfg.Server.Port
}
ctx := context.Background()
logger.Info(ctx, "Starting API-Systemd server", "port", serverPort, "api_key_configured", cfg.Security.APIKey != "")
// 创建路由器
r := router.New(cfg)
// 创建HTTP服务器
server := &http.Server{
Addr: serverPort,
Handler: r,
ReadTimeout: cfg.Server.ReadTimeout,
WriteTimeout: cfg.Server.WriteTimeout,
}
// 优雅关闭处理
go func() {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
<-sigChan
logger.Info(ctx, "Shutting down server...")
shutdownCtx, cancel := context.WithTimeout(context.Background(), cfg.Server.ShutdownTimeout)
defer cancel()
if err := server.Shutdown(shutdownCtx); err != nil {
logger.Error(ctx, "Server shutdown error", "error", err)
}
}()
logger.Info(ctx, "Server started successfully", "port", serverPort)
fmt.Printf("🚀 API-Systemd Server listening on %s...\n", serverPort)
fmt.Printf("📋 API Documentation: https://github.com/rushteam/api-systemd\n")
fmt.Printf("🩺 Health check: http://localhost%s/health\n", serverPort)
fmt.Printf("🔍 Debug profiler: http://localhost%s/debug/\n", serverPort)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Error(ctx, "Server failed to start", "error", err)
panic(err)
}
logger.Info(ctx, "Server stopped gracefully")
}