github.com/kyleu/dbaudit@v0.0.2-0.20240321155047-ff2f2c940496/app/controller/clib/admin.go (about) 1 // Package clib - Content managed by Project Forge, see [projectforge.md] for details. 2 package clib 3 4 import ( 5 "fmt" 6 "net/http" 7 "runtime" 8 "runtime/pprof" 9 "strings" 10 11 "github.com/pkg/errors" 12 13 "github.com/kyleu/dbaudit/app" 14 "github.com/kyleu/dbaudit/app/controller" 15 "github.com/kyleu/dbaudit/app/controller/cutil" 16 "github.com/kyleu/dbaudit/app/lib/database/migrate" 17 "github.com/kyleu/dbaudit/app/lib/log" 18 "github.com/kyleu/dbaudit/app/lib/user" 19 "github.com/kyleu/dbaudit/app/util" 20 "github.com/kyleu/dbaudit/views/vadmin" 21 ) 22 23 func Admin(w http.ResponseWriter, r *http.Request) { 24 path := util.StringSplitAndTrim(strings.TrimPrefix(r.URL.Path, "/admin"), "/") 25 key := "admin" 26 if len(path) > 0 { 27 key += "." + strings.Join(path, ".") 28 } 29 controller.Act(key, w, r, func(as *app.State, ps *cutil.PageState) (string, error) { 30 if len(path) == 0 { 31 ps.SetTitleAndData("Administration", "administration") 32 return controller.Render(w, r, as, &vadmin.Settings{Perms: user.GetPermissions()}, ps, "admin") 33 } 34 ps.DefaultNavIcon = "cog" 35 switch path[0] { 36 case "server": 37 info := util.DebugGetInfo(as.BuildInfo.Version, as.Started) 38 ps.Data = info 39 return controller.Render(w, r, as, &vadmin.ServerInfo{Info: info}, ps, "admin", "App Information") 40 case "cpu": 41 switch path[1] { 42 case "start": 43 err := util.DebugStartCPUProfile() 44 if err != nil { 45 return "", err 46 } 47 return controller.FlashAndRedir(true, "started CPU profile", "/admin", w, ps) 48 case "stop": 49 pprof.StopCPUProfile() 50 return controller.FlashAndRedir(true, "stopped CPU profile", "/admin", w, ps) 51 default: 52 return "", errors.Errorf("unhandled CPU profile action [%s]", path[1]) 53 } 54 case "gc": 55 timer := util.TimerStart() 56 runtime.GC() 57 msg := fmt.Sprintf("ran garbage collection in [%s]", timer.EndString()) 58 return controller.FlashAndRedir(true, msg, "/admin", w, ps) 59 case "heap": 60 err := util.DebugTakeHeapProfile() 61 if err != nil { 62 return "", err 63 } 64 return controller.FlashAndRedir(true, "wrote heap profile", "/admin", w, ps) 65 case "logs": 66 ps.SetTitleAndData("Recent Logs", log.RecentLogs) 67 return controller.Render(w, r, as, &vadmin.Logs{Logs: log.RecentLogs}, ps, "admin", "Recent Logs**folder") 68 case "memusage": 69 x := util.DebugMemStats() 70 ps.Data = x 71 return controller.Render(w, r, as, &vadmin.MemUsage{Mem: x}, ps, "admin", "Memory Usage**desktop") 72 case "migrations": 73 ms := migrate.GetMigrations() 74 am := migrate.ListMigrations(ps.Context, as.DB, nil, nil, ps.Logger) 75 ps.Data = util.ValueMap{"available": ms, "applied": am} 76 return controller.Render(w, r, as, &vadmin.Migrations{Available: ms, Applied: am}, ps, "admin", "Migrations**database") 77 case "modules": 78 di := util.DebugBuildInfo().Deps 79 ps.SetTitleAndData("Modules", di) 80 return controller.Render(w, r, as, &vadmin.Modules{Modules: di}, ps, "admin", "Modules**robot") 81 case "request": 82 ps.SetTitleAndData("Request Debug", cutil.RequestCtxToMap(w, r, as, ps)) 83 return controller.Render(w, r, as, &vadmin.Request{Req: r, Rsp: w}, ps, "admin", "Request**download") 84 case "routes": 85 ps.SetTitleAndData("HTTP Routes", cutil.AppRoutesList) 86 return controller.Render(w, r, as, &vadmin.Routes{Routes: cutil.AppRoutesList}, ps, "admin", "Routes**folder") 87 case "session": 88 ps.SetTitleAndData("Session Debug", ps.Session) 89 return controller.Render(w, r, as, &vadmin.Session{}, ps, "admin", "Session**play") 90 case "sitemap": 91 ps.SetTitleAndData("Sitemap", ps.Menu) 92 return controller.Render(w, r, as, &vadmin.Sitemap{}, ps, "admin", "Sitemap**graph") 93 // $PF_SECTION_START(admin-actions)$ 94 // $PF_SECTION_END(admin-actions)$ 95 default: 96 return "", errors.Errorf("unhandled admin action [%s]", path[0]) 97 } 98 }) 99 }