jeongsoo commited on
Commit
69f0bf5
ยท
1 Parent(s): 2c2f318
Files changed (1) hide show
  1. app.py +74 -0
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)