-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
166 lines (146 loc) · 4.58 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2021 冯立强 fenglq@tingyun.com. All rights reserved.
// +build linux
// +build amd64
// +build cgo
package beegoframe
import (
"net/http"
"reflect"
"runtime"
"github.com/astaxie/beego/context"
"github.com/TingYunGo/goagent"
beego "github.com/astaxie/beego"
param "github.com/astaxie/beego/context/param"
)
const (
beegoRoutineLocalIndex = 9 + 8 + 8 + 8
)
type handlerInfo struct {
name string
method string
isFunc bool
}
var tempVar = 0x1234567890
//go:noinline
func beegoAddMethod(p *beego.ControllerRegister, method, pattern string, f beego.FilterFunc) {
tempVar += 10
}
var methodMap = map[string]string{
"GET": "Get",
"POST": "Post",
"PUT": "Put",
"DELETE": "Delete",
"PATCH": "Patch",
"OPTIONS": "Options",
"HEAD": "Head",
"TRACE": "Trace",
}
//go:noinline
func WrapbeegoAddMethod(p *beego.ControllerRegister, method, pattern string, f beego.FilterFunc) {
handlerPC := reflect.ValueOf(f).Pointer()
methodName := runtime.FuncForPC(handlerPC).Name()
info := handlerInfo{
name: methodName,
isFunc: true,
}
tingyun3.LocalSet(beegoRoutineLocalIndex, info)
defer tingyun3.LocalDelete(beegoRoutineLocalIndex)
beegoAddMethod(p, method, pattern, f)
}
//go:noinline
func beegoHandler(p *beego.ControllerRegister, pattern string, h http.Handler, options ...interface{}) {
tempVar += 10
}
//go:noinline
func WrapbeegoHandler(p *beego.ControllerRegister, pattern string, h http.Handler, options ...interface{}) {
handlerPC := reflect.ValueOf(h).Pointer()
methodName := runtime.FuncForPC(handlerPC).Name()
info := handlerInfo{
name: methodName,
isFunc: true,
}
if len(methodName) == 0 {
reflectVal := reflect.ValueOf(h)
info.name = reflect.Indirect(reflectVal).Type().String()
info.method = "ServeHTTP"
info.isFunc = false
}
tingyun3.LocalSet(beegoRoutineLocalIndex, info)
defer tingyun3.LocalDelete(beegoRoutineLocalIndex)
beegoHandler(p, pattern, h, options...)
}
//go:noinline
func beegoaddWithMethodParams(p *beego.ControllerRegister, pattern string, c beego.ControllerInterface, methodParams []*param.MethodParam, mappingMethods ...string) {
tempVar += 10
}
//go:noinline
func WrapbeegoaddWithMethodParams(p *beego.ControllerRegister, pattern string, c beego.ControllerInterface, methodParams []*param.MethodParam, mappingMethods ...string) {
reflectVal := reflect.ValueOf(c)
info := handlerInfo{
name: reflect.Indirect(reflectVal).Type().String(),
isFunc: false,
}
tingyun3.LocalSet(beegoRoutineLocalIndex, info)
defer tingyun3.LocalDelete(beegoRoutineLocalIndex)
beegoaddWithMethodParams(p, pattern, c, methodParams, mappingMethods...)
}
//go:noinline
func beegoAddAutoPrefix(p *beego.ControllerRegister, prefix string, c beego.ControllerInterface) {
tempVar += 10
}
//go:noinline
func WrapbeegoAddAutoPrefix(p *beego.ControllerRegister, prefix string, c beego.ControllerInterface) {
reflectVal := reflect.ValueOf(c)
info := handlerInfo{
name: reflect.Indirect(reflectVal).Type().String(),
isFunc: false,
}
tingyun3.LocalSet(beegoRoutineLocalIndex, info)
defer tingyun3.LocalDelete(beegoRoutineLocalIndex)
beegoAddAutoPrefix(p, prefix, c)
}
var routeMap map[string]bool = nil
//go:noinline
func beegoaddToRouter(p *beego.ControllerRegister, method, pattern string, r *beego.ControllerInfo) {
tempVar += 10
}
//go:noinline
func WrapbeegoaddToRouter(p *beego.ControllerRegister, method, pattern string, r *beego.ControllerInfo) {
if routeMap == nil {
routeMap = make(map[string]bool)
}
info := tingyun3.LocalGet(beegoRoutineLocalIndex)
if _, found := routeMap[pattern]; !found {
routeMap[pattern] = true
handler := handlerInfo{}
if info != nil {
handler = info.(handlerInfo)
}
p.InsertFilter(pattern, beego.BeforeExec, func(ctx *context.Context) {
action := tingyun3.GetAction()
if action != nil {
if len(handler.name) > 0 {
if handler.isFunc {
action.SetName("ROUTE", handler.name)
} else {
if len(handler.method) > 0 {
action.SetName("Method", handler.method)
} else if method, found := methodMap[action.GetMethod()]; found {
action.SetName("Method", method)
}
action.SetName("CLASS", handler.name)
}
}
action.SetName("URI", pattern)
}
})
}
beegoaddToRouter(p, method, pattern, r)
}
func init() {
tingyun3.Register(reflect.ValueOf(WrapbeegoaddToRouter).Pointer())
tingyun3.Register(reflect.ValueOf(WrapbeegoAddMethod).Pointer())
tingyun3.Register(reflect.ValueOf(WrapbeegoHandler).Pointer())
tingyun3.Register(reflect.ValueOf(WrapbeegoAddAutoPrefix).Pointer())
tingyun3.Register(reflect.ValueOf(WrapbeegoaddWithMethodParams).Pointer())
}