-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
main.go
64 lines (49 loc) · 1.77 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"context"
pb "github.com/kataras/iris/v12/_examples/mvc/grpc-compatible/helloworld"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/mvc"
"google.golang.org/grpc"
)
// See https://github.com/kataras/iris/issues/1449
// Iris automatically binds the standard "context" context.Context to `iris.Context.Request().Context()`
// and any other structure that is not mapping to a registered dependency
// as a payload depends on the request, e.g XML, YAML, Query, Form, JSON.
//
// Useful to use gRPC services as Iris controllers fast and without wrappers.
func main() {
app := newApp()
app.Logger().SetLevel("debug")
// The Iris server should ran under TLS (it's a gRPC requirement).
// POST: https://localhost:443/helloworld.Greeter/SayHello
// with request data: {"name": "John"}
// and expected output: {"message": "Hello John"}
app.Run(iris.TLS(":443", "server.crt", "server.key"))
}
func newApp() *iris.Application {
app := iris.New()
// app.Configure(iris.WithLowercaseRouting) // OPTIONAL.
app.Get("/", func(ctx iris.Context) {
ctx.HTML("<h1>Index Page</h1>")
})
ctrl := &myController{}
// Register gRPC server.
grpcServer := grpc.NewServer()
pb.RegisterGreeterServer(grpcServer, ctrl)
// serviceName := pb.File_helloworld_proto.Services().Get(0).FullName()
// Register MVC application controller.
mvc.New(app).Handle(ctrl, mvc.GRPC{
Server: grpcServer, // Required.
ServiceName: "helloworld.Greeter", // Required.
Strict: false,
})
return app
}
type myController struct {
// Ctx iris.Context
}
// SayHello implements helloworld.GreeterServer.
func (c *myController) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
}