展示服务器列表

This commit is contained in:
奶爸
2019-12-09 18:14:31 +08:00
parent 58277ba0b6
commit bfe6d48927
18 changed files with 173 additions and 77 deletions

View File

@@ -26,10 +26,10 @@ func (cp *commonPage) home(c *gin.Context) {
if ok && isLogin.(bool) {
admin = dao.Admin
}
var servers []model.Server
dao.DB.Find(&servers)
dao.ServerLock.RLock()
defer dao.ServerLock.RUnlock()
c.HTML(http.StatusOK, "page/home", mygin.CommonEnvironment(c, gin.H{
"Admin": admin,
"Servers": servers,
"Servers": dao.ServerList,
}))
}

View File

@@ -5,6 +5,7 @@ import (
"html/template"
"time"
"code.cloudfoundry.org/bytefmt"
"github.com/gin-gonic/gin"
"github.com/p14yground/nezha/pkg/mygin"
@@ -29,6 +30,9 @@ func ServeWeb() {
}
return fmt.Sprintf("%d", time.Now().UnixNano())
},
"bf": func(b uint64) string {
return bytefmt.ByteSize(b)
},
})
r.Static("/static", "resource/static")
r.LoadHTMLGlob("resource/template/**/*")

View File

@@ -7,7 +7,6 @@ import (
"github.com/gin-gonic/gin"
"github.com/naiba/com"
"github.com/patrickmn/go-cache"
"github.com/p14yground/nezha/model"
"github.com/p14yground/nezha/pkg/mygin"
@@ -53,7 +52,9 @@ func (ma *memberAPI) addServer(c *gin.Context) {
})
return
}
dao.Cache.Set(fmt.Sprintf("%s%d%s", model.CtxKeyServer, s.ID, s.Secret), s, cache.NoExpiration)
dao.ServerLock.Lock()
defer dao.ServerLock.Unlock()
dao.ServerList[fmt.Sprintf("%d", s.ID)] = &s
c.JSON(http.StatusOK, model.Response{
Code: http.StatusOK,
})

View File

@@ -4,7 +4,6 @@ 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"
)
@@ -26,10 +25,10 @@ func (mp *memberPage) serve() {
}
func (mp *memberPage) server(c *gin.Context) {
var servers []model.Server
dao.DB.Find(&servers)
dao.ServerLock.RLock()
defer dao.ServerLock.RUnlock()
c.HTML(http.StatusOK, "page/server", mygin.CommonEnvironment(c, gin.H{
"Title": "服务器管理",
"Servers": servers,
"Servers": dao.ServerList,
}))
}

View File

@@ -16,6 +16,7 @@ import (
func init() {
var err error
dao.ServerList = make(map[string]*model.Server)
dao.Conf, err = model.ReadInConfig("data/config.yaml")
if err != nil {
panic(err)
@@ -31,7 +32,6 @@ func init() {
dao.DB = dao.DB.Debug()
}
dao.Cache = cache.New(5*time.Minute, 10*time.Minute)
initDB()
}
@@ -41,7 +41,8 @@ func initDB() {
var servers []model.Server
dao.DB.Find(&servers)
for _, s := range servers {
dao.Cache.Set(fmt.Sprintf("%s%d%s", model.CtxKeyServer, s.ID, s.Secret), s, cache.NoExpiration)
innerS := s
dao.ServerList[fmt.Sprintf("%d", innerS.ID)] = &innerS
}
}

View File

@@ -13,10 +13,7 @@ import (
func ServeRPC() {
server := grpc.NewServer()
pb.RegisterNezhaServiceServer(server, &rpcService.NezhaHandler{
Auth: &rpcService.AuthHandler{
ClientID: "naiba",
ClientSecret: "123456",
},
Auth: &rpcService.AuthHandler{},
})
listen, err := net.Listen("tcp", ":5555")
if err != nil {