Create Flask app on init of Server class

This commit is contained in:
CasVT
2025-08-17 16:34:49 +02:00
parent 39d5c9e51e
commit c9719ec221
4 changed files with 14 additions and 13 deletions

View File

@@ -63,7 +63,6 @@ def _main(
set_db_location(db_folder)
SERVER = Server()
SERVER.create_app()
with SERVER.app.app_context():
StartTypeHandlers.start_timer(start_type)
setup_db()

View File

@@ -64,11 +64,16 @@ class Server(metaclass=Singleton):
def __init__(self) -> None:
self.__start_type = None
self.app = self._create_app()
return
def create_app(self) -> None:
"""Creates an flask app instance that can be used to start a web server"""
@staticmethod
def _create_app() -> Flask:
"""Creates a flask app instance that can be used to start a web server.
Returns:
Flask: The instance.
"""
from frontend.api import admin_api, api
from frontend.ui import render, ui
@@ -125,8 +130,7 @@ class Server(metaclass=Singleton):
# Setup db handling
app.teardown_appcontext(close_db)
self.app = app
return
return app
def set_url_prefix(self, url_prefix: str) -> None:
"""Change the URL prefix of the server.
@@ -139,7 +143,7 @@ class Server(metaclass=Singleton):
Flask(__name__),
{url_prefix: self.app.wsgi_app}
)
self.url_prefix = url_prefix
self.__class__.url_prefix = url_prefix
return
def __create_waitress_server(

View File

@@ -10,11 +10,10 @@ from backend.internals.server import Server
ui = Blueprint('ui', __name__)
methods = ['GET']
SERVER = Server()
def render(filename: str, **kwargs: Any) -> str:
return render_template(filename, url_prefix=SERVER.url_prefix, **kwargs)
return render_template(filename, url_prefix=Server.url_prefix, **kwargs)
@ui.route('/manifest.json', methods=methods)
@@ -27,14 +26,14 @@ def ui_manifest():
"description": "MIND is a simple self hosted reminder application that can send push notifications to your device. Set the reminder and forget about it!",
"display": "standalone",
"orientation": "portrait-primary",
"start_url": f"{SERVER.url_prefix}/",
"scope": f"{SERVER.url_prefix}/",
"id": f"{SERVER.url_prefix}/",
"start_url": f"{Server.url_prefix}/",
"scope": f"{Server.url_prefix}/",
"id": f"{Server.url_prefix}/",
"background_color": "#1b1b1b",
"theme_color": "#6b6b6b",
"icons": [
{
"src": f"{SERVER.url_prefix}/static/img/favicon.svg",
"src": f"{Server.url_prefix}/static/img/favicon.svg",
"type": "image/svg+xml",
"sizes": "any"
}

View File

@@ -10,7 +10,6 @@ from frontend.ui import ui
class Test_MIND(unittest.TestCase):
def test_create_app(self):
SERVER = Server()
SERVER.create_app()
self.assertTrue(hasattr(SERVER, 'app'))
app = SERVER.app
self.assertIsInstance(app, Flask)