2020-02-12 18:27:11 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-03-07 11:53:23 +01:00
|
|
|
pb "github.com/kataras/iris/v12/_examples/mvc/grpc-compatible/helloworld"
|
|
|
|
|
2020-02-12 18:27:11 +01:00
|
|
|
"github.com/kataras/iris/v12"
|
2020-03-07 11:53:23 +01:00
|
|
|
grpcWrapper "github.com/kataras/iris/v12/middleware/grpc"
|
2020-02-12 18:27:11 +01:00
|
|
|
"github.com/kataras/iris/v12/mvc"
|
2020-03-07 11:53:23 +01:00
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
2020-02-12 18:27:11 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// See https://github.com/kataras/iris/issues/1449
|
2020-02-29 13:18:15 +01:00
|
|
|
// 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.
|
2020-02-12 18:27:11 +01:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
app := newApp()
|
|
|
|
app.Logger().SetLevel("debug")
|
|
|
|
|
2020-03-07 11:53:23 +01:00
|
|
|
// POST: https://localhost/hello
|
|
|
|
// with request data: {"name": "John"}
|
|
|
|
// and expected output: {"message": "Hello John"}
|
|
|
|
app.Run(iris.TLS(":443", "server.crt", "server.key"))
|
2020-02-12 18:27:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func newApp() *iris.Application {
|
|
|
|
app := iris.New()
|
|
|
|
|
2020-03-07 11:53:23 +01:00
|
|
|
ctrl := &myController{}
|
|
|
|
// Register gRPC server.
|
|
|
|
grpcServer := grpc.NewServer()
|
|
|
|
pb.RegisterGreeterServer(grpcServer, ctrl)
|
2020-02-12 18:27:11 +01:00
|
|
|
|
2020-03-07 11:53:23 +01:00
|
|
|
// Register MVC application controller.
|
|
|
|
mvc.New(app).Handle(ctrl)
|
|
|
|
|
|
|
|
// Serve the gRPC server under the Iris HTTP webserver one,
|
|
|
|
// the Iris server should ran under TLS (it's a gRPC requirement).
|
|
|
|
app.WrapRouter(grpcWrapper.New(grpcServer))
|
2020-02-12 18:27:11 +01:00
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
|
|
|
type myController struct{}
|
|
|
|
|
2020-03-07 11:53:23 +01:00
|
|
|
// PostHello implements helloworld.GreeterServer
|
|
|
|
func (c *myController) PostHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
|
|
|
|
return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
|
2020-02-12 18:27:11 +01:00
|
|
|
}
|