mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-05 21:20:06 +00:00
gRPC with toek demo
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
pb "github.com/EDDYCJY/go-grpc-example/proto"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
// Auth ..
|
||||
type Auth struct {
|
||||
AppKey string
|
||||
AppSecret string
|
||||
}
|
||||
|
||||
// GetRequestMetadata ..
|
||||
func (a *Auth) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
|
||||
return map[string]string{"app_key": a.AppKey, "app_secret": a.AppSecret}, nil
|
||||
}
|
||||
|
||||
// RequireTransportSecurity ..
|
||||
func (a *Auth) RequireTransportSecurity() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func main() {
|
||||
auth := Auth{
|
||||
AppKey: "naiba",
|
||||
AppSecret: "nbsecret0",
|
||||
}
|
||||
conn, err := grpc.Dial(":5555", grpc.WithInsecure(), grpc.WithPerRPCCredentials(&auth))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
client := pb.NewSearchServiceClient(conn)
|
||||
resp, err := client.Search(context.Background(), &pb.SearchRequest{
|
||||
Request: "gRPC",
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("client.Search err: %v", err)
|
||||
}
|
||||
|
||||
log.Printf("resp: %s", resp.GetResponse())
|
||||
}
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
|
||||
pb "github.com/EDDYCJY/go-grpc-example/proto"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
// SearchService ..
|
||||
type SearchService struct {
|
||||
auth *Auth
|
||||
}
|
||||
|
||||
// Search ..
|
||||
func (s *SearchService) Search(ctx context.Context, r *pb.SearchRequest) (*pb.SearchResponse, error) {
|
||||
if err := s.auth.Check(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pb.SearchResponse{Response: r.GetRequest() + " Token Server"}, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
server := grpc.NewServer()
|
||||
pb.RegisterSearchServiceServer(server, &SearchService{})
|
||||
|
||||
lis, err := net.Listen("tcp", ":5555")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
server.Serve(lis)
|
||||
}
|
||||
|
||||
// Auth ..
|
||||
type Auth struct {
|
||||
appKey string
|
||||
appSecret string
|
||||
}
|
||||
|
||||
// Check ..
|
||||
func (a *Auth) Check(ctx context.Context) error {
|
||||
md, ok := metadata.FromIncomingContext(ctx)
|
||||
if !ok {
|
||||
return status.Errorf(codes.Unauthenticated, "metadata.FromIncomingContext err")
|
||||
}
|
||||
|
||||
var (
|
||||
appKey string
|
||||
appSecret string
|
||||
)
|
||||
if value, ok := md["app_key"]; ok {
|
||||
appKey = value[0]
|
||||
}
|
||||
if value, ok := md["app_secret"]; ok {
|
||||
appSecret = value[0]
|
||||
}
|
||||
|
||||
if appKey != a.GetAppKey() || appSecret != a.GetAppSecret() {
|
||||
return status.Errorf(codes.Unauthenticated, "invalid token")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetAppKey ..
|
||||
func (a *Auth) GetAppKey() string {
|
||||
return "naiba"
|
||||
}
|
||||
|
||||
// GetAppSecret ..
|
||||
func (a *Auth) GetAppSecret() string {
|
||||
return "nbsecret"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user