|
|
|
|
|
|
|
|
@app.route('/login', methods=['GET', 'POST']) |
|
|
def login(): |
|
|
error = None |
|
|
next_url = request.args.get('next') |
|
|
logger.info(f"-------------- λ‘κ·ΈμΈ νμ΄μ§ μ μ (Next: {next_url}) --------------") |
|
|
logger.info(f"Method: {request.method}") |
|
|
|
|
|
if request.method == 'POST': |
|
|
logger.info("λ‘κ·ΈμΈ μλ λ°μ") |
|
|
username = request.form.get('username', '') |
|
|
password = request.form.get('password', '') |
|
|
logger.info(f"μ
λ ₯λ μ¬μ©μλͺ
: {username}") |
|
|
logger.info(f"λΉλ°λ²νΈ μ
λ ₯ μ¬λΆ: {len(password) > 0}") |
|
|
|
|
|
|
|
|
valid_username = ADMIN_USERNAME |
|
|
valid_password = ADMIN_PASSWORD |
|
|
logger.info(f"κ²μ¦μ© μ¬μ©μλͺ
: {valid_username}") |
|
|
logger.info(f"κ²μ¦μ© λΉλ°λ²νΈ μ‘΄μ¬ μ¬λΆ: {valid_password is not None and len(valid_password) > 0}") |
|
|
|
|
|
if username == valid_username and password == valid_password: |
|
|
logger.info(f"λ‘κ·ΈμΈ μ±κ³΅: {username}") |
|
|
|
|
|
logger.debug(f"μΈμ
μ€μ μ : {session}") |
|
|
|
|
|
|
|
|
session.permanent = True |
|
|
session['logged_in'] = True |
|
|
session['username'] = username |
|
|
session.modified = True |
|
|
|
|
|
logger.info(f"μΈμ
μ€μ ν: {session}") |
|
|
logger.info("μΈμ
μ€μ μλ£, 리λλ μ
μλ") |
|
|
|
|
|
|
|
|
redirect_to = next_url or url_for('index') |
|
|
logger.info(f"리λλ μ
λμ: {redirect_to}") |
|
|
response = redirect(redirect_to) |
|
|
return response |
|
|
else: |
|
|
logger.warning("λ‘κ·ΈμΈ μ€ν¨: μμ΄λ λλ λΉλ°λ²νΈ λΆμΌμΉ") |
|
|
if username != valid_username: logger.warning("μ¬μ©μλͺ
λΆμΌμΉ") |
|
|
if password != valid_password: logger.warning("λΉλ°λ²νΈ λΆμΌμΉ") |
|
|
error = 'μμ΄λ λλ λΉλ°λ²νΈκ° μ¬λ°λ₯΄μ§ μμ΅λλ€.' |
|
|
else: |
|
|
logger.info("λ‘κ·ΈμΈ νμ΄μ§ GET μμ²") |
|
|
if 'logged_in' in session: |
|
|
logger.info("μ΄λ―Έ λ‘κ·ΈμΈλ μ¬μ©μ, λ©μΈ νμ΄μ§λ‘ 리λλ μ
") |
|
|
return redirect(url_for('index')) |
|
|
|
|
|
logger.info("---------- λ‘κ·ΈμΈ νμ΄μ§ λ λλ§ ----------") |
|
|
return render_template('login.html', error=error, next=next_url) |
|
|
|
|
|
|
|
|
@app.route('/logout') |
|
|
def logout(): |
|
|
logger.info("-------------- λ‘κ·Έμμ μμ² --------------") |
|
|
logger.info(f"λ‘κ·Έμμ μ μΈμ
μν: {session}") |
|
|
|
|
|
if 'logged_in' in session: |
|
|
username = session.get('username', 'unknown') |
|
|
logger.info(f"μ¬μ©μ {username} λ‘κ·Έμμ μ²λ¦¬ μμ") |
|
|
session.pop('logged_in', None) |
|
|
session.pop('username', None) |
|
|
session.modified = True |
|
|
logger.info(f"μΈμ
μ 보 μμ μλ£. νμ¬ μΈμ
: {session}") |
|
|
else: |
|
|
logger.warning("λ‘κ·ΈμΈλμ§ μμ μνμμ λ‘κ·Έμμ μλ") |
|
|
|
|
|
logger.info("λ‘κ·ΈμΈ νμ΄μ§λ‘ 리λλ μ
") |
|
|
response = redirect(url_for('login')) |
|
|
return response |
|
|
|
|
|
|
|
|
@app.route('/') |
|
|
@login_required |
|
|
def index(): |
|
|
"""λ©μΈ νμ΄μ§""" |
|
|
global app_ready |
|
|
|
|
|
|
|
|
current_time = datetime.now() |
|
|
start_time = datetime.fromtimestamp(os.path.getmtime(__file__)) |
|
|
time_diff = (current_time - start_time).total_seconds() |
|
|
|
|
|
if not app_ready and time_diff > 30: |
|
|
logger.warning(f"μ±μ΄ 30μ΄ μ΄μ μ΄κΈ°ν μ€ μνμ
λλ€. κ°μ λ‘ ready μνλ‘ λ³κ²½ν©λλ€.") |
|
|
app_ready = True |
|
|
|
|
|
if not app_ready: |
|
|
logger.info("μ±μ΄ μμ§ μ€λΉλμ§ μμ λ‘λ© νμ΄μ§ νμ") |
|
|
return render_template('loading.html'), 503 |
|
|
|
|
|
logger.info("λ©μΈ νμ΄μ§ μμ²") |
|
|
return render_template('index.html') |
|
|
|
|
|
|
|
|
@app.route('/api/status') |
|
|
@login_required |
|
|
def app_status(): |
|
|
"""μ± μ΄κΈ°ν μν νμΈ API""" |
|
|
logger.info(f"μ± μν νμΈ μμ²: {'Ready' if app_ready else 'Not Ready'}") |
|
|
return jsonify({"ready": app_ready}) |
|
|
|
|
|
|
|
|
@app.route('/api/llm', methods=['GET', 'POST']) |
|
|
@login_required |
|
|
def llm_api(): |
|
|
"""μ¬μ© κ°λ₯ν LLM λͺ©λ‘ λ° μ ν API""" |
|
|
global llm_interface |
|
|
|
|
|
if not app_ready: |
|
|
return jsonify({"error": "μ±μ΄ μμ§ μ΄κΈ°ν μ€μ
λλ€. μ μ ν λ€μ μλν΄μ£ΌμΈμ."}), 503 |
|
|
|
|
|
if request.method == 'GET': |
|
|
logger.info("LLM λͺ©λ‘ μμ²") |
|
|
try: |
|
|
current_details = llm_interface.get_current_llm_details() if hasattr(llm_interface, 'get_current_llm_details') else {"id": "unknown", "name": "Unknown"} |
|
|
supported_llms_dict = llm_interface.SUPPORTED_LLMS if hasattr(llm_interface, 'SUPPORTED_LLMS') else {} |
|
|
supported_list = [{ |
|
|
"name": name, "id": id, "current": id == current_details.get("id") |
|
|
} for name, id in supported_llms_dict.items()] |
|
|
|
|
|
return jsonify({ |
|
|
"supported_llms": supported_list, |
|
|
"current_llm": current_details |
|
|
}) |
|
|
except Exception as e: |
|
|
logger.error(f"LLM μ 보 μ‘°ν μ€λ₯: {e}") |
|
|
return jsonify({"error": "LLM μ 보 μ‘°ν μ€ μ€λ₯ λ°μ"}), 500 |
|
|
|
|
|
elif request.method == 'POST': |
|
|
data = request.get_json() |
|
|
if not data or 'llm_id' not in data: |
|
|
return jsonify({"error": "LLM IDκ° μ 곡λμ§ μμμ΅λλ€."}), 400 |
|
|
|
|
|
llm_id = data['llm_id'] |
|
|
logger.info(f"LLM λ³κ²½ μμ²: {llm_id}") |
|
|
|
|
|
try: |
|
|
if not hasattr(llm_interface, 'set_llm') or not hasattr(llm_interface, 'llm_clients'): |
|
|
raise NotImplementedError("LLM μΈν°νμ΄μ€μ νμν λ©μλ/μμ± μμ") |
|
|
|
|
|
if llm_id not in llm_interface.llm_clients: |
|
|
return jsonify({"error": f"μ§μλμ§ μλ LLM ID: {llm_id}"}), 400 |
|
|
|
|
|
success = llm_interface.set_llm(llm_id) |
|
|
if success: |
|
|
new_details = llm_interface.get_current_llm_details() |
|
|
logger.info(f"LLMμ΄ '{new_details.get('name', llm_id)}'λ‘ λ³κ²½λμμ΅λλ€.") |
|
|
return jsonify({ |
|
|
"success": True, |
|
|
"message": f"LLMμ΄ '{new_details.get('name', llm_id)}'λ‘ λ³κ²½λμμ΅λλ€.", |
|
|
"current_llm": new_details |
|
|
}) |
|
|
else: |
|
|
logger.error(f"LLM λ³κ²½ μ€ν¨ (ID: {llm_id})") |
|
|
return jsonify({"error": "LLM λ³κ²½ μ€ λ΄λΆ μ€λ₯ λ°μ"}), 500 |
|
|
except Exception as e: |
|
|
logger.error(f"LLM λ³κ²½ μ²λ¦¬ μ€ μ€λ₯: {e}", exc_info=True) |
|
|
return jsonify({"error": f"LLM λ³κ²½ μ€ μ€λ₯ λ°μ: {str(e)}"}), 500 |
|
|
|