-
Notifications
You must be signed in to change notification settings - Fork 1
/
handle_test.go
95 lines (79 loc) · 2.03 KB
/
handle_test.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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package ginplus
import (
"database/sql"
"fmt"
"testing"
"github.com/gin-gonic/gin"
oteltrace "go.opentelemetry.io/otel/trace"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
type HandleApi struct {
}
func (l *HandleApi) Get() gin.HandlerFunc {
sqlDB, err := sql.Open("mysql", "root:12345678@tcp(localhost:3060)/prom?charset=utf8mb4&parseTime=True&loc=Local")
if err != nil {
panic(err)
}
db, err := gorm.Open(mysql.New(mysql.Config{
Conn: sqlDB,
}))
if err != nil {
panic(err)
}
db = db.Debug()
if err := db.Use(&OpentracingPlugin{}); err != nil {
panic(err)
}
return func(c *gin.Context) {
_span := c.Value("span")
fmt.Printf("span: %T\n", _span)
ctx := c.Request.Context()
span, ok := _span.(oteltrace.Span)
if ok {
fmt.Println("--------------ok------------------")
ctx, span = span.TracerProvider().Tracer("api").Start(ctx, "HandleApi.Get")
defer span.End()
}
type Dict struct {
Id uint `gorm:"column:id;primaryKey;autoIncrement;comment:主键ID"`
Name string `gorm:"column:name;type:varchar(255);not null;comment:字典名称"`
}
list := make([]*Dict, 0)
err = db.Table("prom_dict").WithContext(ctx).Find(&list).Error
if err != nil {
c.String(200, err.Error())
return
}
var first Dict
if err := db.Table("prom_dict").WithContext(ctx).First(&first).Error; err != nil {
c.String(200, err.Error())
return
}
if err := db.Table("prom_dict").WithContext(ctx).Where("id = 0").Updates(&first).Error; err != nil {
c.String(200, err.Error())
return
}
if err := db.Table("prom_dict").WithContext(ctx).Where("id = 0").Delete(&first).Error; err != nil {
c.String(200, err.Error())
return
}
c.JSON(200, list)
}
}
func NewHandleApiApi() *HandleApi {
return &HandleApi{}
}
func TestApiHandleFunc(t *testing.T) {
ginR := gin.New()
midd := NewMiddleware()
ginR.Use(
midd.Tracing("http://localhost:14268/api/traces"),
midd.Logger(),
midd.IpLimit(100, 5),
midd.Interceptor(),
midd.Cors(),
)
r := New(ginR, WithControllers(NewHandleApiApi()))
NewCtrlC(r).Start()
}