|
from flask import Flask, jsonify, request |
|
from flask_cors import CORS |
|
from datetime import datetime |
|
import pytz |
|
|
|
app = Flask(__name__) |
|
CORS(app) |
|
|
|
|
|
stats = { |
|
'views': 2498, |
|
'usage': 2498 |
|
} |
|
|
|
|
|
def get_china_time(): |
|
china_tz = pytz.timezone('Asia/Shanghai') |
|
return datetime.now(china_tz).strftime('%Y-%m-%d') |
|
|
|
|
|
@app.route('/stats', methods=['GET']) |
|
def get_stats(): |
|
return jsonify({ |
|
**stats, |
|
'date': get_china_time() |
|
}) |
|
|
|
|
|
@app.route('/increment', methods=['POST']) |
|
def increment_count(): |
|
data = request.get_json() |
|
count_type = data.get('type') |
|
|
|
if count_type == 'view': |
|
stats['views'] += 1 |
|
elif count_type == 'usage': |
|
stats['usage'] += 1 |
|
|
|
return jsonify({ |
|
**stats, |
|
'date': get_china_time() |
|
}) |
|
|
|
if __name__ == '__main__': |
|
|
|
app.run(host='0.0.0.0', port=7860) |