2020-02-12 18:27:11 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-03-28 13:00:26 +02:00
|
|
|
"log"
|
2020-02-12 18:27:11 +01:00
|
|
|
|
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"
|
|
|
|
"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-04-25 01:30:19 +02:00
|
|
|
// The Iris server should ran under TLS (it's a gRPC requirement).
|
2020-04-28 04:22:58 +02:00
|
|
|
// POST: https://localhost:443/helloworld.Greeter/SayHello
|
2020-03-07 11:53:23 +01:00
|
|
|
// 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-04-26 05:21:20 +02:00
|
|
|
// app.Configure(iris.WithLowercaseRouting) // OPTIONAL.
|
|
|
|
|
|
|
|
app.Get("/", func(ctx iris.Context) {
|
|
|
|
ctx.HTML("<h1>Index Page</h1>")
|
|
|
|
})
|
2020-02-12 18:27:11 +01:00
|
|
|
|
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-04-25 01:30:19 +02:00
|
|
|
// serviceName := pb.File_helloworld_proto.Services().Get(0).FullName()
|
|
|
|
|
2020-05-04 19:23:15 +02:00
|
|
|
// Register MVC application controller for gRPC services.
|
|
|
|
// You can bind as many mvc gRpc services in the same Party or app,
|
|
|
|
// as the ServiceName differs.
|
2022-03-28 13:00:26 +02:00
|
|
|
mvc.New(app).
|
|
|
|
Register(new(myService)).
|
|
|
|
Handle(ctrl, mvc.GRPC{
|
|
|
|
Server: grpcServer, // Required.
|
|
|
|
ServiceName: "helloworld.Greeter", // Required.
|
|
|
|
Strict: false,
|
|
|
|
})
|
2020-03-07 11:53:23 +01:00
|
|
|
|
2020-02-12 18:27:11 +01:00
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
2022-03-28 13:00:26 +02:00
|
|
|
type service interface {
|
|
|
|
DoSomething() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type myService struct{}
|
|
|
|
|
|
|
|
func (s *myService) DoSomething() error {
|
|
|
|
log.Println("service: DoSomething")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-28 04:22:58 +02:00
|
|
|
type myController struct {
|
|
|
|
// Ctx iris.Context
|
2022-03-28 13:00:26 +02:00
|
|
|
|
|
|
|
SingletonDependency service
|
2020-04-28 04:22:58 +02:00
|
|
|
}
|
2020-02-12 18:27:11 +01:00
|
|
|
|
2020-04-25 01:30:19 +02:00
|
|
|
// SayHello implements helloworld.GreeterServer.
|
2020-05-08 02:55:54 +02:00
|
|
|
// See https://github.com/kataras/iris/issues/1449#issuecomment-625570442
|
|
|
|
// for the comments below (https://github.com/iris-contrib/swagger).
|
|
|
|
//
|
|
|
|
// @Description greet service
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {string} string "Hello {name}"
|
|
|
|
// @Router /helloworld.Greeter/SayHello [post]
|
2020-04-25 01:30:19 +02:00
|
|
|
func (c *myController) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
|
2022-03-28 13:00:26 +02:00
|
|
|
err := c.SingletonDependency.DoSomething()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-07 11:53:23 +01:00
|
|
|
return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
|
2020-02-12 18:27:11 +01:00
|
|
|
}
|