Moved API prefixes from Server to Constants

This commit is contained in:
CasVT
2025-08-12 16:57:13 +02:00
parent 8739e1ab95
commit b5387a0635
5 changed files with 22 additions and 22 deletions

View File

@@ -42,6 +42,9 @@ class Constants:
HOSTING_THREADS = 10
HOSTING_REVERT_TIME = 60.0 # seconds
API_PREFIX = "/api"
ADMIN_API_EXTENSION = "/admin"
ADMIN_PREFIX = API_PREFIX + ADMIN_API_EXTENSION
DB_FOLDER = ("db",)
DB_NAME = "MIND.db"

View File

@@ -55,9 +55,6 @@ class ThreadedTaskDispatcher(TTD):
# region Server
class Server(metaclass=Singleton):
api_prefix = "/api"
admin_api_extension = "/admin"
admin_prefix = "/api/admin"
url_prefix = ''
def __init__(self) -> None:
@@ -87,7 +84,9 @@ class Server(metaclass=Singleton):
@app.errorhandler(404)
def not_found(e):
if request.path.startswith((self.api_prefix, self.admin_prefix)):
if request.path.startswith(
(Constants.API_PREFIX, Constants.ADMIN_PREFIX)
):
return {'error': "NotFound", "result": {}}, 404
return render("page_not_found.html")
@@ -101,8 +100,8 @@ class Server(metaclass=Singleton):
# Add endpoints
app.register_blueprint(ui)
app.register_blueprint(api, url_prefix=self.api_prefix)
app.register_blueprint(admin_api, url_prefix=self.admin_prefix)
app.register_blueprint(api, url_prefix=Constants.API_PREFIX)
app.register_blueprint(admin_api, url_prefix=Constants.ADMIN_PREFIX)
# Setup db handling
app.teardown_appcontext(close_db)

View File

@@ -14,15 +14,14 @@ import frontend.api
from backend.base.custom_exceptions import (DatabaseFileNotFound,
NotificationServiceNotFound,
ReminderNotFound, TemplateNotFound)
from backend.base.definitions import MindException, StartType
from backend.base.definitions import Constants, MindException, StartType
from backend.base.helpers import folder_path
from backend.internals.server import Server
from frontend.input_validation import API_DOCS, DataSource, InputVariable
# autopep8: on
API_PREFIX = Server.api_prefix
ADMIN_PREFIX = Server.admin_prefix
API_PREFIX = Constants.API_PREFIX
ADMIN_PREFIX = Constants.ADMIN_PREFIX
API_FILE = folder_path('docs', 'src', 'other_docs', 'api.md')
url_var_map = {

View File

@@ -12,7 +12,7 @@ from flask import Response, g, request, send_file
from backend.base.custom_exceptions import (AccessUnauthorized,
APIKeyExpired, APIKeyInvalid,
LogFileNotFound, UserNotFound)
from backend.base.definitions import (ApiKeyEntry, MindException,
from backend.base.definitions import (ApiKeyEntry, Constants, MindException,
SendResult, Serialisable, StartType)
from backend.base.helpers import folder_path
from backend.base.logging import LOGGER, get_log_filepath
@@ -81,7 +81,7 @@ def auth() -> None:
if (
user_data.admin
and not request.path.startswith(
(Server.admin_prefix, Server.api_prefix + '/auth')
(Constants.ADMIN_PREFIX, Constants.API_PREFIX + '/auth')
)
):
raise APIKeyInvalid(api_key)
@@ -89,7 +89,7 @@ def auth() -> None:
if (
not user_data.admin
and
request.path.startswith(Server.admin_prefix)
request.path.startswith(Constants.ADMIN_PREFIX)
):
raise APIKeyInvalid(api_key)

View File

@@ -21,11 +21,10 @@ from backend.base.custom_exceptions import (AccessUnauthorized,
KeyNotFound, NewAccountsNotAllowed,
NotificationServiceNotFound,
UsernameInvalid, UsernameTaken)
from backend.base.definitions import (DataSource, DataType, MindException,
RepeatQuantity, SortingMethod,
TimelessSortingMethod)
from backend.base.definitions import (Constants, DataSource, DataType,
MindException, RepeatQuantity,
SortingMethod, TimelessSortingMethod)
from backend.base.helpers import folder_path
from backend.internals.server import Server
from backend.internals.settings import SettingsValues
if TYPE_CHECKING:
@@ -918,13 +917,13 @@ def get_api_docs(request: Request) -> Type[EndpointData]:
"""
assert request.url_rule is not None
if request.path.startswith(Server.admin_prefix):
if request.path.startswith(Constants.ADMIN_PREFIX):
url = (
Server.admin_api_extension +
request.url_rule.rule.split(Server.admin_prefix)[1]
Constants.ADMIN_API_EXTENSION +
request.url_rule.rule.split(Constants.ADMIN_PREFIX)[1]
)
else:
url = request.url_rule.rule.split(Server.api_prefix)[1]
url = request.url_rule.rule.split(Constants.API_PREFIX)[1]
return API_DOCS[url]
@@ -986,7 +985,7 @@ class APIBlueprint(Blueprint):
if self == api:
processed_rule = rule
elif self == admin_api:
processed_rule = Server.admin_api_extension + rule
processed_rule = Constants.ADMIN_API_EXTENSION + rule
else:
raise NotImplementedError