100 lines
3.7 KiB
Python
100 lines
3.7 KiB
Python
from flask import Flask, request, jsonify, render_template
|
|
from .config import Config
|
|
from .utils import (get_latest_emails, get_latest_email_with_code,
|
|
add_allowed_domain, remove_allowed_domain,
|
|
get_allowed_domains, get_allowed_domains_with_time,
|
|
get_all_emails, get_email_by_id)
|
|
import redis
|
|
import json
|
|
import logging
|
|
|
|
# 配置日志
|
|
logger = logging.getLogger('app')
|
|
logger.setLevel(logging.DEBUG)
|
|
# 添加控制台处理器
|
|
handler = logging.StreamHandler()
|
|
handler.setFormatter(logging.Formatter(
|
|
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
|
))
|
|
logger.addHandler(handler)
|
|
|
|
def create_app():
|
|
app = Flask(__name__)
|
|
app.config.from_object(Config)
|
|
|
|
@app.route('/emails', methods=['GET'])
|
|
def get_emails():
|
|
recipient = request.args.get('recipient')
|
|
limit = request.args.get('limit', default=10, type=int)
|
|
if not recipient:
|
|
return jsonify({'error': 'Recipient email is required'}), 400
|
|
emails = get_latest_emails(recipient, limit)
|
|
return jsonify(emails)
|
|
|
|
@app.route('/latest_email', methods=['GET'])
|
|
def get_latest_email():
|
|
recipient = request.args.get('recipient')
|
|
if not recipient:
|
|
return jsonify({'error': 'Recipient email is required'}), 400
|
|
email_data = get_latest_email_with_code(recipient)
|
|
if email_data:
|
|
return jsonify(email_data)
|
|
return jsonify({'error': 'No emails found for this recipient'}), 404
|
|
|
|
@app.route('/allowed_domains/add', methods=['POST'])
|
|
def add_domain():
|
|
domain = request.json.get('domain')
|
|
if not domain:
|
|
return jsonify({'error': 'Domain is required'}), 400
|
|
add_allowed_domain(domain)
|
|
return jsonify({'message': 'Domain added successfully'}), 201
|
|
|
|
@app.route('/allowed_domains/remove', methods=['POST'])
|
|
def remove_domain():
|
|
domain = request.json.get('domain')
|
|
if not domain:
|
|
return jsonify({'error': 'Domain is required'}), 400
|
|
remove_allowed_domain(domain)
|
|
return jsonify({'message': 'Domain removed successfully'}), 200
|
|
|
|
@app.route('/allowed_domains/list', methods=['GET'])
|
|
def list_domains():
|
|
domains = get_allowed_domains()
|
|
return jsonify(domains), 200
|
|
|
|
@app.route('/allowed_domains/list_with_time', methods=['GET'])
|
|
def list_domains_with_time():
|
|
domains_with_time = get_allowed_domains_with_time()
|
|
return jsonify(domains_with_time), 200
|
|
|
|
@app.route('/web/list')
|
|
def list_emails_page():
|
|
try:
|
|
emails = get_all_emails(count=50)
|
|
# 检查每个邮件的 message_id
|
|
for email in emails:
|
|
logger.debug(f"Email message_id: {email.get('message_id')}")
|
|
return render_template('email_list.html', emails=emails)
|
|
except Exception as e:
|
|
logger.error(f"Error in list_emails_page: {str(e)}")
|
|
return "获取邮件列表失败", 500
|
|
|
|
@app.route('/web/email/<message_id>')
|
|
def view_email_page(message_id):
|
|
try:
|
|
logger.debug(f"Accessing email with message_id: {message_id}")
|
|
|
|
# 使用新函数获取邮件数据
|
|
email = get_email_by_id(message_id)
|
|
if not email:
|
|
logger.warning(f"No email found for message_id: {message_id}")
|
|
return "邮件不存在", 404
|
|
|
|
logger.debug(f"Retrieved email: {email}")
|
|
return render_template('email_detail.html', email=email)
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error in view_email_page: {str(e)}", exc_info=True)
|
|
return "获取邮件详情失败", 500
|
|
|
|
return app |