59 lines
2.7 KiB
Python
59 lines
2.7 KiB
Python
import traceback
|
|
|
|
from sanic import Sanic, response
|
|
from app.core.logger import make_log
|
|
|
|
app = Sanic(__name__)
|
|
|
|
from app.api.middleware import attach_user_to_request, close_db_session, close_request_handler
|
|
|
|
app.register_middleware(attach_user_to_request, "request")
|
|
app.register_middleware(close_db_session, "response")
|
|
|
|
from app.api.routes._index import s_index, s_favicon
|
|
from app.api.routes._system import s_api_system, s_api_system_version, s_api_system_send_status
|
|
from app.api.routes.auth import s_api_v1_auth_twa
|
|
from app.api.routes.statics import s_api_tonconnect_manifest, s_api_platform_metadata
|
|
from app.api.routes.node_storage import s_api_v1_storage_post, s_api_v1_storage_get
|
|
from app.api.routes.account import s_api_v1_account_get
|
|
from app.api.routes._blockchain import s_api_v1_blockchain_send_new_content_message, \
|
|
s_api_v1_blockchain_send_purchase_content_message
|
|
from app.api.routes.content import s_api_v1_content_list
|
|
|
|
app.add_route(s_index, "/", methods=["GET", "OPTIONS"])
|
|
app.add_route(s_favicon, "/favicon.ico", methods=["GET", "OPTIONS"])
|
|
|
|
app.add_route(s_api_system, "/api/node", methods=["GET", "OPTIONS"])
|
|
app.add_route(s_api_system_version, "/api/system.version", methods=["GET", "OPTIONS"])
|
|
app.add_route(s_api_system_send_status, "/api/system.sendStatus", methods=["POST", "OPTIONS"])
|
|
|
|
app.add_route(s_api_tonconnect_manifest, "/api/tonconnect-manifest.json", methods=["GET", "OPTIONS"])
|
|
app.add_route(s_api_platform_metadata, "/api/platform-metadata.json", methods=["GET", "OPTIONS"])
|
|
|
|
app.add_route(s_api_v1_auth_twa, "/api/v1/auth.twa", methods=["POST", "OPTIONS"])
|
|
|
|
app.add_route(s_api_v1_storage_post, "/api/v1/storage", methods=["POST", "OPTIONS"])
|
|
app.add_route(s_api_v1_storage_get, "/api/v1/storage/<file_hash>", methods=["GET", "OPTIONS"])
|
|
|
|
app.add_route(s_api_v1_account_get, "/api/v1/account", methods=["GET", "OPTIONS"])
|
|
|
|
app.add_route(s_api_v1_blockchain_send_new_content_message, "/api/v1/blockchain.sendNewContentMessage", methods=["POST", "OPTIONS"])
|
|
app.add_route(s_api_v1_blockchain_send_purchase_content_message, "/api/v1/blockchain.sendPurchaseContentMessage", methods=["POST", "OPTIONS"])
|
|
|
|
app.add_route(s_api_v1_content_list, "/api/v1/content.list", methods=["GET", "OPTIONS"])
|
|
|
|
|
|
@app.exception(BaseException)
|
|
async def s_handle_exception(request, exception):
|
|
response_buffer = response.json({"error": "An internal server error occurred"}, status=500)
|
|
try:
|
|
raise exception
|
|
except AssertionError as e:
|
|
response_buffer = response.json({"error": str(e)}, status=400)
|
|
except BaseException as e:
|
|
make_log("sanic_exception", f"Exception: {e}" + '\n' + str(traceback.format_exc()), level='error')
|
|
|
|
response_buffer = await close_db_session(request, response_buffer)
|
|
return response_buffer
|
|
|