2019-12-08 16:59:58 +08:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
2020-12-19 10:57:10 +08:00
|
|
|
"strings"
|
2019-12-08 16:59:58 +08:00
|
|
|
"time"
|
|
|
|
|
2019-12-09 18:14:31 +08:00
|
|
|
"code.cloudfoundry.org/bytefmt"
|
2021-05-10 18:04:38 +08:00
|
|
|
"github.com/gin-contrib/pprof"
|
2019-12-08 16:59:58 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
2020-11-11 10:07:45 +08:00
|
|
|
"github.com/naiba/nezha/pkg/mygin"
|
|
|
|
"github.com/naiba/nezha/service/dao"
|
2019-12-08 16:59:58 +08:00
|
|
|
)
|
|
|
|
|
2019-12-10 18:05:02 +08:00
|
|
|
func ServeWeb(port uint) {
|
2019-12-08 23:18:29 +08:00
|
|
|
gin.SetMode(gin.ReleaseMode)
|
2021-05-10 18:04:38 +08:00
|
|
|
r := gin.Default()
|
2019-12-08 23:18:29 +08:00
|
|
|
if dao.Conf.Debug {
|
|
|
|
gin.SetMode(gin.DebugMode)
|
2021-05-10 18:04:38 +08:00
|
|
|
pprof.Register(r)
|
2019-12-08 23:18:29 +08:00
|
|
|
}
|
2019-12-08 16:59:58 +08:00
|
|
|
r.Use(mygin.RecordPath)
|
|
|
|
r.SetFuncMap(template.FuncMap{
|
|
|
|
"tf": func(t time.Time) string {
|
2021-01-19 09:59:04 +08:00
|
|
|
return t.Format("2006年1月2号 15:04:05")
|
2019-12-08 16:59:58 +08:00
|
|
|
},
|
2020-12-20 22:09:53 +08:00
|
|
|
"safe": func(s string) template.HTML {
|
|
|
|
return template.HTML(s)
|
2020-12-19 10:57:10 +08:00
|
|
|
},
|
2020-12-19 12:43:03 +08:00
|
|
|
"tag": func(s string) template.HTML {
|
|
|
|
return template.HTML(`<` + s + `>`)
|
|
|
|
},
|
2019-12-09 23:45:23 +08:00
|
|
|
"stf": func(s uint64) string {
|
|
|
|
return time.Unix(int64(s), 0).Format("2006年1月2号 15:04")
|
|
|
|
},
|
|
|
|
"sf": func(duration uint64) string {
|
|
|
|
return time.Duration(time.Duration(duration) * time.Second).String()
|
|
|
|
},
|
2019-12-09 18:14:31 +08:00
|
|
|
"bf": func(b uint64) string {
|
|
|
|
return bytefmt.ByteSize(b)
|
|
|
|
},
|
2020-12-19 10:57:10 +08:00
|
|
|
"ts": func(s string) string {
|
|
|
|
return strings.TrimSpace(s)
|
|
|
|
},
|
2021-01-16 14:11:51 +08:00
|
|
|
"float32f": func(f float32) string {
|
|
|
|
return fmt.Sprintf("%.2f", f)
|
|
|
|
},
|
2021-01-16 00:45:49 +08:00
|
|
|
"divU64": func(a, b uint64) float32 {
|
|
|
|
if b == 0 {
|
|
|
|
if a > 0 {
|
|
|
|
return 100
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
2021-01-16 14:11:51 +08:00
|
|
|
if a == 0 {
|
|
|
|
// 这是从未在线的情况
|
2021-04-19 23:11:00 +08:00
|
|
|
return 0.00001 / float32(b) * 100
|
2021-01-16 14:11:51 +08:00
|
|
|
}
|
2021-01-16 00:45:49 +08:00
|
|
|
return float32(a) / float32(b) * 100
|
|
|
|
},
|
|
|
|
"div": func(a, b int) float32 {
|
|
|
|
if b == 0 {
|
|
|
|
if a > 0 {
|
|
|
|
return 100
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
2021-01-16 14:11:51 +08:00
|
|
|
if a == 0 {
|
|
|
|
// 这是从未在线的情况
|
2021-04-19 23:11:00 +08:00
|
|
|
return 0.00001 / float32(b) * 100
|
2021-01-16 14:11:51 +08:00
|
|
|
}
|
2021-01-16 00:45:49 +08:00
|
|
|
return float32(a) / float32(b) * 100
|
|
|
|
},
|
|
|
|
"addU64": func(a, b uint64) uint64 {
|
|
|
|
return a + b
|
|
|
|
},
|
|
|
|
"add": func(a, b int) int {
|
|
|
|
return a + b
|
|
|
|
},
|
|
|
|
"dayBefore": func(i int) string {
|
|
|
|
year, month, day := time.Now().Date()
|
|
|
|
today := time.Date(year, month, day, 0, 0, 0, 0, time.Local)
|
|
|
|
return today.AddDate(0, 0, i-29).Format("1月2号")
|
|
|
|
},
|
2021-01-16 11:23:42 +08:00
|
|
|
"className": func(percent float32) string {
|
|
|
|
if percent == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if percent > 95 {
|
|
|
|
return "good"
|
|
|
|
}
|
|
|
|
if percent > 80 {
|
|
|
|
return "warning"
|
|
|
|
}
|
|
|
|
return "danger"
|
|
|
|
},
|
|
|
|
"statusName": func(percent float32) string {
|
|
|
|
if percent == 0 {
|
|
|
|
return "无数据"
|
|
|
|
}
|
|
|
|
if percent > 95 {
|
|
|
|
return "良好"
|
|
|
|
}
|
|
|
|
if percent > 80 {
|
|
|
|
return "低可用"
|
|
|
|
}
|
|
|
|
return "故障"
|
|
|
|
},
|
2019-12-08 16:59:58 +08:00
|
|
|
})
|
|
|
|
r.Static("/static", "resource/static")
|
|
|
|
r.LoadHTMLGlob("resource/template/**/*")
|
|
|
|
routers(r)
|
2019-12-10 18:05:02 +08:00
|
|
|
r.Run(fmt.Sprintf(":%d", port))
|
2019-12-08 16:59:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func routers(r *gin.Engine) {
|
|
|
|
// 通用页面
|
|
|
|
cp := commonPage{r}
|
|
|
|
cp.serve()
|
|
|
|
// 游客页面
|
|
|
|
gp := guestPage{r}
|
|
|
|
gp.serve()
|
|
|
|
// 会员页面
|
|
|
|
mp := &memberPage{r}
|
|
|
|
mp.serve()
|
|
|
|
// API
|
|
|
|
api := r.Group("api")
|
|
|
|
{
|
|
|
|
ma := &memberAPI{api}
|
|
|
|
ma.serve()
|
|
|
|
}
|
|
|
|
}
|