nezha/pkg/mygin/mygin.go

63 lines
2.0 KiB
Go
Raw Normal View History

2019-12-08 03:59:58 -05:00
package mygin
import (
"fmt"
"strings"
"github.com/gin-gonic/gin"
"github.com/nicksnyder/go-i18n/v2/i18n"
2019-12-08 03:59:58 -05:00
2020-11-10 21:07:45 -05:00
"github.com/naiba/nezha/model"
2022-01-08 22:54:14 -05:00
"github.com/naiba/nezha/service/singleton"
2019-12-08 03:59:58 -05:00
)
var adminPage = map[string]bool{
"/server": true,
"/monitor": true,
"/setting": true,
"/notification": true,
"/ddns": true,
2024-07-14 07:41:50 -04:00
"/nat": true,
"/cron": true,
2022-05-18 08:52:18 -04:00
"/api": true,
}
2019-12-08 03:59:58 -05:00
func CommonEnvironment(c *gin.Context, data map[string]interface{}) gin.H {
data["MatchedPath"] = c.MustGet("MatchedPath")
2022-01-08 22:54:14 -05:00
data["Version"] = singleton.Version
data["Conf"] = singleton.Conf
data["Themes"] = model.Themes
2024-08-13 12:24:17 -04:00
data["CustomCode"] = singleton.Conf.Site.CustomCode
data["CustomCodeDashboard"] = singleton.Conf.Site.CustomCodeDashboard
// 是否是管理页面
data["IsAdminPage"] = adminPage[data["MatchedPath"].(string)]
2019-12-08 03:59:58 -05:00
// 站点标题
if t, has := data["Title"]; !has {
2022-01-08 22:54:14 -05:00
data["Title"] = singleton.Conf.Site.Brand
2019-12-08 03:59:58 -05:00
} else {
2022-01-08 22:54:14 -05:00
data["Title"] = fmt.Sprintf("%s - %s", t, singleton.Conf.Site.Brand)
2019-12-08 03:59:58 -05:00
}
2019-12-20 10:58:09 -05:00
u, ok := c.Get(model.CtxKeyAuthorizedUser)
if ok {
data["Admin"] = u
2019-12-08 03:59:58 -05:00
}
data["LANG"] = map[string]string{
"Add": singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "Add"}),
"Edit": singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "Edit"}),
"AlarmRule": singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "AlarmRule"}),
"Notification": singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "NotificationMethod"}),
"Server": singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "Server"}),
"Monitor": singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "ServicesManagement"}),
"Cron": singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "ScheduledTasks"}),
}
2019-12-08 03:59:58 -05:00
return data
}
func RecordPath(c *gin.Context) {
url := c.Request.URL.String()
for _, p := range c.Params {
url = strings.Replace(url, p.Value, ":"+p.Key, 1)
}
c.Set("MatchedPath", url)
}