mirror of
https://github.com/kataras/iris.git
synced 2025-12-23 04:47:02 +00:00
New: gRPC MVC features, new WithLowercaseRouting option and add some new context methods
read HISTORY.md Former-commit-id: 30a16cceb11f754aa32923058abeda1e736350e7
This commit is contained in:
66
mvc/grpc.go
Normal file
66
mvc/grpc.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package mvc
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"path"
|
||||
|
||||
"github.com/kataras/iris/v12/context"
|
||||
)
|
||||
|
||||
// GRPC registers a controller which serves gRPC clients.
|
||||
// It accepts the controller ptr to a struct value,
|
||||
// the gRPCServer itself, and a strict option which is explained below.
|
||||
//
|
||||
// The differences by a common controller are:
|
||||
// HTTP verb: only POST (Party.AllowMethods can be used for more),
|
||||
// method parsing is disabled: path is the function name as it is,
|
||||
// if 'strictMode' option is true then this controller will only serve gRPC-based clients
|
||||
// and fires 404 on common HTTP clients,
|
||||
// otherwise HTTP clients can send and receive JSON (protos contain json struct fields by-default).
|
||||
type GRPC struct {
|
||||
// Server is required and should be gRPC Server derives from google's grpc package.
|
||||
Server http.Handler
|
||||
// ServiceName is required and should be the name of the service (used to build the gRPC route path),
|
||||
// e.g. "helloworld.Greeter".
|
||||
// For a controller's method of "SayHello" and ServiceName "helloworld.Greeter",
|
||||
// both gRPC and common HTTP request path is: "/helloworld.Greeter/SayHello".
|
||||
//
|
||||
// Tip: the ServiceName can be fetched through proto's file descriptor, e.g.
|
||||
// serviceName := pb.File_helloworld_proto.Services().Get(0).FullName().
|
||||
ServiceName string
|
||||
|
||||
// When Strict option is true then this controller will only serve gRPC-based clients
|
||||
// and fires 404 on common HTTP clients.
|
||||
Strict bool
|
||||
}
|
||||
|
||||
// Apply parses the controller's methods and registers gRPC handlers to the application.
|
||||
func (g GRPC) Apply(c *ControllerActivator) {
|
||||
defer c.Activated()
|
||||
|
||||
pre := func(ctx context.Context) {
|
||||
if ctx.IsGRPC() { // gRPC, consumes and produces protobuf.
|
||||
g.Server.ServeHTTP(ctx.ResponseWriter(), ctx.Request())
|
||||
ctx.StopExecution()
|
||||
return
|
||||
}
|
||||
|
||||
if g.Strict {
|
||||
ctx.NotFound()
|
||||
} else {
|
||||
// Allow common HTTP clients, consumes and produces JSON.
|
||||
ctx.Next()
|
||||
}
|
||||
}
|
||||
|
||||
for i := 0; i < c.Type.NumMethod(); i++ {
|
||||
m := c.Type.Method(i)
|
||||
path := path.Join(g.ServiceName, m.Name)
|
||||
if route := c.Handle(http.MethodPost, path, m.Name, pre); route != nil {
|
||||
route.Description = "gRPC"
|
||||
if g.Strict {
|
||||
route.Description = "-only"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user