-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a full gRPC example as previously requested at: #1449
- Loading branch information
Showing
11 changed files
with
414 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Package main implements a client for Greeter service. | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"os" | ||
"time" | ||
|
||
pb "github.com/kataras/iris/v12/_examples/mvc/grpc-compatible/helloworld" | ||
|
||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials" | ||
) | ||
|
||
const ( | ||
address = "localhost:443" | ||
defaultName = "world" | ||
) | ||
|
||
func main() { | ||
// Set up a connection to the server. | ||
cred, err := credentials.NewClientTLSFromFile("../server.crt", "localhost") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
conn, err := grpc.Dial(address, grpc.WithTransportCredentials(cred), grpc.WithBlock()) | ||
if err != nil { | ||
log.Fatalf("did not connect: %v", err) | ||
} | ||
defer conn.Close() | ||
c := pb.NewGreeterClient(conn) | ||
|
||
// Contact the server and print out its response. | ||
name := defaultName | ||
if len(os.Args) > 1 { | ||
name = os.Args[1] | ||
} | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second) | ||
defer cancel() | ||
r, err := c.PostHello(ctx, &pb.HelloRequest{Name: name}) | ||
if err != nil { | ||
log.Fatalf("could not greet: %v", err) | ||
} | ||
log.Printf("Greeting: %s", r.GetMessage()) | ||
} |
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,3 @@ | ||
# Helloworld gRPC Example | ||
|
||
https://github.com/grpc/grpc-go/tree/master/examples/helloworld |
210 changes: 210 additions & 0 deletions
210
_examples/mvc/grpc-compatible/helloworld/helloworld.pb.go
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,23 @@ | ||
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 PostHello (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; | ||
} |
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,50 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"crypto/tls" | ||
"crypto/x509" | ||
"encoding/json" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
|
||
pb "github.com/kataras/iris/v12/_examples/mvc/grpc-compatible/helloworld" | ||
) | ||
|
||
func main() { | ||
b, err := ioutil.ReadFile("../server.crt") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
cp := x509.NewCertPool() | ||
if !cp.AppendCertsFromPEM(b) { | ||
log.Fatal("credentials: failed to append certificates") | ||
} | ||
|
||
transport := &http.Transport{ | ||
TLSClientConfig: &tls.Config{ | ||
RootCAs: cp, | ||
}, | ||
} | ||
|
||
client := http.Client{Transport: transport} | ||
buf := new(bytes.Buffer) | ||
err = json.NewEncoder(buf).Encode(pb.HelloRequest{Name: "world"}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
resp, err := client.Post("https://localhost/hello", "application/json", buf) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
var reply pb.HelloReply | ||
err = json.NewDecoder(resp.Body).Decode(&reply) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
log.Printf("Greeting: %s", reply.GetMessage()) | ||
} |
Oops, something went wrong.