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-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-11-20 13:17:49 +00:00
|
|
|
BeeAdminApp.Route("/healthcheck", toolbox.Healthcheck)
|
|
|
|
BeeAdminApp.Route("/task", toolbox.TaskStatus)
|
|
|
|
BeeAdminApp.Route("/runtask", toolbox.RunTask)
|
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) {
|
|
|
|
rw.Write([]byte("Welcome to Admin Dashboard"))
|
|
|
|
}
|
|
|
|
|
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-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-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)
|
|
|
|
}
|
|
|
|
}
|