update
This commit is contained in:
1 parent
163f6cf48d
commit
353f1db25d
19 files changed
+205
-118
No files matched your search
+28
-10
@@ -1,5 +1,6 @@
|
||||
from flask import Flask, jsonify, render_template
|
||||
import os
|
||||
import logging
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
@@ -8,58 +9,75 @@ BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
PARENT_DIR = os.path.dirname(BASE_DIR) # 上一个文件夹
|
||||
SEC_NEWS_PATH = os.path.join(PARENT_DIR, 'history', 'sec_news.md')
|
||||
TECH_PASSAGE_PATH = os.path.join(PARENT_DIR, 'history', 'tech_passage.md')
|
||||
CORE_LOG_PATH = os.path.join(PARENT_DIR, 'log', 'core.log') # 新增日志文件路径
|
||||
CORE_LOG_PATH = os.path.join(PARENT_DIR, 'log', 'core.log')
|
||||
WEB_LOG_PATH = os.path.join(PARENT_DIR, 'log', 'app.log')
|
||||
|
||||
# 配置日志记录器
|
||||
logging.basicConfig(
|
||||
filename=WEB_LOG_PATH,
|
||||
level=logging.INFO,
|
||||
format= '%(asctime)s - %(levelname)s - %(message)s'
|
||||
)
|
||||
|
||||
# 替换输出内容
|
||||
def replace_content(content):
|
||||
content = content.replace('####', '###')
|
||||
content = content.replace(r"e:\Self-Tool-Code\PyBot", '.') # 修改: 使用原始字符串避免转义问题
|
||||
return content
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
logging.info("访问主页")
|
||||
return render_template('index.html')
|
||||
|
||||
@app.route('/get-sec-news')
|
||||
def get_sec_news():
|
||||
print(f"尝试打开安全新闻历史推送文件: {SEC_NEWS_PATH}")
|
||||
logging.info(f"尝试打开安全新闻历史推送文件: {SEC_NEWS_PATH}")
|
||||
try:
|
||||
with open(SEC_NEWS_PATH, 'r', encoding='utf-8') as file:
|
||||
content = file.read()
|
||||
content = replace_content(content)
|
||||
return jsonify({'content': content}), 200
|
||||
except FileNotFoundError:
|
||||
print(f"文件缺失: {SEC_NEWS_PATH}")
|
||||
logging.error(f"文件缺失: {SEC_NEWS_PATH}")
|
||||
return jsonify({'error': '安全新闻历史推送文件缺失!'}), 404
|
||||
except Exception as e:
|
||||
print(f"读取时出错: {SEC_NEWS_PATH}, 原因: {str(e)}")
|
||||
logging.error(f"读取时出错: {SEC_NEWS_PATH}, 原因: {str(e)}")
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
@app.route('/get-tech-passage')
|
||||
def get_tech_passage():
|
||||
print(f"尝试打开技术文章历史推送文件: {TECH_PASSAGE_PATH}")
|
||||
logging.info(f"尝试打开技术文章历史推送文件: {TECH_PASSAGE_PATH}")
|
||||
try:
|
||||
with open(TECH_PASSAGE_PATH, 'r', encoding='utf-8') as file:
|
||||
content = file.read()
|
||||
content = replace_content(content)
|
||||
return jsonify({'content': content}), 200
|
||||
except FileNotFoundError:
|
||||
print(f"文件缺失: {TECH_PASSAGE_PATH}")
|
||||
logging.error(f"文件缺失: {TECH_PASSAGE_PATH}")
|
||||
return jsonify({'error': '技术文章历史推送文件缺失!'}), 404
|
||||
except Exception as e:
|
||||
print(f"读取时出错: {TECH_PASSAGE_PATH}, 原因: {str(e)}")
|
||||
logging.error(f"读取时出错: {TECH_PASSAGE_PATH}, 原因: {str(e)}")
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
@app.route('/log')
|
||||
def get_log():
|
||||
print(f"尝试打开核心日志文件: {CORE_LOG_PATH}")
|
||||
logging.info(f"尝试打开核心日志文件: {CORE_LOG_PATH}")
|
||||
# 读取日志文件内容
|
||||
with open(CORE_LOG_PATH, 'r', encoding='utf-8') as file:
|
||||
log_content = file.read()
|
||||
# 将日志内容传递给模板
|
||||
return render_template('log.html', log_content=log_content)
|
||||
|
||||
@app.route('/weblog')
|
||||
def get_weblog():
|
||||
logging.info(f"尝试打开Web应用日志文件: {WEB_LOG_PATH}")
|
||||
with open(WEB_LOG_PATH, 'r') as file:
|
||||
log_content = file.read()
|
||||
log_content = replace_content(log_content)
|
||||
return render_template('log.html', log_content=log_content)
|
||||
|
||||
def run_server():
|
||||
app.run(host='0.0.0.0', port=5000)
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True) # 在生产环境中应设置为 False
|
||||
app.run(debug=False) # 在生产环境中应设置为 False
|
||||
@@ -99,7 +99,7 @@
|
||||
const htmlContent = marked.parse(data.content);
|
||||
document.getElementById('markdown-content').innerHTML = htmlContent;
|
||||
} else {
|
||||
document.getElementById('markdown-content').innerHTML = '<p>加载历史推送文件时出错!</p>';
|
||||
document.getElementById('markdown-content').innerHTML = '<p>加载历史推送文件时出错!(推送历史记录为空)</p>';
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
@@ -119,7 +119,7 @@
|
||||
const htmlContent = marked.parse(data.content);
|
||||
document.getElementById('markdown-content').innerHTML = htmlContent;
|
||||
} else {
|
||||
document.getElementById('markdown-content').innerHTML = '<p>加载历史推送文件时出错!</p>';
|
||||
document.getElementById('markdown-content').innerHTML = '<p>加载历史推送文件时出错!(推送历史记录为空)</p>';
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
|
||||
Reference in new issue
Block a user