-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
62 lines (50 loc) · 1.05 KB
/
main.go
File metadata and controls
62 lines (50 loc) · 1.05 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
package main
import (
"log"
"time"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"zeusro.com/shit/api"
"zeusro.com/shit/db"
)
// 中间件:日志 + traceID
func LoggerWithTraceID() gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
traceID := uuid.New().String()
c.Set("traceID", traceID)
// 继续执行
c.Next()
// 记录日志
duration := time.Since(start)
log.Printf("[TraceID:%s] %s %s %d %s",
traceID,
c.Request.Method,
c.Request.URL.Path,
c.Writer.Status(),
duration,
)
}
}
func main() {
// 初始化 ClickHouse
for i := 0; i < 2; i++ {
err := db.InitClickHouse()
if err != nil {
time.Sleep(time.Second * 3)
} else {
break
}
}
// 设置 ClickHouse 连接到 api 包
api.SetClickHouseConn(db.Conn)
r := gin.Default()
// 加载静态文件 index.html
r.StaticFile("/", "./static/index.html")
// 使用自定义日志+traceID中间件
r.Use(LoggerWithTraceID())
// 注册所有路由
api.RegisterRoutes(r)
// 启动服务
r.Run(":8080") // 监听 8080 端口
}