hnshosting-wp/worker/main.py
Nathan Woodburn c9f8a8237d
All checks were successful
Build Docker / Build Bot (push) Successful in 21s
Build Docker / Build Master (push) Successful in 25s
main: Add code block for tlsa info
2023-09-20 22:21:00 +10:00

97 lines
2.4 KiB
Python

# API server
from flask import Flask, request, jsonify
import dotenv
import os
import threading
dotenv.load_dotenv()
app = Flask(__name__)
# API route for POST requests for new-site (takes variable 'domain')
@app.route('/new-site', methods=['POST'])
def new_site():
# Get the URL from the request
domain = request.args.get('domain')
count = get_sites_count()
if site_exists(domain):
return jsonify({'error': 'Site already exists', 'success': 'false'})
# Add site to file
sites_file = open('sites.txt', 'a')
sites_file.write(domain + '\n')
sites_file.close()
# New site in background
thread = threading.Thread(target=new_site_script, args=(domain,))
thread.start()
# Return the domain and the number of sites
return jsonify({'domain': domain, 'count': count})
@app.route('/tlsa', methods=['GET'])
def tlsa():
domain = request.args.get('domain')
if domain == None:
return jsonify({'error': 'Invalid domain', 'success': 'false'})
tlsa = None
try:
tlsa_file = open('wordpress-'+domain+'/tlsa.txt', 'r')
tlsa = tlsa_file.readlines()
tlsa_file.close()
except FileNotFoundError as e:
return jsonify({'error': 'TLSA record not found', 'success': 'false', 'ex': str(e)})
# Remove newlines
tlsa = tlsa[0].strip('\n')
return jsonify({'domain': domain, 'tlsa': tlsa})
# Return status
@app.route('/status', methods=['GET'])
def status():
num_Sites = get_sites_count()
availability=(num_Sites < int(os.getenv('MAX_SITES')))
return jsonify({'availability': availability, 'num_sites': num_Sites})
# Ping status
@app.route('/ping')
def ping():
return 'pong'
def get_sites_count():
# Get number of files in nginx/sites
dir = os.listdir('/etc/nginx/sites-available')
num_Sites = len(dir) - 1
# Return number of lines in file
return num_Sites
def site_exists(domain):
# If file doesn't exist, create it
try:
sites_file = open('sites.txt', 'r')
except FileNotFoundError:
sites_file = open('sites.txt', 'w')
sites_file.close()
sites_file = open('sites.txt', 'r')
# Check if domain is in file
if domain in sites_file.read():
return True
else:
return False
def new_site_script(domain):
script = 'bash wp.sh ' + domain
os.system(script)
# Start the server
if __name__ == '__main__':
app.run(debug=False, port=5000,host='0.0.0.0')