-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstart.py
More file actions
135 lines (105 loc) · 4.26 KB
/
Copy pathstart.py
File metadata and controls
135 lines (105 loc) · 4.26 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import logging
from datetime import timedelta
import sentry_sdk
from cachetools import TTLCache
from loguru import logger
from quart import Quart, session
import routers.contracts
import routers.cup
import routers.decision
import routers.federal
import routers.helpers
import routers.index
import routers.laboratory
import routers.mmwb
import routers.remote
import routers.sign_tools
import routers.web_editor
import routers.grist
import routers.rely
from other.config_reader import config, update_test_user
from db.sql_models import Base
from db.sql_pool import create_async_pool
app = Quart(__name__)
# Initialize DB pool
app.db_pool, app.db_engine = create_async_pool(config.db_dsn)
logger.add("log/app.log", level=logging.INFO)
# app.config['HOME_PATH'] = ''
# assets = Environment(app)
# assets.url = app.static_url_path
# scss = Bundle('css/main.css', filters='cssmin', output='css/main.min.css')
# assets.register('main_css', scss)
# sky say it must be bottom assets.register
app.config["SECRET_KEY"] = config.secret_key.get_secret_value()
app.config["EXPLAIN_TEMPLATE_LOADING"] = False
app.config["SESSION_COOKIE_SAMESITE"] = "Lax"
app.config["PERMANENT_SESSION_LIFETIME"] = timedelta(days=7)
app.register_blueprint(routers.index.blueprint)
app.register_blueprint(routers.laboratory.blueprint)
# app.register_blueprint(routers.federal.blueprint)
app.register_blueprint(routers.federal.cors_enabled_blueprint)
app.register_blueprint(routers.sign_tools.blueprint)
app.register_blueprint(routers.contracts.blueprint)
app.register_blueprint(routers.helpers.blueprint)
app.register_blueprint(routers.decision.blueprint)
app.register_blueprint(routers.cup.blueprint)
app.register_blueprint(routers.remote.blueprint)
app.register_blueprint(routers.web_editor.blueprint)
app.register_blueprint(routers.mmwb.blueprint)
app.register_blueprint(routers.grist.blueprint)
app.register_blueprint(routers.rely.blueprint)
# @app.context_processor
# def inject_assets():
# return dict(assets=assets)
# locale.setlocale(locale.LC_ALL, 'en_GB.UTF-8')
# Создаем TTL кеш: максимум 100 ключей, время жизни ключа - 1 минута
error_cache = TTLCache(maxsize=100, ttl=timedelta(hours=1).total_seconds())
def before_send(event, hint):
error_type = event.get("exception", {}).get("values", [{}])[0].get("type", "")
error_value = event.get("exception", {}).get("values", [{}])[0].get("value", "")
error_key = f"{error_type}:{error_value}"
if error_key in error_cache:
# Если ошибка уже в кеше, не отправляем ее снова
return None
# Если ошибки нет в кеше, добавляем ее и отправляем событие
error_cache[error_key] = True
return event
sentry_sdk.init(
dsn=config.sentry_dsn,
traces_sample_rate=0.1,
profiles_sample_rate=0.1,
before_send=before_send,
)
@app.route("/updatedb")
async def update_db():
# Base.metadata.drop_all(engine, tables=[Signatures])
# DROP TABLE ` t_signatures `
# session.execute('DROP TABLE t_signatures')
# session.execute('DROP TABLE t_transactions')
# session.execute('DROP TABLE t_signers')
# session.commit()
async with app.db_engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
return "OK"
@app.before_request
async def before_request():
session.permanent = True
if "userdata" not in session: # Чтобы избежать перезаписи сессии на каждом запросе
update_test_user()
@app.before_serving
async def initialize_grist_cache():
"""Инициализация кеша Grist при запуске приложения"""
from other.grist_cache import grist_cache
if not config.test_mode:
await grist_cache.initialize_cache()
if __name__ == "__main__":
if config.test_mode:
app.run(host="0.0.0.0", port=config.port, debug=True)
else:
import uvicorn
try:
import uvloop
uvloop.install() # Заменяет стандартный event loop
except ImportError:
pass # Если uvloop не установлен, используем стандартный
uvicorn.run(app, host="0.0.0.0", port=config.port, access_log=False)