feat 历史记录

This commit is contained in:
2026-01-14 18:15:36 +08:00
parent 313a93702e
commit 636008b97d
16 changed files with 58 additions and 66 deletions

View File

@@ -15,14 +15,6 @@ const (
MaxQueueUpSize = 100 // 最大排队人数
)
// PublicPaths 不需要鉴权的接口,硬编码注册
var PublicPaths = []string{
"/user/info",
"/qgdzs/get_question",
"/qgdzs/answer_question",
"/qgdzs/get_all_category",
}
var (
OnlineUsersGauge prometheus.Gauge
)

View File

@@ -1,4 +1,4 @@
package server
package grpc_server
import (
"context"

View File

@@ -1,4 +1,4 @@
package server
package grpc_server
import (
"git.hlsq.asia/mmorpg/service-common/discover/common"

View File

@@ -5,7 +5,7 @@ import (
"fmt"
"git.hlsq.asia/mmorpg/service-common/db/redis"
"git.hlsq.asia/mmorpg/service-common/log"
"git.hlsq.asia/mmorpg/service-common/net/grpc/service"
"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"
@@ -36,7 +36,7 @@ func Login(c *gin.Context) {
http_resp.JsonBadRequest(c)
return
}
client, err := service.UserNewClientLB()
client, err := grpc_client.UserNewClientLB()
if err != nil {
log.Errorf("Login UserNewClientLB error: %v", err)
http_resp.JsonOK(c, http_resp.Error(http_resp.Failed))
@@ -97,11 +97,11 @@ func RefreshToken(c *gin.Context) {
}
claims, err := utils.ParseToken(req.RefreshToken, config.Get().Auth.Secret)
if err != nil {
http_resp.JsonOK(c, http_resp.Error(http_resp.TokenInvalid))
http_resp.JsonUnauthorized(c)
return
}
if redis.GetClient().Get(c, fmt.Sprintf(global.KeyGatewayRefreshToken, claims.USN)).Val() != req.RefreshToken {
http_resp.JsonOK(c, http_resp.Error(http_resp.TokenInvalid))
http_resp.JsonUnauthorized(c)
return
}
at, rt, err := genToken(c, claims.USN)
@@ -118,11 +118,11 @@ func RefreshToken(c *gin.Context) {
}
func genToken(ctx context.Context, usn string) (string, string, error) {
at, err := genTokenOne(ctx, global.KeyGatewayAccessToken, usn, 2*time.Hour)
at, err := genTokenOne(ctx, global.KeyGatewayAccessToken, usn, time.Duration(config.Get().Auth.ShortExpire)*time.Minute)
if err != nil {
return "", "", err
}
rt, err := genTokenOne(ctx, global.KeyGatewayRefreshToken, usn, 3*24*time.Hour)
rt, err := genTokenOne(ctx, global.KeyGatewayRefreshToken, usn, time.Duration(config.Get().Auth.LongExpire)*time.Minute)
if err != nil {
return "", "", err
}
@@ -130,7 +130,7 @@ func genToken(ctx context.Context, usn string) (string, string, error) {
}
func genTokenOne(ctx context.Context, key string, usn string, ttl time.Duration) (string, error) {
token, err := utils.GenToken(usn, config.Get().Auth.Secret, time.Duration(config.Get().Auth.Expire)*time.Second)
token, err := utils.GenToken(usn, config.Get().Auth.Secret, ttl)
if err != nil {
return "", err
}

View File

@@ -5,7 +5,7 @@ import (
"context"
"fmt"
"git.hlsq.asia/mmorpg/service-common/db/redis"
"git.hlsq.asia/mmorpg/service-common/net/grpc/service"
"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"
@@ -50,10 +50,10 @@ func (ts *LoginTestSuite) TestLogin() {
})
ts.Run("UserNewClientLB Failed", func() {
monkey.Patch(service.UserNewClientLB, func() (grpc_pb.UserClient, error) {
monkey.Patch(grpc_client.UserNewClientLB, func() (grpc_pb.UserClient, error) {
return nil, assert.AnError
})
defer monkey.Unpatch(service.UserNewClientLB)
defer monkey.Unpatch(grpc_client.UserNewClientLB)
w, c := utils.CreateTestContext("POST", "/", &LoginReq{
Phone: "13800000000",
@@ -68,13 +68,13 @@ func (ts *LoginTestSuite) TestLogin() {
defer ctrl.Finish()
client := mocks.NewMockUserClient(ctrl)
client.EXPECT().
Login(gomock.Any(), gomock.Any()).
PhoneLogin(gomock.Any(), gomock.Any()).
Return(nil, fmt.Errorf("gRPC failed"))
monkey.Patch(service.UserNewClientLB, func() (grpc_pb.UserClient, error) {
monkey.Patch(grpc_client.UserNewClientLB, func() (grpc_pb.UserClient, error) {
return client, nil
})
defer monkey.Unpatch(service.UserNewClientLB)
defer monkey.Unpatch(grpc_client.UserNewClientLB)
w, c := utils.CreateTestContext("POST", "/", &LoginReq{
Phone: "13800000000",
@@ -89,13 +89,13 @@ func (ts *LoginTestSuite) TestLogin() {
defer ctrl.Finish()
client := mocks.NewMockUserClient(ctrl)
client.EXPECT().
Login(gomock.Any(), gomock.Any()).
Return(&grpc_pb.LoginResp{USN: "1", Name: "hh"}, nil)
PhoneLogin(gomock.Any(), gomock.Any()).
Return(&grpc_pb.PhoneLoginResp{USN: "1", Name: "hh"}, nil)
monkey.Patch(service.UserNewClientLB, func() (grpc_pb.UserClient, error) {
monkey.Patch(grpc_client.UserNewClientLB, func() (grpc_pb.UserClient, error) {
return client, nil
})
defer monkey.Unpatch(service.UserNewClientLB)
defer monkey.Unpatch(grpc_client.UserNewClientLB)
w, c := utils.CreateTestContext("POST", "/", &LoginReq{
Phone: "13800000000",

View File

@@ -4,8 +4,7 @@ import (
"context"
"fmt"
"git.hlsq.asia/mmorpg/service-common/db/redis"
"git.hlsq.asia/mmorpg/service-common/net/grpc/service"
"git.hlsq.asia/mmorpg/service-common/net/grpc/stream_client"
"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"
@@ -55,7 +54,7 @@ func (c *Client) handle(event Event) {
}
func (c *Client) onEnter(msg *ss_pb.C2S_EnterInstance) {
client, err := service.SceneNewClientLB()
client, err := grpc_client.SceneNewClientLB()
if err != nil {
c.logger.Errorf("SceneNewClient err: %v", err)
return
@@ -79,7 +78,7 @@ func (c *Client) onLeave() {
if c.SceneSID == "" {
return
}
client, err := service.SceneNewClient(c.SceneSID)
client, err := grpc_client.SceneNewClient(c.SceneSID)
if err != nil {
c.logger.Errorf("SceneNewClient err: %v", err)
return
@@ -98,7 +97,7 @@ func (c *Client) onAction(msg *ss_pb.C2S_Action) {
if c.SceneSID == "" {
return
}
if err := stream_client.SendMessageToScene(c.SceneSID, stream_client.FunAction, &grpc_pb.ActionReq{
if err := grpc_client.SendMessageToScene(c.SceneSID, grpc_client.FunAction, &grpc_pb.ActionReq{
UniqueNo: c.UniqueNo,
USN: c.USN,
Action: int32(msg.Action),
@@ -107,5 +106,6 @@ func (c *Client) onAction(msg *ss_pb.C2S_Action) {
SkillID: msg.SkillID,
}); err != nil {
c.logger.Errorf("send action err: %v", err)
return
}
}

View File

@@ -5,7 +5,7 @@ import (
"fmt"
"git.hlsq.asia/mmorpg/service-common/db/redis"
"git.hlsq.asia/mmorpg/service-common/log"
"git.hlsq.asia/mmorpg/service-common/net/grpc/service"
"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"
@@ -162,7 +162,7 @@ func (l *Login) CheckOnline(user *User) string {
// KickUser 把玩家踢下线
func (l *Login) KickUser(gatewaySID string, usn string) bool {
gc, err := service.GatewayNewClient(gatewaySID)
gc, err := grpc_client.GatewayNewClient(gatewaySID)
if err != nil {
log.Errorf("KickUser cannot find gateway client: %v, sid: %v", err, gatewaySID)
return false

View File

@@ -5,7 +5,6 @@ import (
"git.hlsq.asia/mmorpg/service-common/net/http/http_resp"
"git.hlsq.asia/mmorpg/service-common/utils"
"git.hlsq.asia/mmorpg/service-gateway/config"
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
@@ -43,14 +42,13 @@ func ginLogger(logger *zap.SugaredLogger) gin.HandlerFunc {
func authJwt() gin.HandlerFunc {
return func(c *gin.Context) {
// 如果是Public接口有Token就读没有就算了
public := false
for _, path := range global.PublicPaths {
if strings.HasPrefix(c.Request.URL.Path, path) {
public = true
break
}
pList := strings.SplitN(c.Request.URL.Path, "/", 4)
if len(pList) < 4 || pList[2] == "" {
http_resp.JsonNotFound(c)
return
}
// 如果是Public接口有Token就读没有就算了
public := pList[2] == "open"
token := strings.TrimPrefix(c.GetHeader("Authorization"), "Bearer ")
if token == "" {
@@ -58,16 +56,14 @@ func authJwt() gin.HandlerFunc {
c.Next()
return
}
http_resp.AbortUnauthorized(c)
http_resp.JsonUnauthorized(c)
c.Abort()
return
}
claims, err := utils.ParseToken(token, config.Get().Auth.Secret)
if err != nil {
if public {
c.Next()
return
}
http_resp.AbortUnauthorized(c)
http_resp.JsonUnauthorized(c)
c.Abort()
return
}

View File

@@ -3,7 +3,7 @@ package http_gateway
import (
"context"
"git.hlsq.asia/mmorpg/service-common/log"
"git.hlsq.asia/mmorpg/service-common/net/grpc/service"
"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"
@@ -52,16 +52,18 @@ func InitRouter() *gin.Engine {
r.HandleMethodNotAllowed = true
r.NoMethod(func(c *gin.Context) {
http_resp.JsonMethodNotAllowed(c)
c.Abort()
})
r.NoRoute(func(c *gin.Context) {
http_resp.JsonNotFound(c)
c.Abort()
})
initBaseRoute(r.Group("/"))
auth := r.Group("/")
auth.Use(authJwt())
// 网关
initBaseRoute(auth)
// 用户中心
initUserPath(auth)
// 奇怪的知识-服务端
@@ -72,13 +74,13 @@ func InitRouter() *gin.Engine {
func initBaseRoute(r *gin.RouterGroup) {
g := r.Group("/gw")
g.POST("/login", http_handler.Login)
g.POST("/refresh_token", http_handler.RefreshToken)
g.POST("/open/login", http_handler.Login)
g.POST("/open/refresh_token", http_handler.RefreshToken)
}
func initUserPath(r *gin.RouterGroup) {
g := r.Group("/user")
client, err := service.UserNewClientLB()
client, err := grpc_client.UserNewClientLB()
if err != nil {
log.Errorf("get user conn failed: %v", err)
return
@@ -95,7 +97,7 @@ func initUserPath(r *gin.RouterGroup) {
func initQgdzsPath(r *gin.RouterGroup) {
g := r.Group("/qgdzs")
client, err := service.QgdzsNewClientLB()
client, err := grpc_client.QgdzsNewClientLB()
if err != nil {
log.Errorf("get qgdzs conn failed: %v", err)
return

View File

@@ -28,8 +28,9 @@ func (ts *TestSuite) SetupSuite() {
},
},
Auth: &config.AuthConfig{
Secret: "test",
Expire: 259200,
Secret: "test",
ShortExpire: 15,
LongExpire: 10080,
},
})