-
Notifications
You must be signed in to change notification settings - Fork 43
/
flask_app.py
101 lines (83 loc) · 3.05 KB
/
flask_app.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""Main Flask application."""
import json
import logging
from pathlib import Path
import sys
import arroba.server
from arroba import xrpc_repo, xrpc_server, xrpc_sync
from flask import Flask, g
import flask_gae_static
from lexrpc.server import Server
import lexrpc.flask_server
from oauth_dropins.webutil import (
appengine_info,
appengine_config,
flask_util,
)
import common
logger = logging.getLogger(__name__)
logging.getLogger('negotiator').setLevel(logging.WARNING)
app_dir = Path(__file__).parent
app = Flask(__name__, static_folder=None)
app.template_folder = './templates'
app.json.compact = False
app.config.from_pyfile(app_dir / 'config.py')
app.url_map.converters['regex'] = flask_util.RegexConverter
app.after_request(flask_util.default_modern_headers)
app.register_error_handler(Exception, flask_util.handle_exception)
if (appengine_info.LOCAL_SERVER
# ugly hack to infer if we're running unit tests
and 'unittest' not in sys.modules):
flask_gae_static.init_app(app)
# don't redirect API requests with blank path elements
app.url_map.merge_slashes = False
app.url_map.redirect_defaults = False
app.wsgi_app = flask_util.ndb_context_middleware(
app.wsgi_app, client=appengine_config.ndb_client, **common.NDB_CONTEXT_KWARGS)
# deregister XRPC methods we don't support
for nsid in (
'com.atproto.repo.applyWrites',
'com.atproto.repo.createRecord',
'com.atproto.repo.deleteRecord',
'com.atproto.repo.putRecord',
'com.atproto.repo.uploadBlob',
'com.atproto.server.createSession',
'com.atproto.server.getAccountInviteCodes',
'com.atproto.server.getSession',
'com.atproto.server.listAppPasswords',
'com.atproto.server.refreshSession',
):
del arroba.server.server._methods[nsid]
lexrpc.flask_server.init_flask(arroba.server.server, app)
###########################################
# https://github.com/googleapis/python-ndb/issues/743#issuecomment-2067590945
#
# fixes "RuntimeError: Key has already been set in this batch" errors due to
# tasklets in pages.serve_feed
from logging import error as log_error
from sys import modules
from google.cloud.datastore_v1.types.entity import Key
from google.cloud.ndb._cache import (
_GlobalCacheSetBatch,
global_compare_and_swap,
global_set_if_not_exists,
global_watch,
)
from google.cloud.ndb.tasklets import Future, Return, tasklet
GLOBAL_CACHE_KEY_PREFIX: bytes = modules["google.cloud.ndb._cache"]._PREFIX
LOCKED_FOR_READ: bytes = modules["google.cloud.ndb._cache"]._LOCKED_FOR_READ
LOCK_TIME: bytes = modules["google.cloud.ndb._cache"]._LOCK_TIME
@tasklet
def custom_global_lock_for_read(key: str, value: str):
if value is not None:
yield global_watch(key, value)
lock_acquired = yield global_compare_and_swap(
key, LOCKED_FOR_READ, expires=LOCK_TIME
)
else:
lock_acquired = yield global_set_if_not_exists(
key, LOCKED_FOR_READ, expires=LOCK_TIME
)
if lock_acquired:
raise Return(LOCKED_FOR_READ)
modules["google.cloud.ndb._cache"].global_lock_for_read = custom_global_lock_for_read