1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-19 02:00:56 +00:00
Beego/server/web/admin.go

127 lines
3.4 KiB
Go
Raw Normal View History

2020-07-22 14:50:08 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package web
2020-07-22 14:50:08 +00:00
import (
"fmt"
"net/http"
"reflect"
"time"
"github.com/astaxie/beego/core/logs"
2020-07-22 14:50:08 +00:00
)
// BeeAdminApp is the default adminApp used by admin module.
var beeAdminApp *adminApp
// FilterMonitorFunc is default monitor filter when admin module is enable.
// if this func returns, admin module records qps for this request by condition of this function logic.
// usage:
// func MyFilterMonitor(method, requestPath string, t time.Duration, pattern string, statusCode int) 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, string, int) bool
func init() {
2020-09-20 07:36:08 +00:00
2020-07-22 14:50:08 +00:00
FilterMonitorFunc = func(string, string, time.Duration, string, int) bool { return true }
}
func list(root string, p interface{}, m M) {
pt := reflect.TypeOf(p)
pv := reflect.ValueOf(p)
if pt.Kind() == reflect.Ptr {
pt = pt.Elem()
pv = pv.Elem()
}
for i := 0; i < pv.NumField(); i++ {
var key string
if root == "" {
key = pt.Field(i).Name
} else {
key = root + "." + pt.Field(i).Name
}
if pv.Field(i).Kind() == reflect.Struct {
list(key, pv.Field(i).Interface(), m)
} else {
m[key] = pv.Field(i).Interface()
}
}
}
func writeJSON(rw http.ResponseWriter, jsonData []byte) {
rw.Header().Set("Content-Type", "application/json")
rw.Write(jsonData)
}
// adminApp is an http.HandlerFunc map used as beeAdminApp.
type adminApp struct {
*HttpServer
2020-07-22 14:50:08 +00:00
}
// Route adds http.HandlerFunc to adminApp with url pattern.
func (admin *adminApp) Run() {
// if len(task.AdminTaskList) > 0 {
// task.StartTask()
// }
logs.Warning("now we don't start tasks here, if you use task module," +
" please invoke task.StartTask, or task will not be executed")
2020-07-22 14:50:08 +00:00
addr := BConfig.Listen.AdminAddr
if BConfig.Listen.AdminPort != 0 {
addr = fmt.Sprintf("%s:%d", BConfig.Listen.AdminAddr, BConfig.Listen.AdminPort)
}
2020-07-22 14:50:08 +00:00
logs.Info("Admin server Running on %s", addr)
2020-09-20 07:36:08 +00:00
admin.HttpServer.Run(addr)
}
2020-07-22 14:50:08 +00:00
func registerAdmin() error {
if BConfig.Listen.EnableAdmin {
2020-09-20 07:36:08 +00:00
c := &adminController{
servers: make([]*HttpServer, 0, 2),
}
beeAdminApp = &adminApp{
HttpServer: NewHttpServerWithCfg(*BConfig),
}
// keep in mind that all data should be html escaped to avoid XSS attack
beeAdminApp.Router("/", c, "get:AdminIndex")
beeAdminApp.Router("/qps", c, "get:QpsIndex")
beeAdminApp.Router("/prof", c, "get:ProfIndex")
beeAdminApp.Router("/healthcheck", c, "get:Healthcheck")
beeAdminApp.Router("/task", c, "get:TaskStatus")
beeAdminApp.Router("/listconf", c, "get:ListConf")
beeAdminApp.Router("/metrics", c, "get:PrometheusMetrics")
go beeAdminApp.Run()
2020-07-22 14:50:08 +00:00
}
return nil
2020-07-22 14:50:08 +00:00
}