Spaces:
Sleeping
Sleeping
init
Browse files
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
RAG ๊ฒ์ ์ฑ๋ด ๋ฉ์ธ ์คํ ํ์ผ
|
3 |
+
"""
|
4 |
+
|
5 |
+
# os ๋ชจ๋ ์ํฌํธ
|
6 |
+
import os
|
7 |
+
import logging
|
8 |
+
|
9 |
+
# ๋ก๊น
์ค์
|
10 |
+
logging.basicConfig(
|
11 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
12 |
+
level=logging.INFO
|
13 |
+
)
|
14 |
+
logger = logging.getLogger(__name__)
|
15 |
+
|
16 |
+
try:
|
17 |
+
# ์ฑ ๋ชจ๋์์ Flask ์ฑ ๊ฐ์ ธ์ค๊ธฐ
|
18 |
+
from app.app import app
|
19 |
+
|
20 |
+
# ์ฅ์น ๋ผ์ฐํธ ๋ฑ๋ก ์ฝ๋ ์ถ๊ฐ
|
21 |
+
try:
|
22 |
+
from flask_cors import CORS
|
23 |
+
from app.app_device_routes import register_device_routes
|
24 |
+
|
25 |
+
# CORS ์ค์
|
26 |
+
CORS(app, supports_credentials=True)
|
27 |
+
|
28 |
+
# ์ฅ์น ์๋ฒ URL ํ๊ฒฝ ๋ณ์ (๋๋ฌธ์๋ก ๋ณ๊ฒฝ)
|
29 |
+
DEVICE_SERVER_URL = os.getenv('DEVICE_SERVER_URL', 'http://localhost:5050')
|
30 |
+
logger.info(f"์ฅ์น ์๋ฒ URL: {DEVICE_SERVER_URL}")
|
31 |
+
|
32 |
+
# ์ธ์ฆ ๋ฐ์ฝ๋ ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
|
33 |
+
from app.app import login_required
|
34 |
+
|
35 |
+
# ์ธ์ฆ ๋ฐ์ฝ๋ ์ดํฐ ์ค๋ฒ๋ผ์ด๋ (์ฅ์น API ์ธ์ฆ ์ฐํ)
|
36 |
+
from functools import wraps
|
37 |
+
from flask import request, session, redirect, url_for
|
38 |
+
|
39 |
+
def device_login_required(f):
|
40 |
+
@wraps(f)
|
41 |
+
def decorated_function(*args, **kwargs):
|
42 |
+
# API ์์ฒญ์ด๊ณ ํด๋ผ์ด์ธํธ์์ ์ค๋ ๊ฒฝ์ฐ ์ธ์ฆ ๋ฌด์ (์์ ์กฐ์น)
|
43 |
+
if request.path.startswith('/api/device/'):
|
44 |
+
logger.info(f"์ฅ์น API ์์ฒญ: {request.path} - ์ธ์ฆ ์ ์ธ")
|
45 |
+
return f(*args, **kwargs)
|
46 |
+
|
47 |
+
# ๊ทธ ์ธ์๋ ๊ธฐ์กด login_required ์ฌ์ฉ
|
48 |
+
return login_required(f)(*args, **kwargs)
|
49 |
+
return decorated_function
|
50 |
+
|
51 |
+
# ์ฅ์น ๋ผ์ฐํธ ๋ฑ๋ก (๋๋ฌธ์ ๋ณ์๋ช
์ฌ์ฉ)
|
52 |
+
register_device_routes(app, device_login_required, DEVICE_SERVER_URL)
|
53 |
+
logger.info("์ฅ์น ๋ผ์ฐํธ ์ง์ ๋ฑ๋ก ์ฑ๊ณต")
|
54 |
+
|
55 |
+
# 404 ์ค๋ฅ ํธ๋ค๋ฌ ๋ฑ๋ก
|
56 |
+
@app.errorhandler(404)
|
57 |
+
def not_found(e):
|
58 |
+
# ํด๋ผ์ด์ธํธ๊ฐ JSON์ ๊ธฐ๋ํ๋ API ํธ์ถ์ธ ๊ฒฝ์ฐ JSON ์๋ต
|
59 |
+
from flask import request, jsonify
|
60 |
+
if request.path.startswith('/api/'):
|
61 |
+
return jsonify({"success": False, "error": "์์ฒญํ API ์๋ํฌ์ธํธ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค."}), 404
|
62 |
+
# ์ผ๋ฐ ์น ํ์ด์ง ์์ฒญ์ธ ๊ฒฝ์ฐ HTML ์๋ต
|
63 |
+
return "ํ์ด์ง๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.", 404
|
64 |
+
except Exception as e:
|
65 |
+
logger.error(f"์ฅ์น ๋ผ์ฐํธ ๋ฑ๋ก ์คํจ: {e}", exc_info=True)
|
66 |
+
|
67 |
+
except ImportError as e:
|
68 |
+
logger.error(f"์ฑ ๋ชจ๋ ๊ฐ์ ธ์ค๊ธฐ ์คํจ: {e}")
|
69 |
+
raise
|
70 |
+
|
71 |
+
if __name__ == '__main__':
|
72 |
+
port = int(os.environ.get("PORT", 7860))
|
73 |
+
logger.info(f"์๋ฒ๋ฅผ http://0.0.0.0:{port} ์์ ์์ํฉ๋๋ค.")
|
74 |
+
app.run(debug=False, host='0.0.0.0', port=port)
|