-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
537 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* @Time : 27/04/2020 12:00 PM | ||
* @Author : solacowa@gmail.com | ||
* @File : grpc | ||
* @Software: GoLand | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/icowan/shorter/pkg/grpc/pb" | ||
"google.golang.org/grpc" | ||
"log" | ||
"time" | ||
) | ||
|
||
func main() { | ||
conn, err := grpc.Dial("127.0.0.1:8082", grpc.WithInsecure(), grpc.WithBlock()) | ||
if err != nil { | ||
log.Fatalf("did not connect: %v", err) | ||
} | ||
defer func() { | ||
_ = conn.Close() | ||
}() | ||
|
||
svc := pb.NewShorterClient(conn) | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), time.Second) | ||
defer cancel() | ||
|
||
r, err := svc.Post(ctx, &pb.PostRequest{ | ||
Domain: "https://www.baidu.com", | ||
}) | ||
if err != nil { | ||
log.Fatalf("could not put: %v", err) | ||
} | ||
|
||
fmt.Println("Code", r.Data.Code, "ShortUri", r.Data.ShortUri) | ||
|
||
r, err = svc.Get(ctx, &pb.GetRequest{ | ||
Code: r.Data.Code, | ||
}) | ||
if err != nil { | ||
log.Fatalf("could not get: %v", err) | ||
} | ||
log.Printf("data: %s", r.GetData()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/** | ||
* @Time : 27/04/2020 11:34 AM | ||
* @Author : solacowa@gmail.com | ||
* @File : handler | ||
* @Software: GoLand | ||
*/ | ||
|
||
package grpc | ||
|
||
import ( | ||
"context" | ||
"github.com/icowan/shorter/pkg/service" | ||
|
||
kitgrpc "github.com/go-kit/kit/transport/grpc" | ||
endpoint2 "github.com/icowan/shorter/pkg/endpoint" | ||
"github.com/icowan/shorter/pkg/grpc/pb" | ||
) | ||
|
||
type grpcServer struct { | ||
get kitgrpc.Handler | ||
post kitgrpc.Handler | ||
} | ||
|
||
func (g *grpcServer) Get(ctx context.Context, req *pb.GetRequest) (*pb.ServiceResponse, error) { | ||
_, rep, err := g.get.ServeGRPC(ctx, req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return rep.(*pb.ServiceResponse), nil | ||
} | ||
|
||
func (g *grpcServer) Post(ctx context.Context, req *pb.PostRequest) (*pb.ServiceResponse, error) { | ||
_, rep, err := g.post.ServeGRPC(ctx, req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return rep.(*pb.ServiceResponse), nil | ||
} | ||
|
||
func MakeGRPCHandler(eps endpoint2.Endpoints, opts map[string][]kitgrpc.ServerOption) pb.ShorterServer { | ||
return &grpcServer{ | ||
get: kitgrpc.NewServer( | ||
eps.GetEndpoint, | ||
decodeGetRequest, | ||
encodeResponse, | ||
opts["Get"]..., | ||
), | ||
post: kitgrpc.NewServer( | ||
eps.PostEndpoint, | ||
decodePostRequest, | ||
encodePostResponse, | ||
opts["Post"]..., | ||
), | ||
} | ||
} | ||
|
||
func decodePostRequest(ctx context.Context, r interface{}) (interface{}, error) { | ||
return endpoint2.PostRequest{URL: r.(*pb.PostRequest).GetDomain()}, nil | ||
} | ||
|
||
func decodeGetRequest(_ context.Context, r interface{}) (interface{}, error) { | ||
return endpoint2.GetRequest{Code: r.(*pb.GetRequest).Code}, nil | ||
} | ||
|
||
func encodeResponse(_ context.Context, r interface{}) (interface{}, error) { | ||
resp := r.(endpoint2.GetResponse) | ||
var ( | ||
errStr string | ||
err error | ||
dataResponse *pb.ResponseData | ||
) | ||
if resp.Err != nil { | ||
errStr = resp.Err.Error() | ||
err = resp.Err | ||
} | ||
if resp.Data != nil { | ||
data := resp.Data.(*service.Redirect) | ||
dataResponse = &pb.ResponseData{ | ||
Url: data.URL, | ||
Code: data.Code, | ||
ShortUri: data.URL, | ||
} | ||
} | ||
|
||
return &pb.ServiceResponse{ | ||
Data: dataResponse, | ||
Err: errStr, | ||
}, err | ||
} | ||
|
||
func encodePostResponse(_ context.Context, r interface{}) (interface{}, error) { | ||
resp := r.(endpoint2.PostResponse) | ||
var ( | ||
errStr string | ||
err error | ||
) | ||
if resp.Err != nil { | ||
errStr = resp.Err.Error() | ||
err = resp.Err | ||
} | ||
return &pb.ServiceResponse{ | ||
Data: &pb.ResponseData{ | ||
Url: resp.Data.Url, | ||
Code: resp.Data.Code, | ||
ShortUri: resp.Data.ShortUri, | ||
}, | ||
Err: errStr, | ||
}, err | ||
} |
Oops, something went wrong.