init commit
This commit is contained in:
71
app/__init__.py
Normal file
71
app/__init__.py
Normal file
@@ -0,0 +1,71 @@
|
||||
import os
|
||||
from flask import Flask, request, g, jsonify, current_app
|
||||
from app.utils.typed_flask import CoCFlask
|
||||
from typing import cast
|
||||
|
||||
from app.services.appwrite_client import AppWriteClient
|
||||
from app.utils.api_response import ApiResponder
|
||||
|
||||
from app.blueprints.main import main_bp
|
||||
from app.blueprints.char import char_bp
|
||||
|
||||
from app.utils.settings import get_settings
|
||||
|
||||
settings = get_settings()
|
||||
|
||||
def create_app() -> CoCFlask:
|
||||
app = CoCFlask(__name__)
|
||||
|
||||
app.config.update(
|
||||
SECRET_KEY=settings.flask_secret_key,
|
||||
APPWRITE_ENDPOINT=settings.appwrite_endpoint,
|
||||
APPWRITE_PROJECT_ID=settings.appwrite_project_id,
|
||||
APPWRITE_API_KEY=settings.appwrite_api_key,
|
||||
)
|
||||
|
||||
default_headers = {
|
||||
"Cache-Control": "no-store",
|
||||
"Content-Type": "application/json; charset=utf-8",
|
||||
# Add CORS if needed:
|
||||
# "Access-Control-Allow-Origin": "*",
|
||||
}
|
||||
|
||||
full_app_name = f"{settings.app_name} - v {settings.app_version}"
|
||||
app.api = ApiResponder(
|
||||
app_name=full_app_name,
|
||||
version_provider=settings.app_version,
|
||||
include_request_id=True,
|
||||
default_headers=default_headers
|
||||
)
|
||||
|
||||
app.api.register_error_handlers(app)
|
||||
|
||||
if not app.config["APPWRITE_ENDPOINT"] or not app.config["APPWRITE_PROJECT_ID"]:
|
||||
raise RuntimeError("Missing APPWRITE_ENDPOINT or APPWRITE_PROJECT_ID")
|
||||
|
||||
# Blueprints
|
||||
app.register_blueprint(main_bp)
|
||||
app.register_blueprint(char_bp)
|
||||
|
||||
@app.before_request
|
||||
def authenticate():
|
||||
auth = request.headers.get("Authorization", "")
|
||||
if not auth.startswith("Bearer "):
|
||||
return app.api.unauthorized("Missing Bearer Token")
|
||||
|
||||
token = auth.split(" ", 1)[1].strip()
|
||||
try:
|
||||
aw = AppWriteClient()
|
||||
user = aw.get_user_from_jwt_token(token)
|
||||
if isinstance(user,str):
|
||||
return app.api.unauthorized("Invalid or Expired JWT")
|
||||
except Exception as e:
|
||||
return app.api.unauthorized("Invalid or Expired JWT")
|
||||
|
||||
# make available to routes
|
||||
g.appwrite_user = user
|
||||
return None
|
||||
|
||||
|
||||
|
||||
return app
|
||||
Reference in New Issue
Block a user