Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["server.go"],
importpath = "go-common/app/service/main/broadcast/server/grpc",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/main/broadcast/api/grpc/v1:go_default_library",
"//app/service/main/broadcast/service:go_default_library",
"//library/conf/paladin:go_default_library",
"//library/net/rpc/warden:go_default_library",
"@org_golang_google_grpc//:go_default_library",
"@org_golang_google_grpc//encoding/gzip:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,94 @@
// Package server generate by warden_gen
package server
import (
"context"
"net"
pb "go-common/app/service/main/broadcast/api/grpc/v1"
"go-common/app/service/main/broadcast/service"
"go-common/library/conf/paladin"
"go-common/library/net/rpc/warden"
"google.golang.org/grpc"
// use gzip decoder
_ "google.golang.org/grpc/encoding/gzip"
)
// New Zerg warden rpc server
func New(svr *service.Service) (*warden.Server, string) {
var rc struct {
Server *warden.ServerConfig
}
if err := paladin.Get("grpc.toml").UnmarshalTOML(&rc); err != nil {
panic(err)
}
_, port, _ := net.SplitHostPort(rc.Server.Addr)
ws := warden.NewServer(rc.Server, grpc.MaxRecvMsgSize(32*1024*1024), grpc.MaxSendMsgSize(32*1024*1024))
pb.RegisterZergServer(ws.Server(), &server{svr})
return ws, port
}
type server struct {
srv *service.Service
}
var _ pb.ZergServer = &server{}
// Ping Service
func (s *server) Ping(ctx context.Context, req *pb.PingReq) (*pb.PingReply, error) {
return &pb.PingReply{}, nil
}
// Close Service
func (s *server) Close(ctx context.Context, req *pb.CloseReq) (*pb.CloseReply, error) {
return &pb.CloseReply{}, nil
}
// Connect connect a conn.
func (s *server) Connect(ctx context.Context, req *pb.ConnectReq) (*pb.ConnectReply, error) {
mid, key, room, platform, accepts, err := s.srv.Connect(ctx, req.Server, req.ServerKey, req.Cookie, req.Token)
if err != nil {
return &pb.ConnectReply{}, err
}
return &pb.ConnectReply{Mid: mid, Key: key, RoomID: room, Accepts: accepts, Platform: platform}, nil
}
// Disconnect disconnect a conn.
func (s *server) Disconnect(ctx context.Context, req *pb.DisconnectReq) (*pb.DisconnectReply, error) {
has, err := s.srv.Disconnect(ctx, req.Mid, req.Key, req.Server)
if err != nil {
return &pb.DisconnectReply{}, err
}
return &pb.DisconnectReply{Has: has}, nil
}
// Heartbeat beartbeat a conn.
func (s *server) Heartbeat(ctx context.Context, req *pb.HeartbeatReq) (*pb.HeartbeatReply, error) {
if err := s.srv.Heartbeat(ctx, req.Mid, req.Key, req.Server); err != nil {
return &pb.HeartbeatReply{}, err
}
return &pb.HeartbeatReply{}, nil
}
// RenewOnline renew server online.
func (s *server) RenewOnline(ctx context.Context, req *pb.OnlineReq) (*pb.OnlineReply, error) {
roomCount, err := s.srv.RenewOnline(ctx, req.Server, req.Sharding, req.RoomCount)
if err != nil {
return &pb.OnlineReply{}, err
}
return &pb.OnlineReply{RoomCount: roomCount}, nil
}
// Receive receive a message.
func (s *server) Receive(ctx context.Context, req *pb.ReceiveReq) (*pb.ReceiveReply, error) {
if err := s.srv.Receive(ctx, req.Mid, req.Proto); err != nil {
return &pb.ReceiveReply{}, err
}
return &pb.ReceiveReply{}, nil
}
// ServerList return server list.
func (s *server) ServerList(ctx context.Context, req *pb.ServerListReq) (*pb.ServerListReply, error) {
return s.srv.ServerList(ctx, req.Platform), nil
}

View File

