mirror of
https://github.com/kataras/iris.git
synced 2025-03-15 17:36:29 +01:00
Add a gRPC router wrapper, useful for the upcoming grpc controller example
Former-commit-id: ae4c29597ce1336a74278b86b59d08b6c91eab0a
This commit is contained in:
parent
0d26f24eb7
commit
dd18dc9ee8
|
@ -52,3 +52,8 @@ func main() {
|
||||||
iris.WithoutServerError(iris.ErrServerClosed),
|
iris.WithoutServerError(iris.ErrServerClosed),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
If you want to enable CORS in your websocket handler,
|
||||||
|
please follow this post: https://github.com/googollee/go-socket.io/issues/242
|
||||||
|
*/
|
30
middleware/grpc/grpc.go
Normal file
30
middleware/grpc/grpc.go
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
package grpc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/kataras/iris/v12/core/router"
|
||||||
|
)
|
||||||
|
|
||||||
|
// New returns a new gRPC Iris router wrapper for a gRPC server.
|
||||||
|
// useful when you want to share one port (such as 443 for https) between gRPC and Iris.
|
||||||
|
//
|
||||||
|
// The Iris server SHOULD run under HTTP/2 and clients too.
|
||||||
|
//
|
||||||
|
// Usage:
|
||||||
|
// import grpcWrapper "github.com/kataras/iris/v12/middleware/grpc"
|
||||||
|
// [...]
|
||||||
|
// app := iris.New()
|
||||||
|
// grpcServer := grpc.NewServer()
|
||||||
|
// app.WrapRouter(grpcWrapper.New(grpcServer))
|
||||||
|
func New(grpcServer http.Handler) router.WrapperFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request, mux http.HandlerFunc) {
|
||||||
|
if r.ProtoMajor == 2 && strings.HasPrefix(
|
||||||
|
r.Header.Get("Content-Type"), "application/grpc") {
|
||||||
|
grpcServer.ServeHTTP(w, r)
|
||||||
|
} else {
|
||||||
|
mux.ServeHTTP(w, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user