2024-11-19 12:07:35 +00:00
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from flask import Flask, request, jsonify, make_response
|
|
|
|
from flask_cors import CORS
|
2024-11-13 08:13:06 +00:00
|
|
|
|
|
|
|
from config import get_config
|
|
|
|
from services.order import OrderService
|
2024-11-13 08:52:05 +00:00
|
|
|
from services.user import UserService
|
2024-11-19 12:07:35 +00:00
|
|
|
from utils.datetime import parse_time_string, to_milliseconds
|
2024-11-13 08:13:06 +00:00
|
|
|
|
2024-11-19 12:07:35 +00:00
|
|
|
app = Flask('app')
|
|
|
|
CORS(app, resources={r"/*": {"origins": "http://localhost:8080"}})
|
2024-11-13 08:13:06 +00:00
|
|
|
config = get_config()
|
2024-11-19 12:07:35 +00:00
|
|
|
config.order.lifetime = to_milliseconds(**parse_time_string(config.order.lifetime))
|
|
|
|
order_service = OrderService(config)
|
|
|
|
user_service = UserService(config)
|
|
|
|
|
2024-11-13 08:13:06 +00:00
|
|
|
|
2024-11-19 12:07:35 +00:00
|
|
|
@app.after_request
|
|
|
|
def add_cors_headers(response):
|
|
|
|
response.headers['Access-Control-Allow-Origin'] = 'http://127.0.0.1:8080' # 具体的来源
|
|
|
|
# response.headers['Access-Control-Allow-Credentials'] = 'true' # 如果需要凭据支持
|
|
|
|
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
|
|
|
|
response.headers['Access-Control-Allow-Headers'] = 'Content-Type, Authorization'
|
|
|
|
return response
|
2024-11-13 08:52:05 +00:00
|
|
|
|
2024-11-13 08:13:06 +00:00
|
|
|
|
|
|
|
@app.route('/createOrder', methods=['POST'])
|
|
|
|
def create_order():
|
|
|
|
data = request.get_json()
|
2024-11-19 12:07:35 +00:00
|
|
|
payment_method = data.get('paymentMethod', None)
|
2024-11-13 08:52:05 +00:00
|
|
|
if payment_method not in config['PaymentAddresses']:
|
2024-11-13 08:13:06 +00:00
|
|
|
return jsonify({
|
2024-11-19 12:07:35 +00:00
|
|
|
"message": "wrong payment method"
|
|
|
|
}), 400
|
2024-11-20 01:49:25 +00:00
|
|
|
quant = int(data.get('amount', 0))
|
|
|
|
if quant < config.order.get_int("min_quant"):
|
2024-11-19 12:07:35 +00:00
|
|
|
return jsonify({
|
|
|
|
"message": "Amount below minimum limit."
|
2024-11-13 08:13:06 +00:00
|
|
|
}), 400
|
2024-11-13 09:23:59 +00:00
|
|
|
|
2024-11-19 12:07:35 +00:00
|
|
|
wallet_address = data.get('wallet_address', None)
|
|
|
|
if wallet_address is None:
|
|
|
|
name = data.get('name', None)
|
|
|
|
phone = data.get('phone', None)
|
|
|
|
email = data.get('email', None)
|
|
|
|
wallet_addresses = user_service.get_wallet_addresses(name, phone, email)
|
2024-11-13 08:52:05 +00:00
|
|
|
|
2024-11-19 12:07:35 +00:00
|
|
|
if not wallet_addresses:
|
2024-11-13 08:52:05 +00:00
|
|
|
return jsonify({
|
2024-11-19 12:07:35 +00:00
|
|
|
"message": "empty wallet address"
|
2024-11-13 08:52:05 +00:00
|
|
|
}), 400
|
2024-11-19 12:07:35 +00:00
|
|
|
if len(wallet_addresses) > 1:
|
2024-11-13 08:52:05 +00:00
|
|
|
return jsonify({
|
|
|
|
"message": "Please select an address to place your order.",
|
2024-11-19 12:07:35 +00:00
|
|
|
"wallet_addresses": wallet_addresses
|
2024-11-13 08:52:05 +00:00
|
|
|
}), 200
|
|
|
|
# 单个地址
|
2024-11-19 12:07:35 +00:00
|
|
|
wallet_address = wallet_addresses[0]
|
|
|
|
|
|
|
|
order_id, create_timestamp = order_service.create_order(quant, wallet_address)
|
2024-11-13 08:52:05 +00:00
|
|
|
|
2024-11-19 12:07:35 +00:00
|
|
|
return jsonify({"orderID": order_id,
|
|
|
|
"amount": quant,
|
|
|
|
"orderCreateTimestamp": create_timestamp,
|
|
|
|
"orderExpirationTime": config.order.lifetime}), 200
|
2024-11-13 08:13:06 +00:00
|
|
|
|
|
|
|
|
2024-11-13 09:44:45 +00:00
|
|
|
order_status = config.order.order_status
|
2024-11-19 12:07:35 +00:00
|
|
|
|
|
|
|
|
2024-11-13 09:44:45 +00:00
|
|
|
@app.route('/finishOrder', methods=['GET'])
|
|
|
|
def finish_order():
|
|
|
|
data = request.get_json()
|
2024-11-19 12:07:35 +00:00
|
|
|
order_id = data.get('orderID', None)
|
2024-11-13 09:44:45 +00:00
|
|
|
status = order_service.finish_order(order_id)
|
|
|
|
return jsonify({"order_id": order_id,
|
|
|
|
"status": status,
|
|
|
|
"msg": order_status[status]}), 200
|
|
|
|
|
|
|
|
|
2024-11-19 12:07:35 +00:00
|
|
|
def run_gunicorn():
|
|
|
|
# Gunicorn 的命令和参数
|
|
|
|
command = [sys.executable, '-m', 'gunicorn', 'app:app']
|
|
|
|
|
|
|
|
# 启动 Gunicorn
|
|
|
|
subprocess.run(command)
|
|
|
|
|
|
|
|
|
2024-11-13 08:13:06 +00:00
|
|
|
if __name__ == '__main__':
|
2024-11-19 12:07:35 +00:00
|
|
|
if os.name == 'nt':
|
|
|
|
app.run(debug=True, port=5000, host='0.0.0.0')
|
|
|
|
elif os.name == 'posix':
|
|
|
|
run_gunicorn()
|