@@ -0,0 +1,44 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = [
"conn.go",
"http.go",
"online.go",
"push.go",
"server.go",
],
importpath = "go-common/app/service/main/broadcast/server/http",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/main/broadcast/api/grpc/v1:go_default_library",
"//app/service/main/broadcast/service:go_default_library",
"//library/conf/paladin:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/net/http/blademaster/middleware/verify:go_default_library",
"@com_github_gogo_protobuf//proto:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,106 @@
package http
import (
"io/ioutil"
pb "go-common/app/service/main/broadcast/api/grpc/v1"
"go-common/library/ecode"
bm "go-common/library/net/http/blademaster"
"github.com/gogo/protobuf/proto"
)
func connect(ctx *bm.Context) {
query := ctx.Request.URL.Query()
if token, err := c.Get("httpToken").String(); err != nil || token != query.Get("token") {
ctx.Protobuf(nil, ecode.Unauthorized)
return
}
b, err := ioutil.ReadAll(ctx.Request.Body)
if err != nil {
ctx.Protobuf(nil, err)
return
}
var req pb.ConnectReq
if err = proto.Unmarshal(b, &req); err != nil {
ctx.Protobuf(nil, err)
return
}
mid, key, room, platform, accepts, err := srv.Connect(ctx, req.Server, req.ServerKey, req.Cookie, req.Token)
if err != nil {
ctx.Protobuf(nil, err)
return
}
ctx.Protobuf(&pb.ConnectReply{Mid: mid, Key: key, RoomID: room, Accepts: accepts, Platform: platform}, nil)
}
func disconnect(ctx *bm.Context) {
query := ctx.Request.URL.Query()
if token, err := c.Get("httpToken").String(); err != nil || token != query.Get("token") {
ctx.Protobuf(nil, ecode.Unauthorized)
return
}
b, err := ioutil.ReadAll(ctx.Request.Body)
if err != nil {
ctx.Protobuf(nil, err)
return
}
var req pb.DisconnectReq
if err = proto.Unmarshal(b, &req); err != nil {
ctx.Protobuf(nil, err)
return
}
has, err := srv.Disconnect(ctx, req.Mid, req.Key, req.Server)
if err != nil {
ctx.Protobuf(nil, err)
return
}
ctx.Protobuf(&pb.DisconnectReply{Has: has}, nil)
}
func heartbeat(ctx *bm.Context) {
query := ctx.Request.URL.Query()
if token, err := c.Get("httpToken").String(); err != nil || token != query.Get("token") {
ctx.Protobuf(nil, ecode.Unauthorized)
return
}
b, err := ioutil.ReadAll(ctx.Request.Body)
if err != nil {
ctx.Protobuf(nil, err)
return
}
var req pb.HeartbeatReq
if err = proto.Unmarshal(b, &req); err != nil {
ctx.Protobuf(nil, err)
return
}
if err := srv.Heartbeat(ctx, req.Mid, req.Key, req.Server); err != nil {
ctx.Protobuf(nil, err)
return
}
ctx.Protobuf(&pb.HeartbeatReply{}, nil)
}
func renewOnline(ctx *bm.Context) {
query := ctx.Request.URL.Query()
if token, err := c.Get("httpToken").String(); err != nil || token != query.Get("token") {
ctx.Protobuf(nil, ecode.Unauthorized)
return
}
b, err := ioutil.ReadAll(ctx.Request.Body)
if err != nil {
ctx.Protobuf(nil, err)
return
}
var req pb.OnlineReq
if err = proto.Unmarshal(b, &req); err != nil {
ctx.Protobuf(nil, err)
return
}
roomCount, err := srv.RenewOnline(ctx, req.Server, req.Sharding, req.RoomCount)
if err != nil {
ctx.Protobuf(nil, err)
return
}
ctx.Protobuf(&pb.OnlineReply{RoomCount: roomCount}, nil)
}

View File

@@ -0,0 +1,80 @@
package http
import (
"net/http"
"go-common/app/service/main/broadcast/service"
"go-common/library/conf/paladin"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
"go-common/library/net/http/blademaster/middleware/verify"
)
var (
c paladin.Map
srv *service.Service
verifySvr *verify.Verify
)
// Init init.
func Init(s *service.Service) {
var hc struct {
Server *bm.ServerConfig
}
if err := paladin.Get("http.toml").UnmarshalTOML(&hc); err != nil {
if err != paladin.ErrNotExist {
panic(err)
}
}
if err := paladin.Watch("application.toml", &c); err != nil {
panic(err)
}
srv = s
verifySvr = verify.New(nil)
engine := bm.DefaultServer(hc.Server)
outerRouter(engine)
interRouter(engine)
if err := engine.Start(); err != nil {
log.Error("xhttp.Serve2 error(%v)", err)
panic(err)
}
}
func outerRouter(e *bm.Engine) {
g := e.Group("/x/broadcast")
{
g.POST("/conn/connect", connect)
g.POST("/conn/disconnect", disconnect)
g.POST("/conn/heartbeat", heartbeat)
g.POST("/online/renew", renewOnline)
}
}
func interRouter(e *bm.Engine) {
e.Ping(ping)
e.Register(register)
g := e.Group("/x/internal/broadcast")
{
g.POST("/push/keys", verifySvr.Verify, pushKeys)
g.POST("/push/mids", verifySvr.Verify, pushMids)
g.POST("/push/room", verifySvr.Verify, pushRoom)
g.POST("/push/all", verifySvr.Verify, pushAll)
g.GET("/online/top", onlineTop)
g.GET("/online/room", onlineRoom)
g.GET("/online/total", onlineTotal)
g.GET("/server/list", serverList)
g.GET("/server/infos", serverInfos)
g.GET("/server/weight", serverWeight)
}
}
func ping(c *bm.Context) {
if err := srv.Ping(c); err != nil {
log.Error("broadcast-service ping error(%v)", err)
c.AbortWithStatus(http.StatusServiceUnavailable)
}
}
func register(c *bm.Context) {
c.JSON(map[string]interface{}{}, nil)
}

