From aa8dc9476e47003b66fc66fea0f2ff3f64cda7b9 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Wed, 16 Aug 2023 13:48:11 +1000 Subject: [PATCH] worker: Added initial files --- worker/install.sh | 21 +++++++++++++++ worker/main.py | 58 +++++++++++++++++++++++++++++++++++++++++ worker/requirements.txt | 4 +++ wp.sh => worker/wp.sh | 0 4 files changed, 83 insertions(+) create mode 100644 worker/install.sh create mode 100644 worker/main.py create mode 100644 worker/requirements.txt rename wp.sh => worker/wp.sh (100%) diff --git a/worker/install.sh b/worker/install.sh new file mode 100644 index 0000000..3865c73 --- /dev/null +++ b/worker/install.sh @@ -0,0 +1,21 @@ +#!/bin/bash +# Initial install for all prerequisites for the project. +# This makes it quicker to get each site up and running. + +# Update the system +sudo apt update && sudo apt upgrade -y + +# Install Docker +sudo apt install apt-transport-https ca-certificates curl software-properties-common -y +curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg +echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null +sudo apt update +apt-cache policy docker-ce +sudo apt install docker-ce -y +sudo apt install docker-compose -y + +# Install NGINX +sudo apt install nginx -y + +# Install python prerequisites +python3 -m pip install -r requirements.txt \ No newline at end of file diff --git a/worker/main.py b/worker/main.py new file mode 100644 index 0000000..c15d32f --- /dev/null +++ b/worker/main.py @@ -0,0 +1,58 @@ +# API server + +from flask import Flask, request, jsonify +import dotenv +import os + +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') + + # Return the domain and the number of sites + return jsonify({'domain': domain, 'count': count}) + +def get_sites_count(): + # 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') + + # Return number of lines in file + return len(sites_file.readlines()) + +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 + +# Start the server +if __name__ == '__main__': + app.run(debug=False, port=5000) \ No newline at end of file diff --git a/worker/requirements.txt b/worker/requirements.txt new file mode 100644 index 0000000..01ad091 --- /dev/null +++ b/worker/requirements.txt @@ -0,0 +1,4 @@ +python-dotenv +requests +flask +jsonify \ No newline at end of file diff --git a/wp.sh b/worker/wp.sh similarity index 100% rename from wp.sh rename to worker/wp.sh