Core

/app/controller/clib/admin.go (4.1 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
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
package clib

import (
"fmt"
"runtime"
"runtime/pprof"
"strings"

"github.com/pkg/errors"
"github.com/valyala/fasthttp"

"{{{ .Package }}}/app"
"{{{ .Package }}}/app/controller"
"{{{ .Package }}}/app/controller/cutil"{{{ if .HasModule "migration" }}}
"{{{ .Package }}}/app/lib/database/migrate"{{{ end }}}
"{{{ .Package }}}/app/lib/log"{{{ if .HasAccount }}}
"{{{ .Package }}}/app/lib/user"{{{ end }}}
"{{{ .Package }}}/app/util"
"{{{ .Package }}}/views/vadmin"
)

var AppRoutesList map[string][]string

func Admin(rc *fasthttp.RequestCtx) {
path := util.StringSplitAndTrim(strings.TrimPrefix(string(rc.URI().Path()), "/admin"), "/")
key := "admin"
if len(path) > 0 {
key += "." + strings.Join(path, ".")
}
controller.Act(key, rc, func(as *app.State, ps *cutil.PageState) (string, error) {
if len(path) == 0 {
ps.Title = "Administration"
ps.Data = "administration"
return controller.Render(rc, as, {{{ if .HasAccount }}}&vadmin.Settings{Perms: user.GetPermissions()}{{{ else }}}&vadmin.Settings{}{{{ end }}}, ps, "admin")
}
switch path[0] {
case "server":
info := util.DebugGetInfo(as.BuildInfo.Version, as.Started)
ps.Data = info
return controller.Render(rc, as, &vadmin.ServerInfo{Info: info}, ps, "admin", "App Information")
case "cpu":
switch path[1] {
case "start":
err := util.DebugStartCPUProfile()
if err != nil {
return "", err
}
return controller.FlashAndRedir(true, "started CPU profile", "/admin", rc, ps)
case "stop":
pprof.StopCPUProfile()
return controller.FlashAndRedir(true, "stopped CPU profile", "/admin", rc, ps)
default:
return "", errors.Errorf("unhandled CPU profile action [%s]", path[1])
}
case "gc":
timer := util.TimerStart()
runtime.GC()
msg := fmt.Sprintf("ran garbage collection in [%s]", timer.EndString())
return controller.FlashAndRedir(true, msg, "/admin", rc, ps)
case "heap":
err := util.DebugTakeHeapProfile()
if err != nil {
return "", err
}
return controller.FlashAndRedir(true, "wrote heap profile", "/admin", rc, ps)
case "logs":
ps.Title = "Recent Logs"
ps.Data = log.RecentLogs
return controller.Render(rc, as, &vadmin.Logs{Logs: log.RecentLogs}, ps, "admin", "Recent Logs")
case "memusage":
x := util.DebugMemStats()
ps.Data = x
return controller.Render(rc, as, &vadmin.MemUsage{Mem: x}, ps, "admin", "Memory Usage"){{{ if .HasModule "migration" }}}
case "migrations":
ms := migrate.GetMigrations()
am := migrate.ListMigrations(ps.Context, as.DB, nil, nil, ps.Logger)
ps.Data = util.ValueMap{"available": ms, "applied": am}
return controller.Render(rc, as, &vadmin.Migrations{Available: ms, Applied: am}, ps, "admin", "Migrations"){{{ end }}}
case "modules":
di := util.DebugBuildInfo().Deps
ps.Title = "Modules"
ps.Data = di
return controller.Render(rc, as, &vadmin.Modules{Modules: di}, ps, "admin", "Modules"){{{ if .HasModule "queue" }}}
case "queue":
ps.Title = "Queue Debug"
ps.Data = as.Services.Publisher
return controller.Render(rc, as, &vadmin.Queue{Con: as.Services.Consumer, Pub: as.Services.Publisher}, ps, "admin", "Queue"){{{ end }}}
case "request":
ps.Title = "Request Debug"
ps.Data = cutil.RequestCtxToMap(rc, as, ps)
return controller.Render(rc, as, &vadmin.Request{RC: rc}, ps, "admin", "Request")
case "routes":
ps.Title = "HTTP Routes"
ps.Data = AppRoutesList
return controller.Render(rc, as, &vadmin.Routes{Routes: AppRoutesList}, ps, "admin", "Request")
case "session":
ps.Title = "Session Debug"
ps.Data = ps.Session
return controller.Render(rc, as, &vadmin.Session{}, ps, "admin", "Session")
case "sitemap":
ps.Title = "Sitemap"
ps.Data = ps.Menu
return controller.Render(rc, as, &vadmin.Sitemap{}, ps, "admin", "Sitemap"){{{ if .HasModule "websocket" }}}
case "sockets":
return socketRoute(rc, as, ps, path[1:]...){{{ end }}}
// $PF_SECTION_START(admin-actions)$
// $PF_SECTION_END(admin-actions)$
default:
return "", errors.Errorf("unhandled admin action [%s]", path[0])
}
})
}