Commit 26fd5be2 authored by 崔为之's avatar 崔为之 💪🏽

Update project

parent 187a55b6
...@@ -7,12 +7,22 @@ ...@@ -7,12 +7,22 @@
# @Email : StudentCWZ@outlook.com # @Email : StudentCWZ@outlook.com
# @Date : 2023/11/19 16:05 # @Date : 2023/11/19 16:05
# @File : __init__.py # @File : __init__.py
# @Description : # @Description : Registers the logging views with the Flask application.
""" """
from flask import Flask from flask import Flask
from .log import log_api from .log import log_api
def register_log_views(app: Flask): def register_log_views(app: Flask):
"""
Registers the log API blueprint with the Flask application.
Args:
app (Flask): The Flask application.
Returns:
None
"""
app.register_blueprint(log_api) app.register_blueprint(log_api)
...@@ -7,10 +7,10 @@ ...@@ -7,10 +7,10 @@
# @Email : StudentCWZ@outlook.com # @Email : StudentCWZ@outlook.com
# @Date : 2023/11/19 16:05 # @Date : 2023/11/19 16:05
# @File : log.py # @File : log.py
# @Description : # @Description : Defines the log API endpoints.
""" """
from flask import Blueprint, current_app, request, jsonify from flask import Blueprint, jsonify, request
from flask_restful import Api from flask_restful import Api
from loguru import logger from loguru import logger
from pydantic import ValidationError from pydantic import ValidationError
...@@ -19,24 +19,33 @@ from application.schemas import ParseLogRequestItem ...@@ -19,24 +19,33 @@ from application.schemas import ParseLogRequestItem
from application.services import LogService from application.services import LogService
# Define the log API blueprint
log_api = Blueprint('log_api', __name__) log_api = Blueprint('log_api', __name__)
api_logs = Api(log_api) api_logs = Api(log_api)
@log_api.route('/logs', methods=['POST']) @log_api.route('/logs', methods=['POST'])
def parse(): def parse():
"""
Parses the logs based on the request.
Returns:
The total number of logs parsed or an error message if the input data is invalid.
"""
json_data = request.get_json(force=True) json_data = request.get_json(force=True)
# If no data provided, return error message
if not json_data: if not json_data:
logger.error('No input data provided') logger.error('No input data provided')
return jsonify({"message": "No input data provided"}), 400 return jsonify({"message": "No input data provided"}), 400
try: try:
# Validate input data
item = ParseLogRequestItem(**json_data) item = ParseLogRequestItem(**json_data)
except ValidationError as e: except ValidationError as e:
# If validation fails, return error message
return jsonify({"message": "Invalid input data", "errors": e.errors()}), 400 return jsonify({"message": "Invalid input data", "errors": e.errors()}), 400
# If validation passes, parse logs and return total number of logs parsed
total = LogService.parse(item) total = LogService.parse(item)
return jsonify( return jsonify(search_total=total), 200
search_total=total
), 200
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment