10
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

echo製のサーバでgRPCも走らせる方法

Last updated at Posted at 2017-02-22

echoで作ってるRESTサーバにgRPCを足したかったのだけど、さっとやり方が見つからなかったのでメモ

こんな感じのechoのミドルウェアを作って

func GRPCMiddleware(grpcServer *grpc.Server) func(next echo.HandlerFunc) echo.HandlerFunc {
	return func(next echo.HandlerFunc) echo.HandlerFunc {
		return func(c echo.Context) error {
			r := c.Request()
			w := c.Response()
			if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") {
				grpcServer.ServeHTTP(w, r)
				return nil
			}
			if err := next(c); err != nil {
				c.Error(err)
			}
			return nil
		}
	}
}

使う

e.Use(GRPCMiddleware(gRPCServer))

認証とかロギング エラーハンドリングを一元管理できていいです。
そのうちjsからgRPC叩けるようなればgRPCだけでいいんでしょうけど、、

ネタ元
https://github.com/philips/grpc-gateway-example/blob/master/cmd/serve.go#L51

10
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?