feat: Add api endpoint
All checks were successful
Build Docker / Build Image (push) Successful in 20s

This commit is contained in:
Nathan Woodburn 2023-11-08 15:25:47 +11:00
parent 5e6af2bcc8
commit 30c723cebb
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1
2 changed files with 36 additions and 1 deletions

View File

@ -23,3 +23,4 @@ docker run -d \
| max_gifts_per_interval | Maximum number of gifts to send per interval | 24 |
| interval | Interval to send gifts (in seconds) | 86400 (24 hours) |
| discord_webhook | Discord webhook to send notifications to | None |
| api_key | API key for api gifts | None |

34
main.py
View File

@ -75,6 +75,40 @@ def submit():
else:
return render_template('error.html',error=status,address=address)
@app.route('/api', methods=['POST'])
def api():
# Get from params
params = request.args
name = params['name']
email = params['email']
key = params['key']
if key != os.getenv('api_key'):
return jsonify({'error': 'Invalid API key', 'success': False})
if 'X-REAL-IP' in request.headers:
ip = request.headers['X-REAL-IP']
if 'X-Real-Ip' in request.headers:
ip = request.headers['X-Real-Ip']
# Validate email
try:
emailinfo = validate_email(email, check_deliverability=False)
email = emailinfo.normalized
except EmailNotValidError as e:
return jsonify({'error': 'Invalid email address', 'success': False})
status = gift.gift(name, email, "api", ip)
print(status,flush=True)
if status == True:
return jsonify({'success': True})
else:
return jsonify({'error': status, 'success': False})
# Special routes
@app.route('/.well-known/wallets/<token>')
def send_wallet(token):