-
Notifications
You must be signed in to change notification settings - Fork 8.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add grpc example #1401
Merged
Merged
add grpc example #1401
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cc26f8e
add grpc example
thinkerou 3e14617
add readme
thinkerou acaeb9d
add grpc package
thinkerou 8806dc0
fix fmt
thinkerou f5556fc
fix some code view suggest
thinkerou d6fab8d
fix: ignore examples folder for go vet check
appleboy ee2262b
fix: ignore examples folder
appleboy a37a344
improve code readable
thinkerou 73ef02f
add return for missing
thinkerou 368c63c
reduce indent
thinkerou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,19 @@ | ||
## How to run this example | ||
|
||
1. run grpc server | ||
|
||
```sh | ||
$ go run grpc/server.go | ||
``` | ||
|
||
2. run gin server | ||
|
||
```sh | ||
$ go run gin/main.go | ||
``` | ||
|
||
3. use curl command to test it | ||
|
||
```sh | ||
$ curl -v 'http://localhost:8052/rest/n/thinkerou' | ||
``` |
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,46 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
pb "github.com/gin-gonic/gin/examples/grpc/pb" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
func main() { | ||
// Set up a connection to the server. | ||
conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure()) | ||
if err != nil { | ||
log.Fatalf("did not connect: %v", err) | ||
} | ||
defer conn.Close() | ||
client := pb.NewGreeterClient(conn) | ||
|
||
// Set up a http setver. | ||
r := gin.Default() | ||
r.GET("/rest/n/:name", func(c *gin.Context) { | ||
name := c.Param("name") | ||
|
||
// Contact the server and print out its response. | ||
req := &pb.HelloRequest{Name: name} | ||
res, err := client.SayHello(g, req) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{ | ||
"error": err.Error(), | ||
}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{ | ||
"result": fmt.Sprint(res.Message), | ||
}) | ||
}) | ||
|
||
// Run http server | ||
if err := r.Run(":8052"); err != nil { | ||
log.Fatalf("could not run server: %v", err) | ||
} | ||
} |
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,34 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"net" | ||
|
||
pb "github.com/gin-gonic/gin/examples/grpc/pb" | ||
"golang.org/x/net/context" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/reflection" | ||
) | ||
|
||
// server is used to implement helloworld.GreeterServer. | ||
type server struct{} | ||
|
||
// SayHello implements helloworld.GreeterServer | ||
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) { | ||
return &pb.HelloReply{Message: "Hello " + in.Name}, nil | ||
} | ||
|
||
func main() { | ||
lis, err := net.Listen("tcp", ":50051") | ||
if err != nil { | ||
log.Fatalf("failed to listen: %v", err) | ||
} | ||
s := grpc.NewServer() | ||
pb.RegisterGreeterServer(s, &server{}) | ||
|
||
// Register reflection service on gRPC server. | ||
reflection.Register(s) | ||
if err := s.Serve(lis); err != nil { | ||
log.Fatalf("failed to serve: %v", err) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,37 @@ | ||
// Copyright 2015 gRPC authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
syntax = "proto3"; | ||
|
||
option java_multiple_files = true; | ||
option java_package = "io.grpc.examples.helloworld"; | ||
option java_outer_classname = "HelloWorldProto"; | ||
|
||
package helloworld; | ||
|
||
// The greeting service definition. | ||
service Greeter { | ||
// Sends a greeting | ||
rpc SayHello (HelloRequest) returns (HelloReply) {} | ||
} | ||
|
||
// The request message containing the user's name. | ||
message HelloRequest { | ||
string name = 1; | ||
} | ||
|
||
// The response message containing the greetings | ||
message HelloReply { | ||
string message = 1; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing
return
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, thanks a lot, fixed.