public / sonora

A gRPC-Web implementation for Python
Apache License 2.0
243 stars 13 forks source link

Flask example #37

Closed gitpushdashf closed 3 years ago

gitpushdashf commented 3 years ago

Hi,

Do you have an example of this with Flask?

Thank you!

gitpushdashf commented 3 years ago

I'm combining Sonora and Flask with https://github.com/grpc/grpc-web/tree/master/net/grpc/gateway/examples/helloworld to test things out.

from flask import Flask, send_from_directory
from sonora.wsgi import grpcWSGI

import helloworld_pb2
import helloworld_pb2_grpc

FORMAT_STRING = "Hello, {request.name}"

app = Flask(__name__)

class Greeter(helloworld_pb2_grpc.GreeterServicer):
    def SayHello(self, request, context):
        return helloworld_pb2.HelloReply(message=FORMAT_STRING.format(request=request))

    def SayRepeatHello(self, request, context):
        return helloworld_pb2.HelloReply(
            message=FORMAT_STRING.format(request=request) * request.count
        )

@app.route("/")
def index():
    return send_from_directory(".", "index.html")

@app.route("/<path:path>")
def foo(path):
    if path == "/":
        path = "index.html"
    # This appears to filter ../ for us.
    return send_from_directory(".", path)

if __name__ == "__main__":
    app.wsgi_app = grpcWSGI(app.wsgi_app)
    helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), app.wsgi_app)
    app.run()

I'm getting this.

(.venv) grpc-web-python-example % python3 server.py
 * Serving Flask app "server" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [22/Apr/2021 14:03:50] "GET / HTTP/1.1" 304 -
127.0.0.1 - - [22/Apr/2021 14:03:50] "GET /dist/main.js HTTP/1.1" 304 -
127.0.0.1 - - [22/Apr/2021 14:03:50] "POST /helloworld.Greeter/SayHello HTTP/1.1" 500 -
127.0.0.1 - - [22/Apr/2021 14:03:50] "POST /helloworld.Greeter/SayRepeatHello HTTP/1.1" 500 -
Error on request:
Traceback (most recent call last):
  File ".venv/lib/python3.8/site-packages/werkzeug/serving.py", line 323, in run_wsgi
    execute(self.server.app)
  File ".venv/lib/python3.8/site-packages/werkzeug/serving.py", line 314, in execute
    for data in application_iter:
  File ".venv/lib/python3.8/site-packages/sonora/wsgi.py", line 106, in _do_grpc_request
    _, _, message = protocol.unrwap_message(request_data)
  File ".venv/lib/python3.8/site-packages/sonora/protocol.py", line 36, in unrwap_message
    raise ValueError()
ValueError
Error on request:
Traceback (most recent call last):
  File ".venv/lib/python3.8/site-packages/werkzeug/serving.py", line 323, in run_wsgi
    execute(self.server.app)
  File ".venv/lib/python3.8/site-packages/werkzeug/serving.py", line 314, in execute
    for data in application_iter:
  File ".venv/lib/python3.8/site-packages/sonora/wsgi.py", line 106, in _do_grpc_request
    _, _, message = protocol.unrwap_message(request_data)
  File ".venv/lib/python3.8/site-packages/sonora/protocol.py", line 36, in unrwap_message
    raise ValueError()
ValueError

Do you have any suggestions on what I could try?

Thank you!

gitpushdashf commented 3 years ago

One thing to note is that I'm using 0.1.0. I didn't see a git tag for 0.1.1 and didn't realize that one was out. I will give 0.1.1 a try.

gitpushdashf commented 3 years ago

Same error, except the "unrwap_message" typo was fixed.

gitpushdashf commented 3 years ago

If I add message = base64.b64decode(message) to unwrap_message(), I'm able to process requests and read request.name.

However in the browser console, I get this: Unexpected error for sayHello: code = 2, message = "Incomplete response"

public commented 3 years ago

Are you using the improbably grpc web implementation? You've probably hit https://github.com/public/sonora/issues/33 if so. I guess I should finally fix it :)

public commented 3 years ago

Can you share a reproduction?

gitpushdashf commented 3 years ago

Thanks for getting back to me! What do you mean by a reproduction? Would you like a repo with all of the code in it?

public commented 3 years ago

Yeah. What client are you using, what type of RPCs etc?

The interop tests I have seem to pass fine so I am guessing some newer version of grpc-web has created an incompatibility.

public commented 3 years ago

So the base64 thing and the client side error you've hit is because you're using mode=grpcwebtext which I have never implemented in this project, looks like it would be easy to add support though.

public commented 3 years ago

I have made some progress with this. Should have a PR next week.

gitpushdashf commented 3 years ago

That's great, thank you! I am just using Firefox and the grpc-web example.

Here's what I've been using for testing: https://github.com/gitpushdashf/grpc-web-python-example