Compare commits
6 Commits
ec4c3c0413
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 127cddc5e1 | |||
| 5b3143bf80 | |||
| a0968db345 | |||
| 82c4e67742 | |||
| 14cb2f3270 | |||
| fc9b32b115 |
24
app/app.go
24
app/app.go
@@ -2,12 +2,12 @@ package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.hlsq.asia/mmorpg/service-common/discover/common"
|
||||
"git.hlsq.asia/mmorpg/service-common/log"
|
||||
"git.hlsq.asia/mmorpg/service-common/module"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/config"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/internal/grpc_server"
|
||||
"git.hlsq.asia/alex/service-common/discover/common"
|
||||
"git.hlsq.asia/alex/service-common/log"
|
||||
"git.hlsq.asia/alex/service-common/module"
|
||||
"git.hlsq.asia/alex/service-gateway/config"
|
||||
"git.hlsq.asia/alex/service-gateway/internal/global"
|
||||
"git.hlsq.asia/alex/service-gateway/internal/grpcserver"
|
||||
"github.com/judwhite/go-svc"
|
||||
"sync"
|
||||
)
|
||||
@@ -17,17 +17,15 @@ type Program struct {
|
||||
}
|
||||
|
||||
func (p *Program) Init(_ svc.Environment) error {
|
||||
if err := config.LoadConfig(); err != nil {
|
||||
return err
|
||||
}
|
||||
p.moduleList = append(p.moduleList, &module.Base{Log: config.Get().Log})
|
||||
p.moduleList = append(p.moduleList, &module.Log{Cfg: config.Get().Log})
|
||||
p.moduleList = append(p.moduleList, &module.DB{Cfg: config.Get().DB, AppName: config.Get().App.Name})
|
||||
p.moduleList = append(p.moduleList, &module.Snowflake{})
|
||||
p.moduleList = append(p.moduleList, &ModuleWebServer{})
|
||||
p.moduleList = append(p.moduleList, &ModuleWebsocketServer{})
|
||||
p.moduleList = append(p.moduleList, &ModuleLoginQueue{})
|
||||
p.moduleList = append(p.moduleList, &module.Grpc{Server: grpc_server.NewServer(config.Get().Serve.Grpc)})
|
||||
p.moduleList = append(p.moduleList, &module.Prometheus{MetricCfg: config.Get().Metric})
|
||||
p.moduleList = append(p.moduleList, &module.Tracer{MetricCfg: config.Get().Metric, ServiceName: common.KeyDiscoverServiceNameGateway})
|
||||
p.moduleList = append(p.moduleList, &module.Grpc{Server: grpcserver.NewServer(config.Get().Serve.Grpc)})
|
||||
p.moduleList = append(p.moduleList, &module.Prometheus{Cfg: config.Get().Metric})
|
||||
p.moduleList = append(p.moduleList, &module.Tracer{Cfg: config.Get().Metric, ServiceName: common.KeyDiscoverServiceNameGateway})
|
||||
p.moduleList = append(p.moduleList, &module.Discover{})
|
||||
|
||||
for _, m := range p.moduleList {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"git.hlsq.asia/mmorpg/service-common/module"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/ws_handler/login"
|
||||
"git.hlsq.asia/alex/service-common/module"
|
||||
"git.hlsq.asia/alex/service-gateway/internal/global"
|
||||
"git.hlsq.asia/alex/service-gateway/internal/handler/wshandler/login"
|
||||
"runtime"
|
||||
"sync"
|
||||
)
|
||||
|
||||
11
app/web.go
11
app/web.go
@@ -4,10 +4,10 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.hlsq.asia/mmorpg/service-common/log"
|
||||
"git.hlsq.asia/mmorpg/service-common/module"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/config"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/internal/net/http_gateway"
|
||||
"git.hlsq.asia/alex/service-common/log"
|
||||
"git.hlsq.asia/alex/service-common/module"
|
||||
"git.hlsq.asia/alex/service-gateway/config"
|
||||
"git.hlsq.asia/alex/service-gateway/internal/net/httpgateway"
|
||||
"net/http"
|
||||
"sync"
|
||||
)
|
||||
@@ -30,9 +30,10 @@ func (m *ModuleWebServer) Start(ready *sync.WaitGroup) error {
|
||||
defer m.wg.Done()
|
||||
m.server = &http.Server{
|
||||
Addr: fmt.Sprintf("%v:%v", config.Get().Serve.Http.Address, config.Get().Serve.Http.Port),
|
||||
Handler: http_gateway.InitRouter(),
|
||||
Handler: httpgateway.InitRouter(),
|
||||
}
|
||||
ready.Done()
|
||||
log.Infof("http server start, addr: %v", m.server.Addr)
|
||||
if err := m.server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
log.Errorf("http server failed: %v", err.Error())
|
||||
}
|
||||
|
||||
@@ -2,11 +2,11 @@ package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.hlsq.asia/mmorpg/service-common/log"
|
||||
"git.hlsq.asia/mmorpg/service-common/module"
|
||||
"git.hlsq.asia/mmorpg/service-common/net/socket/websocket"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/config"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/internal/net/ws_gateway"
|
||||
"git.hlsq.asia/alex/service-common/log"
|
||||
"git.hlsq.asia/alex/service-common/module"
|
||||
"git.hlsq.asia/alex/service-common/net/socket/websocket"
|
||||
"git.hlsq.asia/alex/service-gateway/config"
|
||||
"git.hlsq.asia/alex/service-gateway/internal/net/wsgateway"
|
||||
"github.com/panjf2000/gnet/v2"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -22,7 +22,7 @@ type ModuleWebsocketServer struct {
|
||||
func (m *ModuleWebsocketServer) Init() error {
|
||||
m.wg = &sync.WaitGroup{}
|
||||
m.server = websocket.NewWSServer(
|
||||
&ws_gateway.GatewayWsServer{},
|
||||
&wsgateway.GatewayWsServer{},
|
||||
log.GetLogger().Named("ws_server"),
|
||||
5*time.Second,
|
||||
)
|
||||
@@ -34,6 +34,7 @@ func (m *ModuleWebsocketServer) Start(ready *sync.WaitGroup) error {
|
||||
go func() {
|
||||
defer m.wg.Done()
|
||||
ready.Done()
|
||||
log.Infof("websocket server start, port: %v", config.Get().Serve.Socket.Web.Port)
|
||||
_ = m.server.Run(
|
||||
fmt.Sprintf("tcp4://0.0.0.0:%v", config.Get().Serve.Socket.Web.Port),
|
||||
true,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package config
|
||||
|
||||
import "git.hlsq.asia/mmorpg/service-common/config"
|
||||
import "git.hlsq.asia/alex/service-common/config"
|
||||
|
||||
const path = "./config"
|
||||
|
||||
|
||||
2
deploy/Jenkinsfile
vendored
2
deploy/Jenkinsfile
vendored
@@ -3,7 +3,7 @@ pipeline {
|
||||
|
||||
environment {
|
||||
// 仓库
|
||||
REPO_URL = 'https://git.hlsq.asia/mmorpg/service-gateway.git'
|
||||
REPO_URL = 'https://git.hlsq.asia/alex/service-gateway.git'
|
||||
REPO_CREDENTIALS_ID = '80805ba2-f4ac-4d84-aee6-d4cce5fc0a96'
|
||||
|
||||
// Registry
|
||||
|
||||
4
go.mod
4
go.mod
@@ -1,10 +1,10 @@
|
||||
module git.hlsq.asia/mmorpg/service-gateway
|
||||
module git.hlsq.asia/alex/service-gateway
|
||||
|
||||
go 1.24.0
|
||||
|
||||
require (
|
||||
bou.ke/monkey v1.0.2
|
||||
git.hlsq.asia/mmorpg/service-common v0.0.0-20260206145147-5a0f4b71d430
|
||||
git.hlsq.asia/alex/service-common master
|
||||
github.com/alicebob/miniredis/v2 v2.35.0
|
||||
github.com/gin-contrib/cors v1.7.6
|
||||
github.com/gin-gonic/gin v1.11.0
|
||||
|
||||
6
go.sum
6
go.sum
@@ -2,8 +2,10 @@ bou.ke/monkey v1.0.2 h1:kWcnsrCNUatbxncxR/ThdYqbytgOIArtYWqcQLQzKLI=
|
||||
bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA=
|
||||
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
||||
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||
git.hlsq.asia/mmorpg/service-common v0.0.0-20260206145147-5a0f4b71d430 h1:8aNIgKpU6HBg7h4ENjuGWshap+VxeKUmLE0IvFoWbug=
|
||||
git.hlsq.asia/mmorpg/service-common v0.0.0-20260206145147-5a0f4b71d430/go.mod h1:mMhZcumphj6gaVTppVYsMTkd+5HupmQgAc53Pd4MH9I=
|
||||
git.hlsq.asia/alex/service-common v0.0.0-20260207051302-0ca8a0ccbb14 h1:4r3tNWRzGVY3Xx6UiGjJJnwoMoWlVqbyGrljxl5d/nQ=
|
||||
git.hlsq.asia/alex/service-common v0.0.0-20260207051302-0ca8a0ccbb14/go.mod h1:mMhZcumphj6gaVTppVYsMTkd+5HupmQgAc53Pd4MH9I=
|
||||
git.hlsq.asia/alex/service-common v0.0.0-20260211064821-558677bc3a2e h1:MNVS1NXNVn4DeTZPcvvA8YFM8afCzZedLPUYMffNgZs=
|
||||
git.hlsq.asia/alex/service-common v0.0.0-20260211064821-558677bc3a2e/go.mod h1:mMhZcumphj6gaVTppVYsMTkd+5HupmQgAc53Pd4MH9I=
|
||||
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
|
||||
github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/IBM/sarama v1.46.3 h1:njRsX6jNlnR+ClJ8XmkO+CM4unbrNr/2vB5KK6UA+IE=
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
package grpc_server
|
||||
package grpcserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.hlsq.asia/mmorpg/service-common/log"
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/ss/ss_pb"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/ws_handler/client"
|
||||
"git.hlsq.asia/alex/service-common/log"
|
||||
"git.hlsq.asia/alex/service-common/proto/rs/rspb"
|
||||
"git.hlsq.asia/alex/service-common/proto/ss/sspb"
|
||||
"git.hlsq.asia/alex/service-gateway/internal/global"
|
||||
"git.hlsq.asia/alex/service-gateway/internal/handler/wshandler/client"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func (s *Server) ToClient(server grpc_pb.Gateway_ToClientServer) error {
|
||||
func (s *Server) ToClient(server rspb.Gateway_ToClientServer) error {
|
||||
wg := &sync.WaitGroup{}
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
@@ -32,8 +32,8 @@ func (s *Server) ToClient(server grpc_pb.Gateway_ToClientServer) error {
|
||||
// client.WriteBytes(ss_pb.MessageID(args.MessageID), args.Payload)
|
||||
//})
|
||||
|
||||
data, err := proto.Marshal(&ss_pb.Message{
|
||||
ID: ss_pb.MessageID(args.MessageID),
|
||||
data, err := proto.Marshal(&sspb.Message{
|
||||
ID: sspb.MessageID(args.MessageID),
|
||||
Payload: args.Payload,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -51,7 +51,7 @@ func (s *Server) ToClient(server grpc_pb.Gateway_ToClientServer) error {
|
||||
//}
|
||||
} else {
|
||||
if cli := client.UserMgr.GetByUSN(args.USN); cli != nil {
|
||||
cli.WriteBytes(ss_pb.MessageID(args.MessageID), args.Payload)
|
||||
cli.WriteBytes(sspb.MessageID(args.MessageID), args.Payload)
|
||||
global.FlowCounter.Add(float64(len(args.Payload)))
|
||||
}
|
||||
}
|
||||
@@ -59,12 +59,12 @@ func (s *Server) ToClient(server grpc_pb.Gateway_ToClientServer) error {
|
||||
}
|
||||
}()
|
||||
wg.Wait()
|
||||
return server.SendAndClose(&grpc_pb.ToClientResp{})
|
||||
return server.SendAndClose(&rspb.ToClientResp{})
|
||||
}
|
||||
|
||||
func (s *Server) KickUser(ctx context.Context, req *grpc_pb.KickUserReq) (*grpc_pb.KickUserResp, error) {
|
||||
func (s *Server) KickUser(ctx context.Context, req *rspb.KickUserReq) (*rspb.KickUserResp, error) {
|
||||
if cli := client.UserMgr.GetByUSN(req.USN); cli != nil {
|
||||
cli.CloseClient()
|
||||
}
|
||||
return &grpc_pb.KickUserResp{}, nil
|
||||
return &rspb.KickUserResp{}, nil
|
||||
}
|
||||
@@ -1,16 +1,16 @@
|
||||
package grpc_server
|
||||
package grpcserver
|
||||
|
||||
import (
|
||||
"git.hlsq.asia/mmorpg/service-common/config"
|
||||
"git.hlsq.asia/mmorpg/service-common/discover/common"
|
||||
"git.hlsq.asia/mmorpg/service-common/net/grpc/service"
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
||||
"git.hlsq.asia/alex/service-common/config"
|
||||
"git.hlsq.asia/alex/service-common/discover/common"
|
||||
"git.hlsq.asia/alex/service-common/net/grpc/service"
|
||||
"git.hlsq.asia/alex/service-common/proto/rs/rspb"
|
||||
"git.hlsq.asia/alex/service-gateway/internal/global"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
grpc_pb.UnimplementedGatewayServer
|
||||
rspb.UnimplementedGatewayServer
|
||||
service.Base
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ func (s *Server) OnCustomGrpcServerOption() []grpc.ServerOption {
|
||||
|
||||
func (s *Server) OnInit(serve *grpc.Server) {
|
||||
global.GatewaySID = s.SID
|
||||
grpc_pb.RegisterGatewayServer(serve, s)
|
||||
rspb.RegisterGatewayServer(serve, s)
|
||||
}
|
||||
|
||||
func (s *Server) OnClose() {
|
||||
@@ -1,13 +1,13 @@
|
||||
package http_handler
|
||||
package httphandler
|
||||
|
||||
import (
|
||||
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
||||
"git.hlsq.asia/mmorpg/service-common/log"
|
||||
"git.hlsq.asia/mmorpg/service-common/net/grpc/grpc_client"
|
||||
"git.hlsq.asia/mmorpg/service-common/net/http/http_resp"
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
|
||||
"git.hlsq.asia/mmorpg/service-common/utils"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
||||
"git.hlsq.asia/alex/service-common/db/redis"
|
||||
"git.hlsq.asia/alex/service-common/log"
|
||||
"git.hlsq.asia/alex/service-common/net/grpc/grpcclient"
|
||||
"git.hlsq.asia/alex/service-common/net/http/httpresp"
|
||||
"git.hlsq.asia/alex/service-common/proto/rs/rspb"
|
||||
"git.hlsq.asia/alex/service-common/utils"
|
||||
"git.hlsq.asia/alex/service-gateway/internal/global"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@@ -29,51 +29,51 @@ type LoginResp struct {
|
||||
func Login(c *gin.Context) {
|
||||
req := &LoginReq{}
|
||||
if err := c.ShouldBindJSON(req); err != nil {
|
||||
http_resp.JsonBadRequest(c)
|
||||
httpresp.JsonBadRequest(c)
|
||||
return
|
||||
}
|
||||
client, err := grpc_client.UserNewClientLB()
|
||||
client, err := grpcclient.UserNewClientLB()
|
||||
if err != nil {
|
||||
log.Errorf("Login UserNewClientLB error: %v", err)
|
||||
http_resp.JsonOK(c, http_resp.Error(http_resp.Failed))
|
||||
httpresp.JsonOK(c, httpresp.Error(httpresp.Failed))
|
||||
return
|
||||
}
|
||||
|
||||
usn, name := int64(0), ""
|
||||
if req.Phone != "" {
|
||||
// TODO 校验验证码
|
||||
login, err := client.PhoneLogin(c, &grpc_pb.PhoneLoginReq{
|
||||
login, err := client.PhoneLogin(c, &rspb.PhoneLoginReq{
|
||||
Phone: req.Phone,
|
||||
Code: req.Code,
|
||||
})
|
||||
if err != nil {
|
||||
log.Errorf("Login PhoneLogin error: %v", err)
|
||||
http_resp.JsonOK(c, http_resp.Error(http_resp.Failed))
|
||||
httpresp.JsonOK(c, httpresp.Error(httpresp.Failed))
|
||||
return
|
||||
}
|
||||
usn, name = login.USN, login.Name
|
||||
} else if req.WxMiniCode != "" {
|
||||
login, err := client.WxMiniLogin(c, &grpc_pb.WxMiniLoginReq{
|
||||
login, err := client.WxMiniLogin(c, &rspb.WxMiniLoginReq{
|
||||
Code: req.WxMiniCode,
|
||||
})
|
||||
if err != nil {
|
||||
log.Errorf("Login WxMiniLogin error: %v", err)
|
||||
http_resp.JsonOK(c, http_resp.Error(http_resp.Failed))
|
||||
httpresp.JsonOK(c, httpresp.Error(httpresp.Failed))
|
||||
return
|
||||
}
|
||||
usn, name = login.USN, login.Name
|
||||
} else {
|
||||
http_resp.JsonBadRequest(c)
|
||||
httpresp.JsonBadRequest(c)
|
||||
return
|
||||
}
|
||||
|
||||
at, rt, err := sessionLogin(c, usn)
|
||||
if err != nil {
|
||||
log.Errorf("Login sessionLogin error: %v, usn: %v", err, usn)
|
||||
http_resp.JsonOK(c, http_resp.Error(http_resp.Failed))
|
||||
httpresp.JsonOK(c, httpresp.Error(httpresp.Failed))
|
||||
return
|
||||
}
|
||||
http_resp.JsonOK(c, http_resp.Success(&LoginResp{
|
||||
httpresp.JsonOK(c, httpresp.Success(&LoginResp{
|
||||
USN: usn,
|
||||
Name: name,
|
||||
AccessToken: at,
|
||||
@@ -93,13 +93,13 @@ type RefreshTokenResp struct {
|
||||
func RefreshToken(c *gin.Context) {
|
||||
req := &RefreshTokenReq{}
|
||||
if err := c.ShouldBindJSON(req); err != nil {
|
||||
http_resp.JsonBadRequest(c)
|
||||
httpresp.JsonBadRequest(c)
|
||||
return
|
||||
}
|
||||
if req.RefreshToken == "" {
|
||||
cookie, err := c.Cookie("refresh_token")
|
||||
if err != nil {
|
||||
http_resp.JsonUnauthorized(c)
|
||||
httpresp.JsonUnauthorized(c)
|
||||
return
|
||||
}
|
||||
req.RefreshToken = cookie
|
||||
@@ -107,7 +107,7 @@ func RefreshToken(c *gin.Context) {
|
||||
|
||||
usn, _ := redis.GetClient().HGet(c, global.KeyGatewayRefreshToken+req.RefreshToken, (&utils.UserSession{}).GetUsnKey()).Int64()
|
||||
if usn == 0 {
|
||||
http_resp.JsonUnauthorized(c)
|
||||
httpresp.JsonUnauthorized(c)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -117,11 +117,11 @@ func RefreshToken(c *gin.Context) {
|
||||
at, rt, err := sessionLogin(c, usn)
|
||||
if err != nil {
|
||||
log.Errorf("RefreshToken sessionLogin error: %v, usn: %v", err, usn)
|
||||
http_resp.JsonOK(c, http_resp.Error(http_resp.Failed))
|
||||
httpresp.JsonOK(c, httpresp.Error(httpresp.Failed))
|
||||
return
|
||||
}
|
||||
|
||||
http_resp.JsonOK(c, http_resp.Success(&RefreshTokenResp{
|
||||
httpresp.JsonOK(c, httpresp.Success(&RefreshTokenResp{
|
||||
AccessToken: at,
|
||||
RefreshToken: rt,
|
||||
}))
|
||||
@@ -137,13 +137,13 @@ type LogoutResp struct {
|
||||
func Logout(c *gin.Context) {
|
||||
req := &LogoutReq{}
|
||||
if err := c.ShouldBindJSON(req); err != nil {
|
||||
http_resp.JsonBadRequest(c)
|
||||
httpresp.JsonBadRequest(c)
|
||||
return
|
||||
}
|
||||
if req.RefreshToken == "" {
|
||||
cookie, err := c.Cookie("refresh_token")
|
||||
if err != nil {
|
||||
http_resp.JsonUnauthorized(c)
|
||||
httpresp.JsonUnauthorized(c)
|
||||
return
|
||||
}
|
||||
req.RefreshToken = cookie
|
||||
@@ -151,5 +151,5 @@ func Logout(c *gin.Context) {
|
||||
if err := sessionLogout(c, req.RefreshToken); err != nil {
|
||||
log.Errorf("Logout sessionLogout error: %v", err)
|
||||
}
|
||||
http_resp.JsonOK(c, http_resp.Success(&LogoutResp{}))
|
||||
httpresp.JsonOK(c, httpresp.Success(&LogoutResp{}))
|
||||
}
|
||||
@@ -1,14 +1,14 @@
|
||||
package http_handler
|
||||
package httphandler
|
||||
|
||||
import (
|
||||
"bou.ke/monkey"
|
||||
"fmt"
|
||||
"git.hlsq.asia/mmorpg/service-common/net/grpc/grpc_client"
|
||||
"git.hlsq.asia/mmorpg/service-common/net/http/http_resp"
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb/mocks"
|
||||
"git.hlsq.asia/mmorpg/service-common/utils"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/internal/testutil"
|
||||
"git.hlsq.asia/alex/service-common/net/grpc/grpcclient"
|
||||
"git.hlsq.asia/alex/service-common/net/http/httpresp"
|
||||
"git.hlsq.asia/alex/service-common/proto/rs/rspb"
|
||||
"git.hlsq.asia/alex/service-common/proto/rs/rspb/mocks"
|
||||
"git.hlsq.asia/alex/service-common/utils"
|
||||
"git.hlsq.asia/alex/service-gateway/internal/testutil"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -30,21 +30,21 @@ func (ts *LoginTestSuite) TestLogin() {
|
||||
Code: "",
|
||||
})
|
||||
Login(c)
|
||||
utils.AssertResponse(&ts.Suite, w, http.StatusBadRequest, http_resp.ParamError)
|
||||
utils.AssertResponse(&ts.Suite, w, http.StatusBadRequest, httpresp.ParamError)
|
||||
})
|
||||
|
||||
ts.Run("UserNewClientLB Failed", func() {
|
||||
monkey.Patch(grpc_client.UserNewClientLB, func() (grpc_pb.UserClient, error) {
|
||||
monkey.Patch(grpcclient.UserNewClientLB, func() (rspb.UserClient, error) {
|
||||
return nil, assert.AnError
|
||||
})
|
||||
defer monkey.Unpatch(grpc_client.UserNewClientLB)
|
||||
defer monkey.Unpatch(grpcclient.UserNewClientLB)
|
||||
|
||||
w, c := utils.CreateTestContext("POST", "/", &LoginReq{
|
||||
Phone: "13800000000",
|
||||
Code: "1234",
|
||||
})
|
||||
Login(c)
|
||||
utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.Failed)
|
||||
utils.AssertResponse(&ts.Suite, w, http.StatusOK, httpresp.Failed)
|
||||
})
|
||||
|
||||
ts.Run("Login Failed", func() {
|
||||
@@ -55,17 +55,17 @@ func (ts *LoginTestSuite) TestLogin() {
|
||||
PhoneLogin(gomock.Any(), gomock.Any()).
|
||||
Return(nil, fmt.Errorf("gRPC failed"))
|
||||
|
||||
monkey.Patch(grpc_client.UserNewClientLB, func() (grpc_pb.UserClient, error) {
|
||||
monkey.Patch(grpcclient.UserNewClientLB, func() (rspb.UserClient, error) {
|
||||
return client, nil
|
||||
})
|
||||
defer monkey.Unpatch(grpc_client.UserNewClientLB)
|
||||
defer monkey.Unpatch(grpcclient.UserNewClientLB)
|
||||
|
||||
w, c := utils.CreateTestContext("POST", "/", &LoginReq{
|
||||
Phone: "13800000000",
|
||||
Code: "1234",
|
||||
})
|
||||
Login(c)
|
||||
utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.Failed)
|
||||
utils.AssertResponse(&ts.Suite, w, http.StatusOK, httpresp.Failed)
|
||||
})
|
||||
|
||||
ts.Run("OK", func() {
|
||||
@@ -74,19 +74,19 @@ func (ts *LoginTestSuite) TestLogin() {
|
||||
client := mocks.NewMockUserClient(ctrl)
|
||||
client.EXPECT().
|
||||
PhoneLogin(gomock.Any(), gomock.Any()).
|
||||
Return(&grpc_pb.PhoneLoginResp{USN: 1, Name: "hh"}, nil)
|
||||
Return(&rspb.PhoneLoginResp{USN: 1, Name: "hh"}, nil)
|
||||
|
||||
monkey.Patch(grpc_client.UserNewClientLB, func() (grpc_pb.UserClient, error) {
|
||||
monkey.Patch(grpcclient.UserNewClientLB, func() (rspb.UserClient, error) {
|
||||
return client, nil
|
||||
})
|
||||
defer monkey.Unpatch(grpc_client.UserNewClientLB)
|
||||
defer monkey.Unpatch(grpcclient.UserNewClientLB)
|
||||
|
||||
w, c := utils.CreateTestContext("POST", "/", &LoginReq{
|
||||
Phone: "13800000000",
|
||||
Code: "1234",
|
||||
})
|
||||
Login(c)
|
||||
utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.OK)
|
||||
utils.AssertResponse(&ts.Suite, w, http.StatusOK, httpresp.OK)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -98,7 +98,7 @@ func (ts *LoginTestSuite) TestRefreshToken() {
|
||||
RefreshToken: "",
|
||||
})
|
||||
RefreshToken(c)
|
||||
utils.AssertResponse(&ts.Suite, w, http.StatusBadRequest, http_resp.ParamError)
|
||||
utils.AssertResponse(&ts.Suite, w, http.StatusBadRequest, httpresp.ParamError)
|
||||
})
|
||||
|
||||
ts.Run("ParseToken Failed", func() {
|
||||
@@ -111,7 +111,7 @@ func (ts *LoginTestSuite) TestRefreshToken() {
|
||||
RefreshToken: "abc",
|
||||
})
|
||||
RefreshToken(c)
|
||||
utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.TokenInvalid)
|
||||
utils.AssertResponse(&ts.Suite, w, http.StatusOK, httpresp.TokenInvalid)
|
||||
})
|
||||
|
||||
//ts.Run("Redis Get Failed", func() {
|
||||
@@ -1,10 +1,10 @@
|
||||
package http_handler
|
||||
package httphandler
|
||||
|
||||
import (
|
||||
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
||||
"git.hlsq.asia/mmorpg/service-common/utils"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/config"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
||||
"git.hlsq.asia/alex/service-common/db/redis"
|
||||
"git.hlsq.asia/alex/service-common/utils"
|
||||
"git.hlsq.asia/alex/service-gateway/config"
|
||||
"git.hlsq.asia/alex/service-gateway/internal/global"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
@@ -3,10 +3,10 @@ package client
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
||||
"git.hlsq.asia/mmorpg/service-common/log"
|
||||
"git.hlsq.asia/mmorpg/service-common/net/socket"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
||||
"git.hlsq.asia/alex/service-common/db/redis"
|
||||
"git.hlsq.asia/alex/service-common/log"
|
||||
"git.hlsq.asia/alex/service-common/net/socket"
|
||||
"git.hlsq.asia/alex/service-gateway/internal/global"
|
||||
"go.uber.org/zap"
|
||||
"runtime/debug"
|
||||
"sync"
|
||||
@@ -3,11 +3,11 @@ package client
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
||||
"git.hlsq.asia/mmorpg/service-common/net/grpc/grpc_client"
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/ss/ss_pb"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
||||
"git.hlsq.asia/alex/service-common/db/redis"
|
||||
"git.hlsq.asia/alex/service-common/net/grpc/grpcclient"
|
||||
"git.hlsq.asia/alex/service-common/proto/rs/rspb"
|
||||
"git.hlsq.asia/alex/service-common/proto/ss/sspb"
|
||||
"git.hlsq.asia/alex/service-gateway/internal/global"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"time"
|
||||
)
|
||||
@@ -15,23 +15,23 @@ import (
|
||||
func (c *Client) handle(event Event) {
|
||||
switch e := event.(type) {
|
||||
case *ClientEvent:
|
||||
msg := &ss_pb.Message{}
|
||||
msg := &sspb.Message{}
|
||||
if err := proto.Unmarshal(e.Msg, msg); err != nil {
|
||||
c.logger.Errorf("handle event proto.Unmarshal err: %v", err)
|
||||
c.cancel()
|
||||
return
|
||||
}
|
||||
switch msg.ID {
|
||||
case ss_pb.MessageID_MESSAGE_ID_ENTER_INSTANCE:
|
||||
m := &ss_pb.C2S_EnterInstance{}
|
||||
case sspb.MessageID_MESSAGE_ID_ENTER_INSTANCE:
|
||||
m := &sspb.C2S_EnterInstance{}
|
||||
if err := proto.Unmarshal(msg.Payload, m); err != nil {
|
||||
c.logger.Errorf("handle event proto.Unmarshal err: %v", err)
|
||||
c.cancel()
|
||||
return
|
||||
}
|
||||
c.onEnter(m)
|
||||
case ss_pb.MessageID_MESSAGE_ID_ACTION:
|
||||
m := &ss_pb.C2S_Action{}
|
||||
case sspb.MessageID_MESSAGE_ID_ACTION:
|
||||
m := &sspb.C2S_Action{}
|
||||
if err := proto.Unmarshal(msg.Payload, m); err != nil {
|
||||
c.logger.Errorf("handle event proto.Unmarshal err: %v", err)
|
||||
c.cancel()
|
||||
@@ -46,20 +46,20 @@ func (c *Client) handle(event Event) {
|
||||
c.Status = 1
|
||||
UserMgr.Add(c.USN, c)
|
||||
redis.GetClient().HSet(c.ctx, fmt.Sprintf(global.KeyGatewayInfo, c.USN), global.HFieldInfoGatewaySID, global.GatewaySID)
|
||||
c.WriteMessage(ss_pb.MessageID_MESSAGE_ID_LOGIN_SUCCESS, &ss_pb.S2C_LoginSuccess{
|
||||
c.WriteMessage(sspb.MessageID_MESSAGE_ID_LOGIN_SUCCESS, &sspb.S2C_LoginSuccess{
|
||||
InstanceID: 1,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) onEnter(msg *ss_pb.C2S_EnterInstance) {
|
||||
client, err := grpc_client.SceneNewClientLB()
|
||||
func (c *Client) onEnter(msg *sspb.C2S_EnterInstance) {
|
||||
client, err := grpcclient.SceneNewClientLB()
|
||||
if err != nil {
|
||||
c.logger.Errorf("SceneNewClient err: %v", err)
|
||||
return
|
||||
}
|
||||
resp, err := client.Enter(c.ctx, &grpc_pb.EnterReq{
|
||||
resp, err := client.Enter(c.ctx, &rspb.EnterReq{
|
||||
USN: c.USN,
|
||||
GatewaySID: global.GatewaySID,
|
||||
InstanceID: msg.InstanceID,
|
||||
@@ -71,19 +71,19 @@ func (c *Client) onEnter(msg *ss_pb.C2S_EnterInstance) {
|
||||
c.SceneSID = resp.SceneSID
|
||||
c.UniqueNo = resp.UniqueNo
|
||||
c.InstanceID = msg.InstanceID
|
||||
c.WriteBytes(ss_pb.MessageID(resp.MessageID), resp.Payload)
|
||||
c.WriteBytes(sspb.MessageID(resp.MessageID), resp.Payload)
|
||||
}
|
||||
|
||||
func (c *Client) onLeave() {
|
||||
if c.SceneSID == 0 {
|
||||
return
|
||||
}
|
||||
client, err := grpc_client.SceneNewClient(c.SceneSID)
|
||||
client, err := grpcclient.SceneNewClient(c.SceneSID)
|
||||
if err != nil {
|
||||
c.logger.Errorf("SceneNewClient err: %v", err)
|
||||
return
|
||||
}
|
||||
_, err = client.Leave(context.Background(), &grpc_pb.LeaveReq{
|
||||
_, err = client.Leave(context.Background(), &rspb.LeaveReq{
|
||||
USN: c.USN,
|
||||
UniqueNo: c.UniqueNo,
|
||||
})
|
||||
@@ -93,11 +93,11 @@ func (c *Client) onLeave() {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) onAction(msg *ss_pb.C2S_Action) {
|
||||
func (c *Client) onAction(msg *sspb.C2S_Action) {
|
||||
if c.SceneSID == 0 {
|
||||
return
|
||||
}
|
||||
if err := grpc_client.SendMessageToScene(c.SceneSID, grpc_client.FunAction, &grpc_pb.ActionReq{
|
||||
if err := grpcclient.SendMessageToScene(c.SceneSID, grpcclient.FunAction, &rspb.ActionReq{
|
||||
UniqueNo: c.UniqueNo,
|
||||
USN: c.USN,
|
||||
Action: int32(msg.Action),
|
||||
@@ -1,12 +1,12 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/ss/ss_pb"
|
||||
"git.hlsq.asia/alex/service-common/proto/ss/sspb"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
// WriteMessage 向客户端发送消息
|
||||
func (c *Client) WriteMessage(id ss_pb.MessageID, data proto.Message) {
|
||||
func (c *Client) WriteMessage(id sspb.MessageID, data proto.Message) {
|
||||
if c.conn == nil || c.conn.IsClose() {
|
||||
return
|
||||
}
|
||||
@@ -15,7 +15,7 @@ func (c *Client) WriteMessage(id ss_pb.MessageID, data proto.Message) {
|
||||
c.logger.Errorf("WriteMessage proto.Marshal err: %v", err)
|
||||
return
|
||||
}
|
||||
m, err := proto.Marshal(&ss_pb.Message{
|
||||
m, err := proto.Marshal(&sspb.Message{
|
||||
ID: id,
|
||||
Payload: d,
|
||||
})
|
||||
@@ -29,11 +29,11 @@ func (c *Client) WriteMessage(id ss_pb.MessageID, data proto.Message) {
|
||||
}
|
||||
|
||||
// WriteBytes 向客户端发送字节数据
|
||||
func (c *Client) WriteBytes(id ss_pb.MessageID, data []byte) {
|
||||
func (c *Client) WriteBytes(id sspb.MessageID, data []byte) {
|
||||
if c.conn == nil || c.conn.IsClose() {
|
||||
return
|
||||
}
|
||||
m, err := proto.Marshal(&ss_pb.Message{
|
||||
m, err := proto.Marshal(&sspb.Message{
|
||||
ID: id,
|
||||
Payload: data,
|
||||
})
|
||||
@@ -1,7 +1,7 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
||||
"git.hlsq.asia/alex/service-gateway/internal/global"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -3,14 +3,14 @@ package login
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
||||
"git.hlsq.asia/mmorpg/service-common/log"
|
||||
"git.hlsq.asia/mmorpg/service-common/net/grpc/grpc_client"
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/ss/ss_pb"
|
||||
"git.hlsq.asia/mmorpg/service-common/utils"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/ws_handler/client"
|
||||
"git.hlsq.asia/alex/service-common/db/redis"
|
||||
"git.hlsq.asia/alex/service-common/log"
|
||||
"git.hlsq.asia/alex/service-common/net/grpc/grpcclient"
|
||||
"git.hlsq.asia/alex/service-common/proto/rs/rspb"
|
||||
"git.hlsq.asia/alex/service-common/proto/ss/sspb"
|
||||
"git.hlsq.asia/alex/service-common/utils"
|
||||
"git.hlsq.asia/alex/service-gateway/internal/global"
|
||||
"git.hlsq.asia/alex/service-gateway/internal/handler/wshandler/client"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@@ -109,8 +109,8 @@ func (l *Login) Stop() {
|
||||
// StartLogin 开始登录流程
|
||||
func (l *Login) StartLogin(user *User) {
|
||||
if !l.CheckToken(user) {
|
||||
user.Cli.WriteMessage(ss_pb.MessageID_MESSAGE_ID_KICK_OUT, &ss_pb.S2C_KickOut{
|
||||
ID: ss_pb.KickOutID_KICK_OUT_ID_TOKEN_INVALID,
|
||||
user.Cli.WriteMessage(sspb.MessageID_MESSAGE_ID_KICK_OUT, &sspb.S2C_KickOut{
|
||||
ID: sspb.KickOutID_KICK_OUT_ID_TOKEN_INVALID,
|
||||
})
|
||||
user.Cli.CloseClient()
|
||||
return
|
||||
@@ -118,8 +118,8 @@ func (l *Login) StartLogin(user *User) {
|
||||
if gatewaySID := l.CheckOnline(user); len(gatewaySID) > 0 {
|
||||
// 如果在线就要踢,如果踢失败了就返回服务器繁忙,一般不应该走到这里
|
||||
if !l.KickUser(user.Cli.SceneSID, user.Cli.USN) {
|
||||
user.Cli.WriteMessage(ss_pb.MessageID_MESSAGE_ID_KICK_OUT, &ss_pb.S2C_KickOut{
|
||||
ID: ss_pb.KickOutID_KICK_OUT_ID_SERVER_BUSY,
|
||||
user.Cli.WriteMessage(sspb.MessageID_MESSAGE_ID_KICK_OUT, &sspb.S2C_KickOut{
|
||||
ID: sspb.KickOutID_KICK_OUT_ID_SERVER_BUSY,
|
||||
})
|
||||
user.Cli.CloseClient()
|
||||
return
|
||||
@@ -128,8 +128,8 @@ func (l *Login) StartLogin(user *User) {
|
||||
if client.UserMgr.GetSize() >= global.MaxOnlineSize {
|
||||
// 如果人数满了就排队
|
||||
if err := GetQueueUp().Enqueue(user.Cli); err != nil {
|
||||
user.Cli.WriteMessage(ss_pb.MessageID_MESSAGE_ID_KICK_OUT, &ss_pb.S2C_KickOut{
|
||||
ID: ss_pb.KickOutID_KICK_OUT_ID_QUEUE_UP_FULL,
|
||||
user.Cli.WriteMessage(sspb.MessageID_MESSAGE_ID_KICK_OUT, &sspb.S2C_KickOut{
|
||||
ID: sspb.KickOutID_KICK_OUT_ID_QUEUE_UP_FULL,
|
||||
})
|
||||
user.Cli.CloseClient()
|
||||
return
|
||||
@@ -137,13 +137,13 @@ func (l *Login) StartLogin(user *User) {
|
||||
// 告诉客户端正在排队
|
||||
position, ok := GetQueueUp().GetPosition(user.Cli.USN)
|
||||
if !ok {
|
||||
user.Cli.WriteMessage(ss_pb.MessageID_MESSAGE_ID_KICK_OUT, &ss_pb.S2C_KickOut{
|
||||
ID: ss_pb.KickOutID_KICK_OUT_ID_SERVER_BUSY,
|
||||
user.Cli.WriteMessage(sspb.MessageID_MESSAGE_ID_KICK_OUT, &sspb.S2C_KickOut{
|
||||
ID: sspb.KickOutID_KICK_OUT_ID_SERVER_BUSY,
|
||||
})
|
||||
user.Cli.CloseClient()
|
||||
return
|
||||
}
|
||||
user.Cli.WriteMessage(ss_pb.MessageID_MESSAGE_ID_QUEUE_UP, &ss_pb.S2C_QueueUp{
|
||||
user.Cli.WriteMessage(sspb.MessageID_MESSAGE_ID_QUEUE_UP, &sspb.S2C_QueueUp{
|
||||
QueueUpCount: int32(position),
|
||||
})
|
||||
} else {
|
||||
@@ -165,12 +165,12 @@ func (l *Login) CheckOnline(user *User) string {
|
||||
|
||||
// KickUser 把玩家踢下线
|
||||
func (l *Login) KickUser(gatewaySID int64, usn int64) bool {
|
||||
gc, err := grpc_client.GatewayNewClient(gatewaySID)
|
||||
gc, err := grpcclient.GatewayNewClient(gatewaySID)
|
||||
if err != nil {
|
||||
log.Errorf("KickUser cannot find gateway client: %v, sid: %v", err, gatewaySID)
|
||||
return false
|
||||
}
|
||||
_, err = gc.KickUser(l.ctx, &grpc_pb.KickUserReq{USN: usn})
|
||||
_, err = gc.KickUser(l.ctx, &rspb.KickUserReq{USN: usn})
|
||||
if err != nil {
|
||||
log.Errorf("KickUser err: %v, sid: %v, usn: %v", err, gatewaySID, usn)
|
||||
return false
|
||||
@@ -3,7 +3,7 @@ package login
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/ws_handler/client"
|
||||
"git.hlsq.asia/alex/service-gateway/internal/handler/wshandler/client"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
)
|
||||
@@ -1,11 +1,11 @@
|
||||
package http_gateway
|
||||
package httpgateway
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
||||
"git.hlsq.asia/mmorpg/service-common/net/http/http_resp"
|
||||
"git.hlsq.asia/mmorpg/service-common/utils"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
||||
"git.hlsq.asia/alex/service-common/db/redis"
|
||||
"git.hlsq.asia/alex/service-common/net/http/httpresp"
|
||||
"git.hlsq.asia/alex/service-common/utils"
|
||||
"git.hlsq.asia/alex/service-gateway/internal/global"
|
||||
"github.com/gin-contrib/cors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
@@ -47,7 +47,7 @@ func authJwt() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
pList := strings.SplitN(c.Request.URL.Path, "/", 4)
|
||||
if len(pList) < 4 || pList[2] == "" {
|
||||
http_resp.JsonNotFound(c)
|
||||
httpresp.JsonNotFound(c)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
@@ -60,14 +60,14 @@ func authJwt() gin.HandlerFunc {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
http_resp.JsonUnauthorized(c)
|
||||
httpresp.JsonUnauthorized(c)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
usn, _ := redis.GetClient().HGet(c, global.KeyGatewayAccessToken+token, (&utils.UserSession{}).GetUsnKey()).Int64()
|
||||
if usn == 0 {
|
||||
http_resp.JsonUnauthorized(c)
|
||||
httpresp.JsonUnauthorized(c)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
@@ -1,14 +1,14 @@
|
||||
package http_gateway
|
||||
package httpgateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.hlsq.asia/mmorpg/service-common/discover/common"
|
||||
"git.hlsq.asia/mmorpg/service-common/log"
|
||||
"git.hlsq.asia/mmorpg/service-common/net/grpc/grpc_client"
|
||||
"git.hlsq.asia/mmorpg/service-common/net/http/http_resp"
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/http_handler"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/internal/net/http_gateway/wrapper"
|
||||
"git.hlsq.asia/alex/service-common/discover/common"
|
||||
"git.hlsq.asia/alex/service-common/log"
|
||||
"git.hlsq.asia/alex/service-common/net/grpc/grpcclient"
|
||||
"git.hlsq.asia/alex/service-common/net/http/httpresp"
|
||||
"git.hlsq.asia/alex/service-common/proto/rs/rspb"
|
||||
"git.hlsq.asia/alex/service-gateway/internal/handler/httphandler"
|
||||
"git.hlsq.asia/alex/service-gateway/internal/net/httpgateway/wrapper"
|
||||
"github.com/gin-contrib/cors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
||||
@@ -61,11 +61,11 @@ func InitRouter() *gin.Engine {
|
||||
|
||||
r.HandleMethodNotAllowed = true
|
||||
r.NoMethod(func(c *gin.Context) {
|
||||
http_resp.JsonMethodNotAllowed(c)
|
||||
httpresp.JsonMethodNotAllowed(c)
|
||||
c.Abort()
|
||||
})
|
||||
r.NoRoute(func(c *gin.Context) {
|
||||
http_resp.JsonNotFound(c)
|
||||
httpresp.JsonNotFound(c)
|
||||
c.Abort()
|
||||
})
|
||||
|
||||
@@ -85,22 +85,22 @@ func InitRouter() *gin.Engine {
|
||||
func initGatewayPath(r *gin.RouterGroup) {
|
||||
g := r.Group("/" + common.KeyDiscoverServiceNameGateway)
|
||||
|
||||
g.POST("/open/login", http_handler.Login)
|
||||
g.POST("/open/refresh_token", http_handler.RefreshToken)
|
||||
g.POST("/open/logout", http_handler.Logout)
|
||||
g.POST("/open/login", httphandler.Login)
|
||||
g.POST("/open/refresh_token", httphandler.RefreshToken)
|
||||
g.POST("/open/logout", httphandler.Logout)
|
||||
}
|
||||
|
||||
func initUserPath(r *gin.RouterGroup) {
|
||||
g := r.Group("/" + common.KeyDiscoverServiceNameUser)
|
||||
|
||||
client, err := grpc_client.UserNewClientLB()
|
||||
client, err := grpcclient.UserNewClientLB()
|
||||
if err != nil {
|
||||
log.Errorf("get user conn failed: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
gwMux := InitServeMux()
|
||||
if err = grpc_pb.RegisterUserHandlerClient(context.Background(), gwMux, client); err != nil {
|
||||
if err = rspb.RegisterUserHandlerClient(context.Background(), gwMux, client); err != nil {
|
||||
log.Errorf("RegisterUserHandlerClient err: %v", err)
|
||||
return
|
||||
}
|
||||
@@ -111,14 +111,14 @@ func initUserPath(r *gin.RouterGroup) {
|
||||
func initQgdzsPath(r *gin.RouterGroup) {
|
||||
g := r.Group("/" + common.KeyDiscoverServiceNameQgdzs)
|
||||
|
||||
client, err := grpc_client.QgdzsNewClientLB()
|
||||
client, err := grpcclient.QgdzsNewClientLB()
|
||||
if err != nil {
|
||||
log.Errorf("get qgdzs conn failed: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
gwMux := InitServeMux()
|
||||
if err = grpc_pb.RegisterQgdzsHandlerClient(context.Background(), gwMux, client); err != nil {
|
||||
if err = rspb.RegisterQgdzsHandlerClient(context.Background(), gwMux, client); err != nil {
|
||||
log.Errorf("RegisterQgdzsHandlerClient err: %v", err)
|
||||
return
|
||||
}
|
||||
@@ -3,8 +3,8 @@ package wrapper
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"git.hlsq.asia/mmorpg/service-common/net/http/http_resp"
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/rs_common"
|
||||
"git.hlsq.asia/alex/service-common/net/http/httpresp"
|
||||
"git.hlsq.asia/alex/service-common/proto/rs/rscommon"
|
||||
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
@@ -17,32 +17,32 @@ func ErrorHandler(_ context.Context, _ *runtime.ServeMux, _ runtime.Marshaler, w
|
||||
if !ok {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
_ = json.NewEncoder(w).Encode(http_resp.Error(http_resp.Failed))
|
||||
_ = json.NewEncoder(w).Encode(httpresp.Error(httpresp.Failed))
|
||||
return
|
||||
}
|
||||
|
||||
code, msg := 0, ""
|
||||
for _, detail := range st.Details() {
|
||||
if errorInfo, ok := detail.(*rs_common.ErrorInfo); ok {
|
||||
if errorInfo, ok := detail.(*rscommon.ErrorInfo); ok {
|
||||
code = int(errorInfo.Code)
|
||||
msg = errorInfo.Msg
|
||||
break
|
||||
}
|
||||
}
|
||||
if code == 0 {
|
||||
code = http_resp.Failed.Code()
|
||||
msg = http_resp.Failed.Error()
|
||||
code = httpresp.Failed.Code()
|
||||
msg = httpresp.Failed.Error()
|
||||
}
|
||||
if st.Code() == codes.Unknown || st.Code() == codes.Unimplemented || st.Code() == codes.NotFound {
|
||||
msg = st.Message()
|
||||
}
|
||||
if st.Code() == codes.NotFound {
|
||||
code = http_resp.NotFound.Code()
|
||||
code = httpresp.NotFound.Code()
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(grpcCodeToHTTPCode(st.Code()))
|
||||
_ = json.NewEncoder(w).Encode(http_resp.Error(http_resp.NewCode(code, msg)))
|
||||
_ = json.NewEncoder(w).Encode(httpresp.Error(httpresp.NewCode(code, msg)))
|
||||
}
|
||||
|
||||
// 这里定义 Internal 属于业务错误,其他的属于 500 报错
|
||||
@@ -2,7 +2,7 @@ package wrapper
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"git.hlsq.asia/mmorpg/service-common/net/http/http_resp"
|
||||
"git.hlsq.asia/alex/service-common/net/http/httpresp"
|
||||
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
||||
"io"
|
||||
)
|
||||
@@ -20,9 +20,9 @@ func NewWrappedMarshaler(inner runtime.Marshaler) *WrappedMarshaler {
|
||||
func (w *WrappedMarshaler) Marshal(v interface{}) ([]byte, error) {
|
||||
dataBytes, err := w.inner.Marshal(v)
|
||||
if err != nil {
|
||||
return json.Marshal(http_resp.Error(http_resp.Failed))
|
||||
return json.Marshal(httpresp.Error(httpresp.Failed))
|
||||
}
|
||||
return json.Marshal(http_resp.Success(json.RawMessage(dataBytes)))
|
||||
return json.Marshal(httpresp.Success(json.RawMessage(dataBytes)))
|
||||
}
|
||||
|
||||
func (w *WrappedMarshaler) Unmarshal(data []byte, v interface{}) error {
|
||||
@@ -1,11 +1,11 @@
|
||||
package ws_gateway
|
||||
package wsgateway
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.hlsq.asia/mmorpg/service-common/log"
|
||||
"git.hlsq.asia/mmorpg/service-common/net/socket"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/ws_handler/client"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/ws_handler/login"
|
||||
"git.hlsq.asia/alex/service-common/log"
|
||||
"git.hlsq.asia/alex/service-common/net/socket"
|
||||
"git.hlsq.asia/alex/service-gateway/internal/handler/wshandler/client"
|
||||
"git.hlsq.asia/alex/service-gateway/internal/handler/wshandler/login"
|
||||
"go.uber.org/zap"
|
||||
"time"
|
||||
)
|
||||
@@ -1,9 +1,9 @@
|
||||
package testutil
|
||||
|
||||
import (
|
||||
config2 "git.hlsq.asia/mmorpg/service-common/config"
|
||||
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/config"
|
||||
config2 "git.hlsq.asia/alex/service-common/config"
|
||||
"git.hlsq.asia/alex/service-common/db/redis"
|
||||
"git.hlsq.asia/alex/service-gateway/config"
|
||||
"github.com/alicebob/miniredis/v2"
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
6
main.go
6
main.go
@@ -2,12 +2,16 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/app"
|
||||
"git.hlsq.asia/alex/service-gateway/app"
|
||||
"git.hlsq.asia/alex/service-gateway/config"
|
||||
"github.com/judwhite/go-svc"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := config.LoadConfig(); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
if err := svc.Run(&app.Program{}, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user