Created
July 7, 2017 12:37
-
-
Save pphetra/fe59ce68e97a458e049c60b4891bcd90 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package main | |
| import ( | |
| "log" | |
| "net/http" | |
| "golang.org/x/net/context" | |
| "google.golang.org/grpc" | |
| "github.com/gin-gonic/gin" | |
| ) | |
| const ( | |
| address = "localhost:50051" | |
| defaultName = "world" | |
| ) | |
| func main() { | |
| r := gin.Default() | |
| r.GET("/ping", func(c *gin.Context) { | |
| conn, err := grpc.Dial(address, grpc.WithInsecure()) | |
| if err != nil { | |
| log.Fatalf("did not connect: %v", err) | |
| } | |
| defer conn.Close() | |
| client := NewProductServiceClient(conn) | |
| result, err := client.List(context.Background(), &Params{}) | |
| if err != nil { | |
| c.Status(http.StatusInternalServerError) | |
| return | |
| } | |
| c.JSON(http.StatusOK, result) | |
| }) | |
| r.Run(":8888") | |
| } |
This file contains hidden or 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
| syntax = "proto3"; | |
| package main; | |
| service ProductService { | |
| rpc List (Params) returns (ProductList) {} | |
| } | |
| message ProductList { | |
| repeated Product product = 1; | |
| } | |
| message Product { | |
| int32 id = 1; | |
| string name = 2; | |
| } | |
| message Params { | |
| } |
This file contains hidden or 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
| package main | |
| import ( | |
| "log" | |
| "net" | |
| "golang.org/x/net/context" | |
| "google.golang.org/grpc" | |
| "google.golang.org/grpc/reflection" | |
| ) | |
| const ( | |
| port = ":50051" | |
| ) | |
| // server is used to implement helloworld.GreeterServer. | |
| type server struct{} | |
| func (s *server) List(ctx context.Context, in *Params) (*ProductList, error) { | |
| return &ProductList{ | |
| Product: []*Product{ | |
| &Product{ | |
| Id: 1, | |
| Name: "product-1", | |
| }, | |
| &Product{ | |
| Id: 2, | |
| Name: "product-2", | |
| }, | |
| }, | |
| }, nil | |
| } | |
| func main() { | |
| lis, err := net.Listen("tcp", port) | |
| if err != nil { | |
| log.Fatalf("failed to listen: %v", err) | |
| } | |
| s := grpc.NewServer() | |
| RegisterProductServiceServer(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) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment