forked from devfeel/dotweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dotweb_sysgroup.go
78 lines (68 loc) · 2.09 KB
/
dotweb_sysgroup.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
package dotweb
import (
jsonutil "github.com/devfeel/dotweb/framework/json"
"github.com/devfeel/dotweb/framework/stringx"
"runtime"
"runtime/debug"
"runtime/pprof"
"strings"
)
// initDotwebGroup init Dotweb route group which start with /dotweb/
func initDotwebGroup(server *HttpServer) {
gInner := server.Group("/dotweb")
gInner.GET("/debug/pprof/:key", showPProf)
gInner.GET("/debug/freemem", freeMemory)
gInner.GET("/state", showServerState)
gInner.GET("/state/interval", showIntervalData)
gInner.GET("/query/:key", showQuery)
gInner.GET("/routers", showRouters)
}
// query pprof debug info
// key:heap goroutine threadcreate block
func showPProf(ctx Context) error {
querykey := ctx.GetRouterName("key")
runtime.GC()
return pprof.Lookup(querykey).WriteTo(ctx.Response().Writer(), 1)
}
func freeMemory(ctx Context) error {
debug.FreeOSMemory()
return nil
}
func showIntervalData(ctx Context) error {
type data struct {
Time string
RequestCount uint64
ErrorCount uint64
}
queryKey := ctx.QueryString("querykey")
d := new(data)
d.Time = queryKey
d.RequestCount = ctx.HttpServer().StateInfo().QueryIntervalRequestData(queryKey)
d.ErrorCount = ctx.HttpServer().StateInfo().QueryIntervalErrorData(queryKey)
return ctx.WriteJson(d)
}
// snow server status
func showServerState(ctx Context) error {
return ctx.WriteHtml(ctx.HttpServer().StateInfo().ShowHtmlData(Version, ctx.HttpServer().DotApp.GlobalUniqueID()))
}
// query server information
func showQuery(ctx Context) error {
querykey := ctx.GetRouterName("key")
switch querykey {
case "state":
return ctx.WriteString(jsonutil.GetJsonString(ctx.HttpServer().StateInfo()))
case "":
return ctx.WriteString("please input key")
default:
return ctx.WriteString("not support key => " + querykey)
}
}
func showRouters(ctx Context) error {
result := ""
for k, _ := range ctx.HttpServer().router.GetAllRouterExpress() {
method := strings.Split(k, routerExpressSplit)[0]
router := strings.Split(k, routerExpressSplit)[1]
result += stringx.CompletionRight(method, " ", 12) + router + "\r\n"
}
return ctx.WriteString(result)
}