auth service

This commit is contained in:
2025-01-20 16:22:41 +08:00
parent 76d3d6f20a
commit bda2501bae
40 changed files with 3340 additions and 1 deletions
+51
View File
@@ -0,0 +1,51 @@
package auth
import (
"context"
auth "github.com/suyiiyii/hertz101/rpc_gen/kitex_gen/auth"
"github.com/suyiiyii/hertz101/rpc_gen/kitex_gen/auth/authservice"
"github.com/cloudwego/kitex/client"
"github.com/cloudwego/kitex/client/callopt"
)
type RPCClient interface {
KitexClient() authservice.Client
Service() string
DeliverTokenByRPC(ctx context.Context, Req *auth.DeliverTokenReq, callOptions ...callopt.Option) (r *auth.DeliveryResp, err error)
VerifyTokenByRPC(ctx context.Context, Req *auth.VerifyTokenReq, callOptions ...callopt.Option) (r *auth.VerifyResp, err error)
}
func NewRPCClient(dstService string, opts ...client.Option) (RPCClient, error) {
kitexClient, err := authservice.NewClient(dstService, opts...)
if err != nil {
return nil, err
}
cli := &clientImpl{
service: dstService,
kitexClient: kitexClient,
}
return cli, nil
}
type clientImpl struct {
service string
kitexClient authservice.Client
}
func (c *clientImpl) Service() string {
return c.service
}
func (c *clientImpl) KitexClient() authservice.Client {
return c.kitexClient
}
func (c *clientImpl) DeliverTokenByRPC(ctx context.Context, Req *auth.DeliverTokenReq, callOptions ...callopt.Option) (r *auth.DeliveryResp, err error) {
return c.kitexClient.DeliverTokenByRPC(ctx, Req, callOptions...)
}
func (c *clientImpl) VerifyTokenByRPC(ctx context.Context, Req *auth.VerifyTokenReq, callOptions ...callopt.Option) (r *auth.VerifyResp, err error) {
return c.kitexClient.VerifyTokenByRPC(ctx, Req, callOptions...)
}
+26
View File
@@ -0,0 +1,26 @@
package auth
import (
"context"
auth "github.com/suyiiyii/hertz101/rpc_gen/kitex_gen/auth"
"github.com/cloudwego/kitex/client/callopt"
"github.com/cloudwego/kitex/pkg/klog"
)
func DeliverTokenByRPC(ctx context.Context, req *auth.DeliverTokenReq, callOptions ...callopt.Option) (resp *auth.DeliveryResp, err error) {
resp, err = defaultClient.DeliverTokenByRPC(ctx, req, callOptions...)
if err != nil {
klog.CtxErrorf(ctx, "DeliverTokenByRPC call failed,err =%+v", err)
return nil, err
}
return resp, nil
}
func VerifyTokenByRPC(ctx context.Context, req *auth.VerifyTokenReq, callOptions ...callopt.Option) (resp *auth.VerifyResp, err error) {
resp, err = defaultClient.VerifyTokenByRPC(ctx, req, callOptions...)
if err != nil {
klog.CtxErrorf(ctx, "VerifyTokenByRPC call failed,err =%+v", err)
return nil, err
}
return resp, nil
}
+40
View File
@@ -0,0 +1,40 @@
package auth
import (
"sync"
"github.com/cloudwego/kitex/client"
)
var (
// todo edit custom config
defaultClient RPCClient
defaultDstService = "auth"
defaultClientOpts = []client.Option{
client.WithHostPorts("127.0.0.1:8888"),
}
once sync.Once
)
func init() {
DefaultClient()
}
func DefaultClient() RPCClient {
once.Do(func() {
defaultClient = newClient(defaultDstService, defaultClientOpts...)
})
return defaultClient
}
func newClient(dstService string, opts ...client.Option) RPCClient {
c, err := NewRPCClient(dstService, opts...)
if err != nil {
panic("failed to init client: " + err.Error())
}
return c
}
func InitClient(dstService string, opts ...client.Option) {
defaultClient = newClient(dstService, opts...)
}