Skip to content

Commit

Permalink
Try exposing flask app to gunicorn
Browse files Browse the repository at this point in the history
  • Loading branch information
aabmass committed Aug 9, 2024
1 parent 23a39ac commit a9b2f54
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
3 changes: 0 additions & 3 deletions e2e-test-server/app.yaml

This file was deleted.

25 changes: 20 additions & 5 deletions e2e-test-server/e2e_test_server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import json
import logging
from typing import Literal

Expand All @@ -20,6 +21,9 @@
from google.cloud import pubsub_v1
from google.cloud.pubsub_v1.subscriber.message import Message
from google.rpc import code_pb2
from flask import Flask, Response, request
from waitress import serve


from . import scenarios, types
from .constants import (
Expand Down Expand Up @@ -146,16 +150,21 @@ def pubsub_callback(message: Message) -> None:
streaming_pull_future.result()


def pubsub_push() -> None:
"""Start a web server listening for incoming pub/sub push message"""
from flask import Flask, Response, request
from waitress import serve

def create_flask_app() -> Flask:
app = Flask(__name__)
responder = _Responder()

@app.route("/", methods=["POST"])
def index() -> Response:
print(
json.dumps(
{
"message": f"got request {request=}",
"request": request,
"is_json": request.is_json,
}
)
)
if not request.is_json:
return Response("Expected a JSON payload", status=400)
payload = types.PubsubPushPayload(**request.json)
Expand All @@ -168,6 +177,12 @@ def index() -> Response:
else:
return Response(status=400)

return app


def pubsub_push() -> None:
"""Start a web server listening for incoming pub/sub push message"""
app = create_flask_app()
serve(app, port=PUSH_PORT)


Expand Down
8 changes: 7 additions & 1 deletion e2e-test-server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
from e2e_test_server.server import cloud_functions_handler
from e2e_test_server.constants import SUBSCRIPTION_MODE, SubscriptionMode

app = None

if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)

Expand All @@ -26,5 +28,9 @@
elif SUBSCRIPTION_MODE is SubscriptionMode.PUSH:
server.pubsub_push()

else:
# being started by gunicorn
app = server.create_flask_app()

# Expose handler to functions-framework
__all__ = ["cloud_functions_handler"]
__all__ = ["app", "cloud_functions_handler"]

0 comments on commit a9b2f54

Please sign in to comment.