This commit is contained in:
h z
2024-12-02 19:50:34 +00:00
parent 83c5662fcb
commit 5557b3434b
7 changed files with 94 additions and 15 deletions

View File

@@ -2,6 +2,8 @@
from flask import Blueprint, session, redirect, url_for, jsonify
from authlib.integrations.flask_client import OAuth
import env_provider
import logging
logger = logging.getLogger(__name__)
auth_bp = Blueprint('auth', __name__, url_prefix='/api')
oauth = OAuth()
@@ -26,10 +28,13 @@ def authorize():
session['user'] = user_info
return jsonify({"message": "login successful", "user": user_info})
except Exception as e:
logger.error(f"Authorization failed: {str(e)}")
return jsonify({"error": "Authorization failed"}), 401
@auth_bp.route('/logout', methods=['GET'])
def logout():
session.pop('user', None)
u = session.pop('user', None)
if u:
logger.info(f"Logged out user: {u}")
logout_url = "https://login.hangman-lab.top/auth/realms/Hangman-Lab/protocol/openid-connect/logout"
return redirect(logout_url)

43
api/log.py Normal file
View File

@@ -0,0 +1,43 @@
#api/log.py
from flask import Blueprint, jsonify, request
from db import get_db
from db.models.Log import Log
from db.utils import insert_log
logs_bp = Blueprint('log', __name__, url_prefix='/api/log')
@logs_bp.route('/', methods=['GET'])
def get_logs():
level = request.args.get('level')
application = request.args.get('application')
page = int(request.args.get('page', 1))
per_page = int(request.args.get('per_page', 10))
with get_db() as db:
query = db.query(Log)
if level:
query = query.filter(Log.level == level)
if application:
query = query.filter(Log.application == application)
total_logs = query.count()
logs = query.order_by(Log.timestamp.desc()).offset((page - 1)*per_page).limit(per_page).all()
return jsonify({
"total": total_logs,
"page": page,
"per_page": per_page,
"logs": [log.to_dict() for log in logs]
})
@logs_bp.route('/', methods=['POST'])
def create_log():
data = request.json
required_fields = ['level', 'message']
for field in required_fields:
if field not in data:
return jsonify({"error": f"missing {field} in request"}), 400
level = data.get('level')
message = data.get('message')
application = "frontend"
extra = data.get('extra', None)
log_entry = Log(level=level, message=message, application=application, extra=extra)
insert_log(log_entry)

View File

@@ -2,6 +2,8 @@
from flask import Blueprint, request, jsonify
from db import get_db
from db.models.Markdown import Markdown
import logging
logger = logging.getLogger(__name__)
markdown_bp = Blueprint('markdown', __name__, url_prefix='/api/markdown')
@@ -23,9 +25,14 @@ def create_markdown():
return jsonify({"error": "missing required fields"}), 400
new_markdown = Markdown(title=title, content=content, path=path)
with get_db() as db:
db.add(new_markdown)
db.commit()
return jsonify(new_markdown.to_dict()), 201
try:
db.add(new_markdown)
db.commit()
return jsonify(new_markdown.to_dict()), 201
except Exception as e:
logger.error(f"failed to create markdown: {e}")
db.rollback()
return jsonify({"error": "create failed"}), 500
@markdown_bp.route('/<int:markdown_id>', methods=['PUT'])
def update_markdown(markdown_id):