How to run grpc-gateway for python
Asked Answered
L

3

5

I am grpc rookie and wanted to create a REST endpoint for health check of the grpc server. For this I decided to use grpc gateway.

However, the example in docs for grpc-REST gateway for proxying grpc to json are only given for Golang. But I have a backend of python and want to use Google Cloud Endpoints to make a gRPC based api that can transcode incoming REST requests.

I did find a manual method of annotation generation in this stackoverflow answer . But I was wondering what is the best method of doing it. This is the sample .proto file I want to generate into a class.

 syntax = "proto3";
 package example;
+
+import "google/api/annotations.proto";
+
 message StringMessage {
   string value = 1;
 }

 service YourService {
-  rpc Echo(StringMessage) returns (StringMessage) {}
+  rpc Echo(StringMessage) returns (StringMessage) {
+    option (google.api.http) = {
+      post: "/v1/example/echo"
+      body: "*"
+    };
+  }
 }

How can I generate .proto files and create the grpc-gateway using python backend?

Lanark answered 27/9, 2020 at 10:49 Comment(2)
Found a solution for generating the GoogleAPI annotation .proto files here : #46131522Lanark
No I have to set up the reverse proxy in PythonLanark
H
7

grpc-gateway is a project that generates a reverse proxy that translates incoming JSON requests to gRPC. To do so, it generates Go code which is then compiled into the reverse proxy binary. You then deploy the reverse proxy binary alongside your intended backend , which would be written in Python in your case. The language the reverse proxy is written in should have no bearing on the language your backend is written in.

Hexose answered 30/9, 2020 at 20:59 Comment(0)
L
0

For proto generation, please take a look at our quick start guide for gRPC Python. grpc-gateway project also has a nice tutorial for how to setup the gateway process.

Lamellicorn answered 30/9, 2020 at 17:48 Comment(0)
P
0

Here's my example repo on how you can run Python gRPC Server and a grpc-gateway proxy for transcoding your api also to REST.

The server is implemented in python, and the gateway is a simple Go proxy (just one class).

Check it out here: https://github.com/angelkjos/grpc-gateway-python-example

Petronel answered 30/7, 2023 at 7:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.