mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-04 04:30:05 +00:00
添加服务器 & 展示服务器
This commit is contained in:
@@ -26,7 +26,10 @@ func (cp *commonPage) home(c *gin.Context) {
|
||||
if ok && isLogin.(bool) {
|
||||
admin = dao.Admin
|
||||
}
|
||||
var servers []model.Server
|
||||
dao.DB.Find(&servers)
|
||||
c.HTML(http.StatusOK, "page/home", mygin.CommonEnvironment(c, gin.H{
|
||||
"Admin": admin,
|
||||
"Admin": admin,
|
||||
"Servers": servers,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -13,6 +13,10 @@ import (
|
||||
|
||||
// ServeWeb ..
|
||||
func ServeWeb() {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
if dao.Conf.Debug {
|
||||
gin.SetMode(gin.DebugMode)
|
||||
}
|
||||
r := gin.Default()
|
||||
r.Use(mygin.RecordPath)
|
||||
r.SetFuncMap(template.FuncMap{
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/naiba/com"
|
||||
|
||||
"github.com/p14yground/nezha/model"
|
||||
"github.com/p14yground/nezha/pkg/mygin"
|
||||
@@ -27,6 +28,33 @@ func (ma *memberAPI) serve() {
|
||||
}))
|
||||
|
||||
mr.POST("/logout", ma.logout)
|
||||
mr.POST("/server", ma.addServer)
|
||||
}
|
||||
|
||||
type serverForm struct {
|
||||
Name string `binding:"required"`
|
||||
}
|
||||
|
||||
func (ma *memberAPI) addServer(c *gin.Context) {
|
||||
var sf serverForm
|
||||
err := c.ShouldBindJSON(&sf)
|
||||
if err == nil {
|
||||
var s model.Server
|
||||
s.Name = sf.Name
|
||||
s.Secret = com.MD5(fmt.Sprintf("%s%s%d", time.Now(), sf.Name, dao.Admin.ID))
|
||||
s.Secret = s.Secret[:10]
|
||||
err = dao.DB.Create(&s).Error
|
||||
}
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, model.Response{
|
||||
Code: http.StatusBadRequest,
|
||||
Message: fmt.Sprintf("请求错误:%s", err),
|
||||
})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, model.Response{
|
||||
Code: http.StatusOK,
|
||||
})
|
||||
}
|
||||
|
||||
type logoutForm struct {
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/p14yground/nezha/model"
|
||||
"github.com/p14yground/nezha/pkg/mygin"
|
||||
"github.com/p14yground/nezha/service/dao"
|
||||
)
|
||||
|
||||
type memberPage struct {
|
||||
@@ -18,4 +22,14 @@ func (mp *memberPage) serve() {
|
||||
Btn: "点此登录",
|
||||
Redirect: "/login",
|
||||
}))
|
||||
mr.GET("/server", mp.server)
|
||||
}
|
||||
|
||||
func (mp *memberPage) server(c *gin.Context) {
|
||||
var servers []model.Server
|
||||
dao.DB.Find(&servers)
|
||||
c.HTML(http.StatusOK, "page/server", mygin.CommonEnvironment(c, gin.H{
|
||||
"Title": "服务器管理",
|
||||
"Servers": servers,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -26,7 +26,16 @@ func init() {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if dao.Conf.Debug {
|
||||
dao.DB = dao.DB.Debug()
|
||||
}
|
||||
dao.Cache = cache.New(5*time.Minute, 10*time.Minute)
|
||||
|
||||
initDB()
|
||||
}
|
||||
|
||||
func initDB() {
|
||||
dao.DB.AutoMigrate(model.Server{})
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
Reference in New Issue
Block a user