-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
46 lines (34 loc) · 1.36 KB
/
main.py
File metadata and controls
46 lines (34 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import typing as t
import logging
import google.cloud.logging
from app import blueprints, event_handlers, exceptions
from flask import Flask, Request, Response
from python_settings import settings
if t.TYPE_CHECKING:
from google.cloud.functions_v1.context import Context
if settings.GCP_LOGGING:
client = google.cloud.logging.Client()
client.setup_logging()
else:
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger("main")
def handle_event(event: dict[str, t.Any], context: "Context") -> None:
"""Handle event from pubsub."""
logger.info("Handle event started")
for handler in event_handlers:
try:
logger.info(f"Handle event by {handler.__name__}")
handler(event, context)()
logger.info(f"Finished handle event by {handler.__name__}")
except exceptions.StopHandlingEvent:
logger.info("Got stop handling event from handler %s", handler.__name__)
break
logger.info("Handle event finished")
def handle_http_request(request: Request) -> Response:
"""Handle HTTP-requests."""
http_app = Flask(__name__)
for blueprint in blueprints:
http_app.register_blueprint(blueprint)
with http_app.request_context(request.environ):
logger.debug("Before request, req.environ=%s", request.environ)
return http_app.full_dispatch_request()