View File

@@ -0,0 +1,31 @@
package http
import bm "go-common/library/net/http/blademaster"
func onlineTop(c *bm.Context) {
v := new(struct {
Business string `form:"business" validate:"required"`
Num int `form:"num" validate:"required"`
})
if err := c.Bind(v); err != nil {
return
}
c.JSON(srv.OnlineTop(c, v.Business, v.Num))
}
func onlineRoom(c *bm.Context) {
v := new(struct {
Business string `form:"business" validate:"required"`
Rooms []string `form:"rooms" validate:"required"`
})
if err := c.Bind(v); err != nil {
return
}
c.JSON(srv.OnlineRoom(c, v.Business, v.Rooms))
}
func onlineTotal(c *bm.Context) {
res := make(map[string]int64)
res["ip_count"], res["conn_count"] = srv.OnlineTotal(c)
c.JSON(res, nil)
}

View File

@@ -0,0 +1,76 @@
package http
import (
"go-common/library/ecode"
bm "go-common/library/net/http/blademaster"
)
const maxMsgSize = 1024 * 1024 * 8
func pushKeys(c *bm.Context) {
v := new(struct {
Op int32 `form:"operation" validate:"required"`
Keys []string `form:"keys,split" validate:"required"`
Msg string `form:"message" validate:"required"`
ContentType int32 `form:"content_type"`
})
if err := c.Bind(v); err != nil {
return
}
if len(v.Msg) > maxMsgSize {
c.JSON(ecode.FileTooLarge, nil)
}
c.JSON(nil, srv.PushKeys(c, v.Op, v.Keys, v.Msg, v.ContentType))
}
func pushMids(c *bm.Context) {
v := new(struct {
Op int32 `form:"operation" validate:"required"`
Mids []int64 `form:"mids,split" validate:"required"`
Msg string `form:"message" validate:"required"`
ContentType int32 `form:"content_type"`
})
if err := c.Bind(v); err != nil {
return
}
if len(v.Msg) > maxMsgSize {
c.JSON(nil, ecode.FileTooLarge)
return
}
c.JSON(nil, srv.PushMids(c, v.Op, v.Mids, v.Msg, v.ContentType))
}
func pushRoom(c *bm.Context) {
v := new(struct {
Op int32 `form:"operation" validate:"required"`
Room string `form:"room" validate:"required"`
Msg string `form:"message" validate:"required"`
ContentType int32 `form:"content_type"`
})
if err := c.Bind(v); err != nil {
return
}
if len(v.Msg) > maxMsgSize {
c.JSON(nil, ecode.FileTooLarge)
return
}
c.JSON(nil, srv.PushRoom(c, v.Op, v.Room, v.Msg, v.ContentType))
}
func pushAll(c *bm.Context) {
v := new(struct {
Op int32 `form:"operation" validate:"required"`
Speed int32 `form:"speed" validate:"min=0"`
Msg string `form:"message" validate:"required"`
Platform string `form:"platform"`
ContentType int32 `form:"content_type"`
})
if err := c.Bind(v); err != nil {
return
}
if len(v.Msg) > maxMsgSize {
c.JSON(nil, ecode.FileTooLarge)
return
}
c.JSON(nil, srv.PushAll(c, v.Op, v.Speed, v.Msg, v.Platform, v.ContentType))
}

View File

@@ -0,0 +1,38 @@
package http
import (
bm "go-common/library/net/http/blademaster"
)
func serverInfos(c *bm.Context) {
c.JSON(srv.ServerInfos(c))
}
func serverList(c *bm.Context) {
v := new(struct {
Platform string `form:"platform" validate:"required"`
})
if err := c.Bind(v); err != nil {
return
}
c.JSON(srv.ServerList(c, v.Platform), nil)
}
func serverWeight(c *bm.Context) {
v := new(struct {
IP string `form:"ip"`
})
if err := c.Bind(v); err != nil {
return
}
nodes, region, province, err := srv.ServerWeight(c, v.IP)
if err != nil {
c.JSON(nil, err)
return
}
data := make(map[string]interface{})
data["nodes"] = nodes
data["region"] = region
data["province"] = province
c.JSON(data, err)
}