59 lines
2.1 KiB
Python
59 lines
2.1 KiB
Python
from flask import Flask, jsonify, render_template
|
|
import os
|
|
|
|
app = Flask(__name__)
|
|
|
|
# 配置文件路径
|
|
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')
|
|
|
|
# print(f"Sec News Path: {SEC_NEWS_PATH}")
|
|
# print(f"Tech Passage Path: {TECH_PASSAGE_PATH}")
|
|
|
|
@app.route('/')
|
|
def index():
|
|
return render_template('index.html')
|
|
|
|
# 替换输出内容
|
|
def replace_content(content):
|
|
content = content.replace('####', '###')
|
|
# content = content.replace('\n\n', '###')
|
|
return content
|
|
|
|
@app.route('/get-sec-news')
|
|
def get_sec_news():
|
|
print(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}")
|
|
return jsonify({'error': '安全新闻历史推送文件缺失!'}), 404
|
|
except Exception as e:
|
|
print(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}")
|
|
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}")
|
|
return jsonify({'error': '技术文章历史推送文件缺失!'}), 404
|
|
except Exception as e:
|
|
print(f"读取时出错: {TECH_PASSAGE_PATH}, 原因: {str(e)}")
|
|
return jsonify({'error': str(e)}), 500
|
|
|
|
def run_server():
|
|
app.run(host='0.0.0.0', port=5000)
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True) # 在生产环境中应设置为 False |