1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-18 18:37:05 +00:00

add a full gRPC example as previously requested at: https://github.com/kataras/iris/issues/1449

Former-commit-id: 0cb5121e7d44644f7f0eb34597ff34274157fe95
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-03-07 12:53:23 +02:00
parent dd18dc9ee8
commit aea836efc7
11 changed files with 414 additions and 24 deletions

View File

@@ -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())
}