Skip to content

Commit

Permalink
add a full gRPC example as previously requested at: #1449
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Mar 7, 2020
1 parent 5191b46 commit 3db1c90
Show file tree
Hide file tree
Showing 11 changed files with 414 additions and 24 deletions.
47 changes: 47 additions & 0 deletions _examples/mvc/grpc-compatible/grpc-client/main.go
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())
}
3 changes: 3 additions & 0 deletions _examples/mvc/grpc-compatible/helloworld/README.md
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 _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.

23 changes: 23 additions & 0 deletions _examples/mvc/grpc-compatible/helloworld/helloworld.proto
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;
}
50 changes: 50 additions & 0 deletions _examples/mvc/grpc-compatible/http-client/main.go
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())
}
Loading

0 comments on commit 3db1c90

Please sign in to comment.