Browse Source

v0.0.1开发:auth服务开发

#Suyghur 2 năm trước cách đây
mục cha
commit
a0c96fa5fe

+ 1 - 1
apis/auth/etc/auth.yaml

@@ -1,5 +1,5 @@
 Name: auth.rpc
-ListenOn: 0.0.0.0:10002
+ListenOn: 0.0.0.0:11000
 Etcd:
   Hosts:
   - 127.0.0.1:2379

+ 2 - 4
apis/auth/internal/logic/csauthlogic.go

@@ -26,14 +26,12 @@ func NewCsAuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CsAuthLogi
 
 func (l *CsAuthLogic) CsAuth(in *pb.CsAuthReq) (*pb.AuthResp, error) {
 	l.Logger.Info("invoke func CsAuth...")
-	l.Logger.Infof("uname: %s", in.Uname)
-	l.Logger.Infof("password: %s", in.Password)
+	l.Logger.Infof("cs_id: %s", in.CsId)
 
 	// todo 查询用户信息
 	// todo 生成token
 	if data, err := structpb.NewStruct(map[string]interface{}{
-		"token":         "cs_auth",
-		"basic_rpc_url": "https://www.baidu.com",
+		"token": "cs_auth",
 	}); err != nil {
 		return &pb.AuthResp{
 			Code: 1,

+ 1 - 5
apis/auth/internal/logic/playerauthlogic.go

@@ -25,7 +25,6 @@ func NewPlayerAuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Player
 }
 
 func (l *PlayerAuthLogic) PlayerAuth(in *pb.PlayerAuthReq) (*pb.AuthResp, error) {
-
 	l.Logger.Info("invoke func PlayerAuth...")
 	l.Logger.Infof("player_id: %s", in.PlayerId)
 	l.Logger.Infof("game_id: %s", in.GameId)
@@ -33,10 +32,7 @@ func (l *PlayerAuthLogic) PlayerAuth(in *pb.PlayerAuthReq) (*pb.AuthResp, error)
 	// todo 查询用户信息
 	// todo 生成token
 	if data, err := structpb.NewStruct(map[string]interface{}{
-		"token":         "player_auth",
-		"has_own_cs":    1,
-		"cs_id":         "cs1231",
-		"basic_rpc_url": "https://www.baidu.com",
+		"token": "player_auth",
 	}); err != nil {
 		return &pb.AuthResp{
 			Code: 1,

+ 23 - 32
apis/auth/pb/auth.pb.go

@@ -128,8 +128,7 @@ type CsAuthReq struct {
 	sizeCache     protoimpl.SizeCache
 	unknownFields protoimpl.UnknownFields
 
-	Uname    string `protobuf:"bytes,1,opt,name=uname,proto3" json:"uname,omitempty"`
-	Password string `protobuf:"bytes,2,opt,name=password,proto3" json:"password,omitempty"`
+	CsId string `protobuf:"bytes,1,opt,name=cs_id,json=csId,proto3" json:"cs_id,omitempty"`
 }
 
 func (x *CsAuthReq) Reset() {
@@ -164,16 +163,9 @@ func (*CsAuthReq) Descriptor() ([]byte, []int) {
 	return file_pb_auth_proto_rawDescGZIP(), []int{2}
 }
 
-func (x *CsAuthReq) GetUname() string {
+func (x *CsAuthReq) GetCsId() string {
 	if x != nil {
-		return x.Uname
-	}
-	return ""
-}
-
-func (x *CsAuthReq) GetPassword() string {
-	if x != nil {
-		return x.Password
+		return x.CsId
 	}
 	return ""
 }
@@ -302,28 +294,27 @@ var file_pb_auth_proto_rawDesc = []byte{
 	0x61, 0x79, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x70,
 	0x6c, 0x61, 0x79, 0x65, 0x72, 0x49, 0x64, 0x12, 0x17, 0x0a, 0x07, 0x67, 0x61, 0x6d, 0x65, 0x5f,
 	0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x67, 0x61, 0x6d, 0x65, 0x49, 0x64,
-	0x22, 0x3d, 0x0a, 0x09, 0x43, 0x73, 0x41, 0x75, 0x74, 0x68, 0x52, 0x65, 0x71, 0x12, 0x14, 0x0a,
-	0x05, 0x75, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x75, 0x6e,
-	0x61, 0x6d, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x18,
-	0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x22,
-	0x24, 0x0a, 0x0c, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x41, 0x75, 0x74, 0x68, 0x52, 0x65, 0x71, 0x12,
-	0x14, 0x0a, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05,
-	0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x22, 0x5d, 0x0a, 0x08, 0x41, 0x75, 0x74, 0x68, 0x52, 0x65, 0x73,
-	0x70, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52,
-	0x04, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x6d, 0x73, 0x67, 0x18, 0x02, 0x20, 0x01,
-	0x28, 0x09, 0x52, 0x03, 0x6d, 0x73, 0x67, 0x12, 0x2b, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18,
-	0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x17, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70,
-	0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x53, 0x74, 0x72, 0x75, 0x63, 0x74, 0x52, 0x04,
-	0x64, 0x61, 0x74, 0x61, 0x32, 0x89, 0x01, 0x0a, 0x04, 0x41, 0x75, 0x74, 0x68, 0x12, 0x2d, 0x0a,
-	0x0a, 0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x41, 0x75, 0x74, 0x68, 0x12, 0x11, 0x2e, 0x70, 0x62,
-	0x2e, 0x50, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x41, 0x75, 0x74, 0x68, 0x52, 0x65, 0x71, 0x1a, 0x0c,
-	0x2e, 0x70, 0x62, 0x2e, 0x41, 0x75, 0x74, 0x68, 0x52, 0x65, 0x73, 0x70, 0x12, 0x25, 0x0a, 0x06,
-	0x63, 0x73, 0x41, 0x75, 0x74, 0x68, 0x12, 0x0d, 0x2e, 0x70, 0x62, 0x2e, 0x43, 0x73, 0x41, 0x75,
+	0x22, 0x20, 0x0a, 0x09, 0x43, 0x73, 0x41, 0x75, 0x74, 0x68, 0x52, 0x65, 0x71, 0x12, 0x13, 0x0a,
+	0x05, 0x63, 0x73, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x63, 0x73,
+	0x49, 0x64, 0x22, 0x24, 0x0a, 0x0c, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x41, 0x75, 0x74, 0x68, 0x52,
+	0x65, 0x71, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28,
+	0x09, 0x52, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x22, 0x5d, 0x0a, 0x08, 0x41, 0x75, 0x74, 0x68,
+	0x52, 0x65, 0x73, 0x70, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01,
+	0x28, 0x03, 0x52, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x6d, 0x73, 0x67, 0x18,
+	0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6d, 0x73, 0x67, 0x12, 0x2b, 0x0a, 0x04, 0x64, 0x61,
+	0x74, 0x61, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x17, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c,
+	0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x53, 0x74, 0x72, 0x75, 0x63,
+	0x74, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x32, 0x89, 0x01, 0x0a, 0x04, 0x41, 0x75, 0x74, 0x68,
+	0x12, 0x2d, 0x0a, 0x0a, 0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x41, 0x75, 0x74, 0x68, 0x12, 0x11,
+	0x2e, 0x70, 0x62, 0x2e, 0x50, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x41, 0x75, 0x74, 0x68, 0x52, 0x65,
+	0x71, 0x1a, 0x0c, 0x2e, 0x70, 0x62, 0x2e, 0x41, 0x75, 0x74, 0x68, 0x52, 0x65, 0x73, 0x70, 0x12,
+	0x25, 0x0a, 0x06, 0x63, 0x73, 0x41, 0x75, 0x74, 0x68, 0x12, 0x0d, 0x2e, 0x70, 0x62, 0x2e, 0x43,
+	0x73, 0x41, 0x75, 0x74, 0x68, 0x52, 0x65, 0x71, 0x1a, 0x0c, 0x2e, 0x70, 0x62, 0x2e, 0x41, 0x75,
+	0x74, 0x68, 0x52, 0x65, 0x73, 0x70, 0x12, 0x2b, 0x0a, 0x09, 0x63, 0x68, 0x65, 0x63, 0x6b, 0x41,
+	0x75, 0x74, 0x68, 0x12, 0x10, 0x2e, 0x70, 0x62, 0x2e, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x41, 0x75,
 	0x74, 0x68, 0x52, 0x65, 0x71, 0x1a, 0x0c, 0x2e, 0x70, 0x62, 0x2e, 0x41, 0x75, 0x74, 0x68, 0x52,
-	0x65, 0x73, 0x70, 0x12, 0x2b, 0x0a, 0x09, 0x63, 0x68, 0x65, 0x63, 0x6b, 0x41, 0x75, 0x74, 0x68,
-	0x12, 0x10, 0x2e, 0x70, 0x62, 0x2e, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x41, 0x75, 0x74, 0x68, 0x52,
-	0x65, 0x71, 0x1a, 0x0c, 0x2e, 0x70, 0x62, 0x2e, 0x41, 0x75, 0x74, 0x68, 0x52, 0x65, 0x73, 0x70,
-	0x42, 0x06, 0x5a, 0x04, 0x2e, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
+	0x65, 0x73, 0x70, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f,
+	0x74, 0x6f, 0x33,
 }
 
 var (

+ 1 - 2
apis/auth/pb/auth.proto

@@ -16,8 +16,7 @@ message PlayerAuthReq{
 }
 
 message CsAuthReq{
-  string uname = 1;
-  string password = 2;
+  string cs_id = 1;
 }
 
 message CheckAuthReq{

+ 1 - 1
bff/apibff/etc/apibff.yaml

@@ -1,6 +1,6 @@
 Name: apibff
 Host: 0.0.0.0
-Port: 10000
+Port: 10001
 
 AuthRpc:
   Etcd:

+ 31 - 0
bff/authbff/authbff.go

@@ -0,0 +1,31 @@
+package main
+
+import (
+	"flag"
+	"fmt"
+
+	"ylink/bff/authbff/internal/config"
+	"ylink/bff/authbff/internal/handler"
+	"ylink/bff/authbff/internal/svc"
+
+	"github.com/zeromicro/go-zero/core/conf"
+	"github.com/zeromicro/go-zero/rest"
+)
+
+var configFile = flag.String("f", "etc/authbff.yaml", "the config file")
+
+func main() {
+	flag.Parse()
+
+	var c config.Config
+	conf.MustLoad(*configFile, &c)
+
+	ctx := svc.NewServiceContext(c)
+	server := rest.MustNewServer(c.RestConf)
+	defer server.Stop()
+
+	handler.RegisterHandlers(server, ctx)
+
+	fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
+	server.Start()
+}

+ 36 - 0
bff/authbff/desc/authbff.api

@@ -0,0 +1,36 @@
+syntax = "v1"
+
+info(
+	title: "api前端服务"
+	desc: "api前端服务 "
+	author: "#Suyghur"
+	version: "v1"
+)
+
+type PlayerAuthReq {
+	PlayerId string `json:"player_id"`
+	GameId   string `json:"game_id"`
+}
+
+type CsAuthReq {
+	CsId string `json:"cs_id"`
+}
+
+type AuthResp {
+	Code int64       `json:"code"`
+	Msg  string      `json:"msg"`
+	Data interface{} `json:"data"`
+}
+
+@server(
+	prefix: api/v1
+)
+service Authbff {
+	@doc "玩家认证"
+	@handler playerAuth
+	post /player/auth (PlayerAuthReq) returns (AuthResp)
+	
+	@doc "客服认证"
+	@handler csAuth
+	post /cs/auth (CsAuthReq) returns (AuthResp)
+}

+ 9 - 0
bff/authbff/etc/authbff.yaml

@@ -0,0 +1,9 @@
+Name: Authbff
+Host: 0.0.0.0
+Port: 10000
+
+AuthRpc:
+  Etcd:
+    Hosts:
+      - 127.0.0.1:2379
+    Key: auth.rpc

+ 11 - 0
bff/authbff/internal/config/config.go

@@ -0,0 +1,11 @@
+package config
+
+import (
+	"github.com/zeromicro/go-zero/rest"
+	"github.com/zeromicro/go-zero/zrpc"
+)
+
+type Config struct {
+	rest.RestConf
+	AuthRpc zrpc.RpcClientConf
+}

+ 35 - 0
bff/authbff/internal/handler/csauthhandler.go

@@ -0,0 +1,35 @@
+package handler
+
+import (
+	"github.com/zeromicro/go-zero/rest/httpx"
+	"net/http"
+	"ylink/bff/authbff/internal/logic"
+	"ylink/bff/authbff/internal/svc"
+	"ylink/bff/authbff/internal/types"
+)
+
+func csAuthHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.CsAuthReq
+		if err := httpx.Parse(r, &req); err != nil {
+			httpx.OkJson(w, &types.AuthResp{
+				Code: 1,
+				Msg:  err.Error(),
+				Data: map[string]interface{}{},
+			})
+			return
+		}
+
+		l := logic.NewCsAuthLogic(r.Context(), svcCtx)
+		resp, err := l.CsAuth(&req)
+		if err != nil {
+			httpx.OkJson(w, &types.AuthResp{
+				Code: 1,
+				Msg:  err.Error(),
+				Data: map[string]interface{}{},
+			})
+		} else {
+			httpx.OkJson(w, resp)
+		}
+	}
+}

+ 28 - 0
bff/authbff/internal/handler/playerauthhandler.go

@@ -0,0 +1,28 @@
+package handler
+
+import (
+	"net/http"
+
+	"github.com/zeromicro/go-zero/rest/httpx"
+	"ylink/bff/authbff/internal/logic"
+	"ylink/bff/authbff/internal/svc"
+	"ylink/bff/authbff/internal/types"
+)
+
+func playerAuthHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.PlayerAuthReq
+		if err := httpx.Parse(r, &req); err != nil {
+			httpx.Error(w, err)
+			return
+		}
+
+		l := logic.NewPlayerAuthLogic(r.Context(), svcCtx)
+		resp, err := l.PlayerAuth(&req)
+		if err != nil {
+			httpx.Error(w, err)
+		} else {
+			httpx.OkJson(w, resp)
+		}
+	}
+}

+ 28 - 0
bff/authbff/internal/handler/routes.go

@@ -0,0 +1,28 @@
+// Code generated by goctl. DO NOT EDIT.
+package handler
+
+import (
+	"net/http"
+
+	"ylink/bff/authbff/internal/svc"
+
+	"github.com/zeromicro/go-zero/rest"
+)
+
+func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
+	server.AddRoutes(
+		[]rest.Route{
+			{
+				Method:  http.MethodPost,
+				Path:    "/player/auth",
+				Handler: playerAuthHandler(serverCtx),
+			},
+			{
+				Method:  http.MethodPost,
+				Path:    "/cs/auth",
+				Handler: csAuthHandler(serverCtx),
+			},
+		},
+		rest.WithPrefix("/api/v1"),
+	)
+}

+ 43 - 0
bff/authbff/internal/logic/csauthlogic.go

@@ -0,0 +1,43 @@
+package logic
+
+import (
+	"context"
+	"ylink/apis/auth/pb"
+
+	"ylink/bff/authbff/internal/svc"
+	"ylink/bff/authbff/internal/types"
+
+	"github.com/zeromicro/go-zero/core/logx"
+)
+
+type CsAuthLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewCsAuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CsAuthLogic {
+	return &CsAuthLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx,
+	}
+}
+
+func (l *CsAuthLogic) CsAuth(req *types.CsAuthReq) (resp *types.AuthResp, err error) {
+	if authResp, err := l.svcCtx.AuthRpc.CsAuth(l.ctx, &pb.CsAuthReq{
+		CsId: req.CsId,
+	}); err != nil {
+		return &types.AuthResp{
+			Code: authResp.Code,
+			Msg:  authResp.Msg,
+			Data: map[string]interface{}{},
+		}, err
+	} else {
+		return &types.AuthResp{
+			Code: authResp.Code,
+			Msg:  authResp.Msg,
+			Data: authResp.Data,
+		}, nil
+	}
+}

+ 44 - 0
bff/authbff/internal/logic/playerauthlogic.go

@@ -0,0 +1,44 @@
+package logic
+
+import (
+	"context"
+	"ylink/apis/auth/pb"
+
+	"ylink/bff/authbff/internal/svc"
+	"ylink/bff/authbff/internal/types"
+
+	"github.com/zeromicro/go-zero/core/logx"
+)
+
+type PlayerAuthLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewPlayerAuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PlayerAuthLogic {
+	return &PlayerAuthLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx,
+	}
+}
+
+func (l *PlayerAuthLogic) PlayerAuth(req *types.PlayerAuthReq) (resp *types.AuthResp, err error) {
+	if authResp, err := l.svcCtx.AuthRpc.PlayerAuth(l.ctx, &pb.PlayerAuthReq{
+		PlayerId: req.PlayerId,
+		GameId:   req.GameId,
+	}); err != nil {
+		return &types.AuthResp{
+			Code: authResp.Code,
+			Msg:  authResp.Msg,
+			Data: map[string]interface{}{},
+		}, err
+	} else {
+		return &types.AuthResp{
+			Code: authResp.Code,
+			Msg:  authResp.Msg,
+			Data: authResp.Data,
+		}, nil
+	}
+}

+ 19 - 0
bff/authbff/internal/svc/servicecontext.go

@@ -0,0 +1,19 @@
+package svc
+
+import (
+	"github.com/zeromicro/go-zero/zrpc"
+	"ylink/apis/auth/auth"
+	"ylink/bff/authbff/internal/config"
+)
+
+type ServiceContext struct {
+	Config  config.Config
+	AuthRpc auth.Auth
+}
+
+func NewServiceContext(c config.Config) *ServiceContext {
+	return &ServiceContext{
+		Config:  c,
+		AuthRpc: auth.NewAuth(zrpc.MustNewClient(c.AuthRpc)),
+	}
+}

+ 17 - 0
bff/authbff/internal/types/types.go

@@ -0,0 +1,17 @@
+// Code generated by goctl. DO NOT EDIT.
+package types
+
+type PlayerAuthReq struct {
+	PlayerId string `json:"player_id"`
+	GameId   string `json:"game_id"`
+}
+
+type CsAuthReq struct {
+	CsId string `json:"cs_id"`
+}
+
+type AuthResp struct {
+	Code int64       `json:"code"`
+	Msg  string      `json:"msg"`
+	Data interface{} `json:"data"`
+}

+ 1 - 1
bff/rpcbff/etc/rpcbff.yaml

@@ -1,5 +1,5 @@
 Name: rpcbff.rpc
-ListenOn: 0.0.0.0:10001
+ListenOn: 0.0.0.0:10002
 Etcd:
   Hosts:
   - 127.0.0.1:2379