This commit refactors the entire backend application into a more structured and maintainable Flask project. It introduces an application factory pattern, consolidates routes into a blueprint, and implements a robust authentication and database layer. - Introduces a Flask application factory (`create_app` in `main.py`) for better organization and testability. - Consolidates all API routes into a single blueprint (`routes/api.py`) for modularity. - Implements a new basic authentication system using a decorator (`@requires_basic_auth`) to secure all endpoints. - Refactors the database access layer with standardized query execution and connection handling. - Adds new modules for core logic, including an account checker (`checker.py`) and user retrieval (`get_users.py`). - Updates the VSCode launch configuration to support the new Flask application structure. BREAKING CHANGE: The application has been completely restructured. The old `server.py` entry point is removed. The application should now be run via the app factory in `main.py`. All API endpoints now require basic authentication.
39 lines
1.3 KiB
Python
39 lines
1.3 KiB
Python
from flask import Blueprint, jsonify
|
|
from ktvmanager.lib.database import get_user_accounts, get_stream_names, single_check, add_account, delete_account, get_user_id_from_username
|
|
from ktvmanager.lib.get_urls import get_latest_urls_from_dns
|
|
from ktvmanager.lib.auth import requires_basic_auth
|
|
|
|
api_blueprint = Blueprint("api", __name__)
|
|
|
|
@api_blueprint.route("/getUserAccounts")
|
|
@requires_basic_auth
|
|
def get_user_accounts_route(username):
|
|
user_id = get_user_id_from_username(username)
|
|
if user_id:
|
|
return get_user_accounts(user_id)
|
|
return jsonify({"message": "User not found"}), 404
|
|
|
|
@api_blueprint.route("/getStreamNames")
|
|
@requires_basic_auth
|
|
def get_stream_names_route(username):
|
|
return get_stream_names()
|
|
|
|
@api_blueprint.route("/getUserAccounts/streams")
|
|
@requires_basic_auth
|
|
def get_user_accounts_streams_route(username):
|
|
return jsonify(get_latest_urls_from_dns())
|
|
|
|
@api_blueprint.route("/singleCheck", methods=["POST"])
|
|
@requires_basic_auth
|
|
def single_check_route(username):
|
|
return single_check()
|
|
|
|
@api_blueprint.route("/addAccount", methods=["POST"])
|
|
@requires_basic_auth
|
|
def add_account_route(username):
|
|
return add_account()
|
|
|
|
@api_blueprint.route("/deleteAccount", methods=["POST"])
|
|
@requires_basic_auth
|
|
def delete_account_route(username):
|
|
return delete_account() |