2013-11-13 13:11:03 +00:00
|
|
|
package beego
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
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"
|
2013-11-13 13:11:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var BeeAdminApp *AdminApp
|
|
|
|
|
2013-11-15 10:08:53 +00:00
|
|
|
//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
|
|
|
|
var FilterMonitorFunc func(string, string, time.Duration) bool
|
|
|
|
|
2013-11-13 13:11:03 +00:00
|
|
|
func init() {
|
|
|
|
BeeAdminApp = &AdminApp{
|
|
|
|
routers: make(map[string]http.HandlerFunc),
|
|
|
|
}
|
|
|
|
BeeAdminApp.Route("/", AdminIndex)
|
2013-11-15 10:08:53 +00:00
|
|
|
BeeAdminApp.Route("/qps", QpsIndex)
|
|
|
|
BeeAdminApp.Route("/prof", ProfIndex)
|
2013-12-15 13:22:50 +00:00
|
|
|
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-11-13 13:11:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func AdminIndex(rw http.ResponseWriter, r *http.Request) {
|
2013-12-12 13:51:17 +00:00
|
|
|
rw.Write([]byte("Welcome to Admin Dashboard\n"))
|
|
|
|
rw.Write([]byte("There are servral functions:\n"))
|
|
|
|
rw.Write([]byte("1. Record all request and request time, http://localhost:8088/qps\n"))
|
|
|
|
rw.Write([]byte("2. Get runtime profiling data by the pprof, http://localhost:8088/prof\n"))
|
|
|
|
rw.Write([]byte("3. Get healthcheck result from http://localhost:8088/prof\n"))
|
|
|
|
rw.Write([]byte("4. Get current task infomation from taskhttp://localhost:8088/task \n"))
|
|
|
|
rw.Write([]byte("5. To run a task passed a param http://localhost:8088/runtask\n"))
|
2013-12-15 13:33:39 +00:00
|
|
|
rw.Write([]byte("6. Get all confige & router infomation http://localhost:8088/listconf\n"))
|
2013-12-12 13:51:17 +00:00
|
|
|
|
2013-11-13 13:11:03 +00:00
|
|
|
}
|
|
|
|
|
2013-11-15 10:08:53 +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-11-17 15:10:21 +00:00
|
|
|
|
2013-12-15 13:22:50 +00:00
|
|
|
func ListConf(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
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)
|
|
|
|
fmt.Fprintln(rw, "HttpTLS:", HttpTLS)
|
|
|
|
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, "EnableHotUpdate:", EnableHotUpdate)
|
|
|
|
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:")
|
|
|
|
for _, router := range BeeApp.Handlers.fixrouters {
|
|
|
|
if router.hasMethod {
|
|
|
|
fmt.Fprintln(rw, router.pattern, "----", router.methods, "----", router.controllerType.Name())
|
|
|
|
} else {
|
|
|
|
fmt.Fprintln(rw, router.pattern, "----", router.controllerType.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, router := range BeeApp.Handlers.routers {
|
|
|
|
if router.hasMethod {
|
|
|
|
fmt.Fprintln(rw, router.pattern, "----", router.methods, "----", router.controllerType.Name())
|
|
|
|
} else {
|
|
|
|
fmt.Fprintln(rw, router.pattern, "----", router.controllerType.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if BeeApp.Handlers.enableAuto {
|
|
|
|
for controllerName, methodObj := range BeeApp.Handlers.autoRouter {
|
|
|
|
fmt.Fprintln(rw, controllerName, "----")
|
|
|
|
for methodName, obj := range methodObj {
|
|
|
|
fmt.Fprintln(rw, " ", methodName, "-----", obj.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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, "AfterStatic:")
|
|
|
|
if bf, ok := BeeApp.Handlers.filters[AfterStatic]; 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 {
|
|
|
|
rw.Write([]byte("ListConf support this command:\n"))
|
|
|
|
rw.Write([]byte("1. command=conf\n"))
|
|
|
|
rw.Write([]byte("2. command=router\n"))
|
|
|
|
rw.Write([]byte("3. command=filter\n"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-15 10:08:53 +00:00
|
|
|
func ProfIndex(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
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 {
|
2013-12-07 05:26:22 +00:00
|
|
|
rw.Write([]byte("request url like '/prof?command=lookup goroutine'\n"))
|
|
|
|
rw.Write([]byte("the command have below types:\n"))
|
|
|
|
rw.Write([]byte("1. lookup goroutine\n"))
|
|
|
|
rw.Write([]byte("2. lookup heap\n"))
|
|
|
|
rw.Write([]byte("3. lookup threadcreate\n"))
|
|
|
|
rw.Write([]byte("4. lookup block\n"))
|
|
|
|
rw.Write([]byte("5. start cpuprof\n"))
|
|
|
|
rw.Write([]byte("6. stop cpuprof\n"))
|
|
|
|
rw.Write([]byte("7. get memprof\n"))
|
|
|
|
rw.Write([]byte("8. gc summary\n"))
|
2013-11-15 10:08:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-15 13:22:50 +00:00
|
|
|
func Healthcheck(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
for name, h := range toolbox.AdminCheckList {
|
|
|
|
if err := h.Check(); err != nil {
|
|
|
|
fmt.Fprintf(rw, "%s : ok\n", name)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(rw, "%s : %s\n", name, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TaskStatus(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
for tname, tk := range toolbox.AdminTaskList {
|
|
|
|
fmt.Fprintf(rw, "%s:%s:%s", tname, tk.GetStatus(), tk.GetPrev().String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//to run a Task by http from the querystring taskname
|
|
|
|
//url like /task?taskname=sendmail
|
|
|
|
func RunTask(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(rw, "%s run success,Now the Status is %s", t.GetStatus())
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(rw, "there's no task which named:%s", taskname)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-13 13:11:03 +00:00
|
|
|
type AdminApp struct {
|
|
|
|
routers map[string]http.HandlerFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (admin *AdminApp) Route(pattern string, f http.HandlerFunc) {
|
|
|
|
admin.routers[pattern] = f
|
|
|
|
}
|
|
|
|
|
|
|
|
func (admin *AdminApp) Run() {
|
2013-11-20 15:53:54 +00:00
|
|
|
if len(toolbox.AdminTaskList) > 0 {
|
|
|
|
toolbox.StartTask()
|
|
|
|
}
|
2013-11-13 13:11:03 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|