1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-10 20:30:54 +00:00
Beego/admin.go

280 lines
11 KiB
Go
Raw Normal View History

2014-04-12 05:18:18 +00:00
// Beego (http://beego.me/)
// @description beego is an open-source, high-performance web framework for the Go programming language.
// @link http://github.com/astaxie/beego for the canonical source repository
// @license http://github.com/astaxie/beego/blob/master/LICENSE
// @authors astaxie
package beego
import (
"fmt"
"net/http"
2014-05-16 18:20:48 +00:00
"strconv"
2013-11-15 10:08:53 +00:00
"time"
2013-12-03 13:37:39 +00:00
"github.com/astaxie/beego/toolbox"
2013-12-15 13:44:58 +00:00
"github.com/astaxie/beego/utils"
)
2014-04-05 17:02:10 +00:00
// BeeAdminApp is the default adminApp used by admin module.
var beeAdminApp *adminApp
2013-12-19 15:46:06 +00:00
// FilterMonitorFunc is default monitor filter when admin module is enable.
// if this func returns, admin module records qbs for this request by condition of this function logic.
// usage:
// func MyFilterMonitor(method, requestPath string, t time.Duration) bool {
// if method == "POST" {
// return false
// }
// if t.Nanoseconds() < 100 {
// return false
// }
// if strings.HasPrefix(requestPath, "/astaxie") {
// return false
// }
// return true
// }
// beego.FilterMonitorFunc = MyFilterMonitor.
2013-11-15 10:08:53 +00:00
var FilterMonitorFunc func(string, string, time.Duration) bool
func init() {
2014-04-05 17:02:10 +00:00
beeAdminApp = &adminApp{
routers: make(map[string]http.HandlerFunc),
}
2014-04-05 17:02:10 +00:00
beeAdminApp.Route("/", adminIndex)
beeAdminApp.Route("/qps", qpsIndex)
beeAdminApp.Route("/prof", profIndex)
beeAdminApp.Route("/healthcheck", healthcheck)
beeAdminApp.Route("/task", taskStatus)
beeAdminApp.Route("/runtask", runTask)
beeAdminApp.Route("/listconf", listConf)
2013-11-15 10:08:53 +00:00
FilterMonitorFunc = func(string, string, time.Duration) bool { return true }
}
2013-12-19 15:46:06 +00:00
// AdminIndex is the default http.Handler for admin module.
// it matches url pattern "/".
2014-04-05 17:02:10 +00:00
func adminIndex(rw http.ResponseWriter, r *http.Request) {
2014-05-16 18:20:48 +00:00
rw.Write([]byte("<html><head><title>beego admin dashboard</title></head><body>"))
rw.Write([]byte("Welcome to Admin Dashboard<br>\n"))
rw.Write([]byte("There are servral functions:<br>\n"))
rw.Write([]byte("1. Record all request and request time, <a href='/qps'>http://localhost:" + strconv.Itoa(AdminHttpPort) + "/qps</a><br>\n"))
rw.Write([]byte("2. Get runtime profiling data by the pprof, <a href='/prof'>http://localhost:" + strconv.Itoa(AdminHttpPort) + "/prof</a><br>\n"))
rw.Write([]byte("3. Get healthcheck result from <a href='/healthcheck'>http://localhost:" + strconv.Itoa(AdminHttpPort) + "/healthcheck</a><br>\n"))
rw.Write([]byte("4. Get current task infomation from task <a href='/task'>http://localhost:" + strconv.Itoa(AdminHttpPort) + "/task</a><br> \n"))
rw.Write([]byte("5. To run a task passed a param <a href='/runtask'>http://localhost:" + strconv.Itoa(AdminHttpPort) + "/runtask</a><br>\n"))
rw.Write([]byte("6. Get all confige & router infomation <a href='/listconf'>http://localhost:" + strconv.Itoa(AdminHttpPort) + "/listconf</a><br>\n"))
rw.Write([]byte("</body></html>"))
}
2013-12-19 15:46:06 +00:00
// QpsIndex is the http.Handler for writing qbs statistics map result info in http.ResponseWriter.
// it's registered with url pattern "/qbs" in admin module.
2014-04-05 17:02:10 +00:00
func qpsIndex(rw http.ResponseWriter, r *http.Request) {
2013-11-20 13:17:49 +00:00
toolbox.StatisticsMap.GetMap(rw)
2013-11-15 10:08:53 +00:00
}
2013-12-19 15:46:06 +00:00
// ListConf is the http.Handler of displaying all beego configuration values as key/value pair.
// it's registered with url pattern "/listconf" in admin module.
2014-04-05 17:02:10 +00:00
func listConf(rw http.ResponseWriter, r *http.Request) {
2013-12-15 13:22:50 +00:00
r.ParseForm()
command := r.Form.Get("command")
if command != "" {
switch command {
case "conf":
fmt.Fprintln(rw, "list all beego's conf:")
fmt.Fprintln(rw, "AppName:", AppName)
fmt.Fprintln(rw, "AppPath:", AppPath)
fmt.Fprintln(rw, "AppConfigPath:", AppConfigPath)
fmt.Fprintln(rw, "StaticDir:", StaticDir)
fmt.Fprintln(rw, "StaticExtensionsToGzip:", StaticExtensionsToGzip)
fmt.Fprintln(rw, "HttpAddr:", HttpAddr)
fmt.Fprintln(rw, "HttpPort:", HttpPort)
2014-05-20 07:30:17 +00:00
fmt.Fprintln(rw, "HttpTLS:", EnableHttpTLS)
2013-12-15 13:22:50 +00:00
fmt.Fprintln(rw, "HttpCertFile:", HttpCertFile)
fmt.Fprintln(rw, "HttpKeyFile:", HttpKeyFile)
fmt.Fprintln(rw, "RecoverPanic:", RecoverPanic)
fmt.Fprintln(rw, "AutoRender:", AutoRender)
fmt.Fprintln(rw, "ViewsPath:", ViewsPath)
fmt.Fprintln(rw, "RunMode:", RunMode)
fmt.Fprintln(rw, "SessionOn:", SessionOn)
fmt.Fprintln(rw, "SessionProvider:", SessionProvider)
fmt.Fprintln(rw, "SessionName:", SessionName)
fmt.Fprintln(rw, "SessionGCMaxLifetime:", SessionGCMaxLifetime)
fmt.Fprintln(rw, "SessionSavePath:", SessionSavePath)
fmt.Fprintln(rw, "SessionHashFunc:", SessionHashFunc)
fmt.Fprintln(rw, "SessionHashKey:", SessionHashKey)
fmt.Fprintln(rw, "SessionCookieLifeTime:", SessionCookieLifeTime)
fmt.Fprintln(rw, "UseFcgi:", UseFcgi)
fmt.Fprintln(rw, "MaxMemory:", MaxMemory)
fmt.Fprintln(rw, "EnableGzip:", EnableGzip)
fmt.Fprintln(rw, "DirectoryIndex:", DirectoryIndex)
fmt.Fprintln(rw, "HttpServerTimeOut:", HttpServerTimeOut)
fmt.Fprintln(rw, "ErrorsShow:", ErrorsShow)
fmt.Fprintln(rw, "XSRFKEY:", XSRFKEY)
fmt.Fprintln(rw, "EnableXSRF:", EnableXSRF)
fmt.Fprintln(rw, "XSRFExpire:", XSRFExpire)
fmt.Fprintln(rw, "CopyRequestBody:", CopyRequestBody)
fmt.Fprintln(rw, "TemplateLeft:", TemplateLeft)
fmt.Fprintln(rw, "TemplateRight:", TemplateRight)
fmt.Fprintln(rw, "BeegoServerName:", BeegoServerName)
fmt.Fprintln(rw, "EnableAdmin:", EnableAdmin)
fmt.Fprintln(rw, "AdminHttpAddr:", AdminHttpAddr)
fmt.Fprintln(rw, "AdminHttpPort:", AdminHttpPort)
case "router":
fmt.Fprintln(rw, "Print all router infomation:")
2014-06-12 16:08:43 +00:00
for method, t := range BeeApp.Handlers.routers {
2014-06-12 16:14:30 +00:00
fmt.Fprintln(rw)
fmt.Fprintln(rw)
fmt.Fprintln(rw, " Method:", method)
2014-06-12 16:08:43 +00:00
printTree(rw, t)
}
2014-06-08 12:24:01 +00:00
// @todo print routers
2013-12-15 13:22:50 +00:00
case "filter":
fmt.Fprintln(rw, "Print all filter infomation:")
if BeeApp.Handlers.enableFilter {
fmt.Fprintln(rw, "BeforeRouter:")
if bf, ok := BeeApp.Handlers.filters[BeforeRouter]; ok {
for _, f := range bf {
2013-12-15 13:44:58 +00:00
fmt.Fprintln(rw, f.pattern, utils.GetFuncName(f.filterFunc))
2013-12-15 13:22:50 +00:00
}
}
fmt.Fprintln(rw, "BeforeExec:")
if bf, ok := BeeApp.Handlers.filters[BeforeExec]; ok {
for _, f := range bf {
2013-12-15 13:44:58 +00:00
fmt.Fprintln(rw, f.pattern, utils.GetFuncName(f.filterFunc))
2013-12-15 13:22:50 +00:00
}
}
fmt.Fprintln(rw, "AfterExec:")
if bf, ok := BeeApp.Handlers.filters[AfterExec]; ok {
for _, f := range bf {
2013-12-15 13:44:58 +00:00
fmt.Fprintln(rw, f.pattern, utils.GetFuncName(f.filterFunc))
2013-12-15 13:22:50 +00:00
}
}
fmt.Fprintln(rw, "FinishRouter:")
if bf, ok := BeeApp.Handlers.filters[FinishRouter]; ok {
for _, f := range bf {
2013-12-15 13:44:58 +00:00
fmt.Fprintln(rw, f.pattern, utils.GetFuncName(f.filterFunc))
2013-12-15 13:22:50 +00:00
}
}
}
default:
rw.Write([]byte("command not support"))
}
} else {
2014-05-16 18:20:48 +00:00
rw.Write([]byte("<html><head><title>beego admin dashboard</title></head><body>"))
rw.Write([]byte("ListConf support this command:<br>\n"))
rw.Write([]byte("1. <a href='?command=conf'>command=conf</a><br>\n"))
rw.Write([]byte("2. <a href='?command=router'>command=router</a><br>\n"))
rw.Write([]byte("3. <a href='?command=filter'>command=filter</a><br>\n"))
rw.Write([]byte("</body></html>"))
2013-12-15 13:22:50 +00:00
}
}
2014-06-12 16:08:43 +00:00
func printTree(rw http.ResponseWriter, t *Tree) {
for _, tr := range t.fixrouters {
printTree(rw, tr)
}
if t.wildcard != nil {
printTree(rw, t.wildcard)
}
for _, l := range t.leaves {
if v, ok := l.runObject.(*controllerInfo); ok {
if v.routerType == routerTypeBeego {
fmt.Fprintln(rw, v.pattern, v.methods, v.controllerType.Name())
} else if v.routerType == routerTypeRESTFul {
fmt.Fprintln(rw, v.pattern, v.methods)
} else if v.routerType == routerTypeHandler {
fmt.Fprintln(rw, v.pattern, "handler")
}
}
}
}
2013-12-19 15:46:06 +00:00
// ProfIndex is a http.Handler for showing profile command.
// it's in url pattern "/prof" in admin module.
2014-04-05 17:02:10 +00:00
func profIndex(rw http.ResponseWriter, r *http.Request) {
2013-11-15 10:08:53 +00:00
r.ParseForm()
command := r.Form.Get("command")
if command != "" {
2013-11-20 13:17:49 +00:00
toolbox.ProcessInput(command, rw)
2013-11-15 10:08:53 +00:00
} else {
2014-05-16 18:20:48 +00:00
rw.Write([]byte("<html><head><title>beego admin dashboard</title></head><body>"))
rw.Write([]byte("request url like '/prof?command=lookup goroutine'<br>\n"))
rw.Write([]byte("the command have below types:<br>\n"))
rw.Write([]byte("1. <a href='?command=lookup goroutine'>lookup goroutine</a><br>\n"))
rw.Write([]byte("2. <a href='?command=lookup heap'>lookup heap</a><br>\n"))
rw.Write([]byte("3. <a href='?command=lookup threadcreate'>lookup threadcreate</a><br>\n"))
rw.Write([]byte("4. <a href='?command=lookup block'>lookup block</a><br>\n"))
rw.Write([]byte("5. <a href='?command=start cpuprof'>start cpuprof</a><br>\n"))
rw.Write([]byte("6. <a href='?command=stop cpuprof'>stop cpuprof</a><br>\n"))
rw.Write([]byte("7. <a href='?command=get memprof'>get memprof</a><br>\n"))
rw.Write([]byte("8. <a href='?command=gc summary'>gc summary</a><br>\n"))
rw.Write([]byte("</body></html>"))
2013-11-15 10:08:53 +00:00
}
}
2013-12-19 15:46:06 +00:00
// Healthcheck is a http.Handler calling health checking and showing the result.
// it's in "/healthcheck" pattern in admin module.
2014-04-05 17:02:10 +00:00
func healthcheck(rw http.ResponseWriter, req *http.Request) {
2013-12-15 13:22:50 +00:00
for name, h := range toolbox.AdminCheckList {
if err := h.Check(); err != nil {
fmt.Fprintf(rw, "%s : %s\n", name, err.Error())
2014-05-23 07:56:25 +00:00
} else {
fmt.Fprintf(rw, "%s : ok\n", name)
2013-12-15 13:22:50 +00:00
}
}
}
2013-12-19 15:46:06 +00:00
// TaskStatus is a http.Handler with running task status (task name, status and the last execution).
// it's in "/task" pattern in admin module.
2014-04-05 17:02:10 +00:00
func taskStatus(rw http.ResponseWriter, req *http.Request) {
2013-12-15 13:22:50 +00:00
for tname, tk := range toolbox.AdminTaskList {
fmt.Fprintf(rw, "%s:%s:%s", tname, tk.GetStatus(), tk.GetPrev().String())
}
}
2013-12-19 15:46:06 +00:00
// RunTask is a http.Handler to run a Task from the "query string.
// the request url likes /runtask?taskname=sendmail.
2014-04-05 17:02:10 +00:00
func runTask(rw http.ResponseWriter, req *http.Request) {
2013-12-15 13:22:50 +00:00
req.ParseForm()
taskname := req.Form.Get("taskname")
if t, ok := toolbox.AdminTaskList[taskname]; ok {
err := t.Run()
if err != nil {
fmt.Fprintf(rw, "%v", err)
}
2014-04-11 08:08:43 +00:00
fmt.Fprintf(rw, "%s run success,Now the Status is %s", taskname, t.GetStatus())
2013-12-15 13:22:50 +00:00
} else {
fmt.Fprintf(rw, "there's no task which named:%s", taskname)
}
}
2014-04-05 17:02:10 +00:00
// adminApp is an http.HandlerFunc map used as beeAdminApp.
type adminApp struct {
routers map[string]http.HandlerFunc
}
2014-04-05 17:02:10 +00:00
// Route adds http.HandlerFunc to adminApp with url pattern.
func (admin *adminApp) Route(pattern string, f http.HandlerFunc) {
admin.routers[pattern] = f
}
2014-04-05 17:02:10 +00:00
// Run adminApp http server.
2013-12-19 15:46:06 +00:00
// Its addr is defined in configuration file as adminhttpaddr and adminhttpport.
2014-04-05 17:02:10 +00:00
func (admin *adminApp) Run() {
2013-11-20 15:53:54 +00:00
if len(toolbox.AdminTaskList) > 0 {
toolbox.StartTask()
}
addr := AdminHttpAddr
if AdminHttpPort != 0 {
addr = fmt.Sprintf("%s:%d", AdminHttpAddr, AdminHttpPort)
}
for p, f := range admin.routers {
http.Handle(p, f)
}
err := http.ListenAndServe(addr, nil)
if err != nil {
BeeLogger.Critical("Admin ListenAndServe: ", err)
}
}