init
This commit is contained in:
36
comet/http/result.go
Normal file
36
comet/http/result.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package http
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
const (
|
||||
// OK ok
|
||||
OK = 0
|
||||
// RequestErr request error
|
||||
RequestErr = -400
|
||||
// ServerErr server error
|
||||
ServerErr = -500
|
||||
|
||||
contextErrCode = "context/err/code"
|
||||
)
|
||||
|
||||
type resp struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
func errors(c *gin.Context, code int, msg string) {
|
||||
c.Set(contextErrCode, code)
|
||||
c.JSON(200, resp{
|
||||
Code: code,
|
||||
Message: msg,
|
||||
})
|
||||
}
|
||||
|
||||
func result(c *gin.Context, data interface{}, code int) {
|
||||
c.Set(contextErrCode, code)
|
||||
c.JSON(200, resp{
|
||||
Code: code,
|
||||
Data: data,
|
||||
})
|
||||
}
|
||||
46
comet/http/server.go
Normal file
46
comet/http/server.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"github.com/gin-contrib/pprof"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/rs/zerolog/log"
|
||||
"gitlab.33.cn/chat/im/comet"
|
||||
"gitlab.33.cn/chat/im/comet/conf"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var (
|
||||
srv *comet.Comet
|
||||
)
|
||||
|
||||
func Start(addr string, s *comet.Comet) *http.Server {
|
||||
srv = s
|
||||
gin.ForceConsoleColor()
|
||||
switch conf.Conf.Env {
|
||||
case conf.DebugMode:
|
||||
gin.SetMode(gin.DebugMode)
|
||||
case conf.ReleaseMode:
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
engine := gin.Default()
|
||||
SetupEngine(engine)
|
||||
pprof.Register(engine)
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: addr,
|
||||
Handler: engine,
|
||||
}
|
||||
go func() {
|
||||
// service connections
|
||||
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
log.Fatal().Err(err).Msg("http listen failed")
|
||||
}
|
||||
}()
|
||||
return srv
|
||||
}
|
||||
|
||||
func SetupEngine(e *gin.Engine) *gin.Engine {
|
||||
e.GET("/statics", Statics)
|
||||
e.GET("/groupDetails", GroupDetails)
|
||||
return e
|
||||
}
|
||||
75
comet/http/statics.go
Normal file
75
comet/http/statics.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package http
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func Statics(c *gin.Context) {
|
||||
var arg struct {
|
||||
Drop bool `form:"isDrop"`
|
||||
}
|
||||
if err := c.BindQuery(&arg); err != nil {
|
||||
errors(c, RequestErr, err.Error())
|
||||
return
|
||||
}
|
||||
res := map[string]interface{}{
|
||||
"buckets": groupsInfo(arg.Drop),
|
||||
}
|
||||
result(c, res, OK)
|
||||
}
|
||||
|
||||
func groupsInfo(isDrop bool) []map[string]interface{} {
|
||||
var res = make([]map[string]interface{}, len(srv.Buckets()))
|
||||
for i, bucket := range srv.Buckets() {
|
||||
if !isDrop {
|
||||
if bucket.GroupCount() == 0 {
|
||||
continue
|
||||
}
|
||||
if len(bucket.GroupsCount()) == 0 {
|
||||
continue
|
||||
}
|
||||
}
|
||||
item := map[string]interface{}{
|
||||
"counts": bucket.GroupCount(),
|
||||
"group-members": bucket.GroupsCount(),
|
||||
}
|
||||
res[i] = item
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func GroupDetails(c *gin.Context) {
|
||||
var arg struct {
|
||||
Groups []string `form:"groups" binding:"required"`
|
||||
}
|
||||
if err := c.BindQuery(&arg); err != nil {
|
||||
errors(c, RequestErr, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
var groups = make([]interface{}, 0)
|
||||
for _, gid := range arg.Groups {
|
||||
gInfo := map[string]interface{}{
|
||||
"gid": gid,
|
||||
"members": groupsMembers(gid),
|
||||
}
|
||||
groups = append(groups, gInfo)
|
||||
}
|
||||
res := map[string]interface{}{
|
||||
"groups": groups,
|
||||
}
|
||||
result(c, res, OK)
|
||||
}
|
||||
|
||||
func groupsMembers(gid string) map[string]string {
|
||||
members := make(map[string]string, 0)
|
||||
for _, bucket := range srv.Buckets() {
|
||||
g := bucket.Group(gid)
|
||||
if g == nil {
|
||||
continue
|
||||
}
|
||||
mems, ips := g.Members()
|
||||
for i, mem := range mems {
|
||||
members[mem] = ips[i]
|
||||
}
|
||||
}
|
||||
return members
|
||||
}
|
||||
Reference in New Issue
Block a user