Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import os
|
| 3 |
+
from werkzeug.serving import run_simple
|
| 4 |
+
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
|
| 7 |
+
load_dotenv(dotenv_path=os.path.join(os.path.dirname(__file__), ".env"))
|
| 8 |
+
#print("DEBUG_ENV_LOADED:", os.environ.get("SESSIONS_DIR_PATH"))
|
| 9 |
+
|
| 10 |
+
from flask import Flask
|
| 11 |
+
from flask_cors import CORS
|
| 12 |
+
from constants import Constants
|
| 13 |
+
#print("DEBUG_CONSTANT:", Constants.SESSIONS_DIR_NAME)
|
| 14 |
+
|
| 15 |
+
from api.api_blueprint import api_blueprint
|
| 16 |
+
from models.base import db
|
| 17 |
+
from models.combined_labels import CombinedLabels
|
| 18 |
+
|
| 19 |
+
import logging
|
| 20 |
+
import os
|
| 21 |
+
from constants import Constants
|
| 22 |
+
from api.api_blueprint import api_blueprint
|
| 23 |
+
from flask import Flask
|
| 24 |
+
from flask_cors import CORS
|
| 25 |
+
|
| 26 |
+
# 先確保 session 目錄有合理預設值
|
| 27 |
+
SESSION_DIR = Constants.SESSIONS_DIR_NAME or "/app/sessions"
|
| 28 |
+
Constants.SESSIONS_DIR_NAME = SESSION_DIR
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def create_session_dir():
|
| 32 |
+
path = Constants.SESSIONS_DIR_NAME
|
| 33 |
+
# 用 makedirs 比較安全,資料夾不存在就建立,存在就跳過
|
| 34 |
+
if not os.path.isdir(path):
|
| 35 |
+
os.makedirs(path, exist_ok=True)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def create_app():
|
| 39 |
+
create_session_dir()
|
| 40 |
+
|
| 41 |
+
app = Flask(__name__)
|
| 42 |
+
app.register_blueprint(api_blueprint, url_prefix=f"{Constants.BASE_PATH}/api")
|
| 43 |
+
|
| 44 |
+
class FilterProgressRequests(logging.Filter):
|
| 45 |
+
def filter(self, record):
|
| 46 |
+
return "/api/progress/" not in record.getMessage()
|
| 47 |
+
|
| 48 |
+
logging.getLogger("werkzeug").addFilter(FilterProgressRequests())
|
| 49 |
+
|
| 50 |
+
CORS(app)
|
| 51 |
+
|
| 52 |
+
return app
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
app = create_app()
|
| 57 |
+
|
| 58 |
+
# ✅ SharedArrayBuffer Compatibility
|
| 59 |
+
@app.after_request
|
| 60 |
+
def add_security_headers(response):
|
| 61 |
+
response.headers["Cross-Origin-Opener-Policy"] = "same-origin"
|
| 62 |
+
response.headers["Cross-Origin-Embedder-Policy"] = "require-corp"
|
| 63 |
+
return response
|
| 64 |
+
|
| 65 |
+
def find_watch_files():
|
| 66 |
+
watch_dirs = ['api', 'models', 'services']
|
| 67 |
+
base_path = os.path.dirname(__file__)
|
| 68 |
+
all_files = []
|
| 69 |
+
for d in watch_dirs:
|
| 70 |
+
dir_path = os.path.join(base_path, d)
|
| 71 |
+
for root, _, files in os.walk(dir_path):
|
| 72 |
+
for f in files:
|
| 73 |
+
if f.endswith('.py'):
|
| 74 |
+
all_files.append(os.path.join(root, f))
|
| 75 |
+
return all_files
|
| 76 |
+
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
use_ssl = os.environ.get("USE_SSL", "false").lower() == "true"
|
| 79 |
+
ssl_context = ("../certs/localhost-cert.pem", "../certs/localhost-key.pem") if use_ssl else None
|
| 80 |
+
run_simple(
|
| 81 |
+
hostname="0.0.0.0",
|
| 82 |
+
port=5001,
|
| 83 |
+
application=app,
|
| 84 |
+
use_debugger=True,
|
| 85 |
+
use_reloader=True,
|
| 86 |
+
extra_files=find_watch_files(),
|
| 87 |
+
ssl_context=ssl_context
|
| 88 |
+
)
|