|
1 | 1 | import base64
|
2 |
| -import os |
3 | 2 | import secrets
|
4 | 3 | import threading
|
5 | 4 | from typing import Generator, Sequence
|
|
12 | 11 | request,
|
13 | 12 | stream_with_context,
|
14 | 13 | )
|
15 |
| -from werkzeug.security import safe_join |
16 | 14 |
|
17 | 15 | import mesop.protos.ui_pb2 as pb
|
18 | 16 | from mesop.component_helpers import diff_component
|
|
23 | 21 | MESOP_WEBSOCKETS_ENABLED,
|
24 | 22 | )
|
25 | 23 | from mesop.events import LoadEvent
|
26 |
| -from mesop.exceptions import MesopDeveloperException, format_traceback |
| 24 | +from mesop.exceptions import format_traceback |
27 | 25 | from mesop.runtime import runtime
|
28 |
| -from mesop.server.config import app_config |
29 | 26 | from mesop.server.constants import WEB_COMPONENTS_PATH_SEGMENT
|
30 | 27 | from mesop.server.server_debug_routes import configure_debug_routes
|
31 | 28 | from mesop.server.server_utils import (
|
32 | 29 | STREAM_END,
|
33 | 30 | create_update_state_event,
|
| 31 | + get_static_folder, |
| 32 | + get_static_url_path, |
34 | 33 | is_same_site,
|
35 | 34 | make_sse_response,
|
36 | 35 | serialize,
|
|
44 | 43 | def configure_flask_app(
|
45 | 44 | *, prod_mode: bool = True, exceptions_to_propagate: Sequence[type] = ()
|
46 | 45 | ) -> Flask:
|
| 46 | + static_folder = get_static_folder() |
| 47 | + static_url_path = get_static_url_path() |
| 48 | + if static_folder and static_url_path: |
| 49 | + print(f"Static folder enabled: {static_folder}") |
| 50 | + |
47 | 51 | flask_app = Flask(
|
48 | 52 | __name__,
|
49 |
| - static_folder=get_static_folder(), |
50 |
| - static_url_path=get_static_url_path(), |
| 53 | + static_folder=static_folder, |
| 54 | + static_url_path=static_url_path, |
51 | 55 | )
|
52 | 56 |
|
53 | 57 | def render_loop(
|
@@ -322,40 +326,3 @@ def ws_generate_data(ws, ui_request):
|
322 | 326 | runtime().delete_context(websocket_session_id)
|
323 | 327 |
|
324 | 328 | return flask_app
|
325 |
| - |
326 |
| - |
327 |
| -def get_static_folder() -> str | None: |
328 |
| - static_folder_name = app_config.static_folder.strip() |
329 |
| - if not static_folder_name: |
330 |
| - print("Static folder disabled.") |
331 |
| - return None |
332 |
| - |
333 |
| - if static_folder_name in { |
334 |
| - ".", |
335 |
| - "..", |
336 |
| - "." + os.path.sep, |
337 |
| - ".." + os.path.sep, |
338 |
| - }: |
339 |
| - raise MesopDeveloperException( |
340 |
| - "Static folder cannot be . or ..: {static_folder_name}" |
341 |
| - ) |
342 |
| - if os.path.isabs(static_folder_name): |
343 |
| - raise MesopDeveloperException( |
344 |
| - "Static folder cannot be an absolute path: static_folder_name}" |
345 |
| - ) |
346 |
| - |
347 |
| - static_folder_path = safe_join(os.getcwd(), static_folder_name) |
348 |
| - |
349 |
| - if not static_folder_path: |
350 |
| - raise MesopDeveloperException( |
351 |
| - "Invalid static folder specified: {static_folder_name}" |
352 |
| - ) |
353 |
| - |
354 |
| - print(f"Static folder enabled: {static_folder_path}") |
355 |
| - return static_folder_path |
356 |
| - |
357 |
| - |
358 |
| -def get_static_url_path() -> str | None: |
359 |
| - if not app_config.static_folder: |
360 |
| - return None |
361 |
| - return app_config.static_url_path |
0 commit comments