From ec53b5862bf6d71a4873676e7fc356633303de13 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Thu, 9 Nov 2023 12:51:35 +1100 Subject: [PATCH] feat: Redirect empty domain to main server --- README.md | 2 ++ sites/db.py | 2 +- sites/slds.py | 9 ++++----- sites/website.py | 8 +++++++- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 4f75f23..746705f 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ services: DB_USER: main DB_PASSWORD: your-db-password DB_NAME: main + CITY_DOMAIN: exampledomainnathan1 WORKERS: 2 # number of workers to run (should be 2 * number of cores) sites: @@ -32,6 +33,7 @@ services: DB_USER: main DB_PASSWORD: your-db-password DB_NAME: main + MAIN_DOMAIN: cities.hnshosting.au WORKERS: 2 # number of workers to run (should be 2 * number of cores) db: diff --git a/sites/db.py b/sites/db.py index 58dedfe..3854b83 100644 --- a/sites/db.py +++ b/sites/db.py @@ -24,7 +24,7 @@ def get_website_data(domain): connection.close() if data == []: - return "No data found for this domain" + return "" parsed = data[0][2] parsed = json.loads(parsed) diff --git a/sites/slds.py b/sites/slds.py index e74ce88..ebd2600 100644 --- a/sites/slds.py +++ b/sites/slds.py @@ -12,21 +12,20 @@ import website app = Flask(__name__) dotenv.load_dotenv() +main_domain = "cities.hnshosting.au" +if os.getenv('MAIN_DOMAIN') != None: + main_domain = os.getenv('MAIN_DOMAIN') #Assets routes @app.route('/assets/') def assets(path): return send_from_directory('templates/assets', path) -#! TODO make prettier -def error(message): - return jsonify({'success': False, 'message': message}), 400 - @app.route('/') def index(): host = request.host if len(host.split('.')) != 2: - return error('Invalid domain') + return redirect('https://'+main_domain) host = host.split('.')[0] # Get website data diff --git a/sites/website.py b/sites/website.py index 5be2417..cdfbc80 100644 --- a/sites/website.py +++ b/sites/website.py @@ -1,9 +1,15 @@ from flask import Flask, make_response, redirect, render_template_string, request, jsonify, render_template, send_from_directory from bs4 import BeautifulSoup +import os +import dotenv + +main_domain = "cities.hnshosting.au" +if os.getenv('MAIN_DOMAIN') != None: + main_domain = os.getenv('MAIN_DOMAIN') def render(data): if data == "": - return "No data found for this domain" + return redirect("https://" + main_domain + '/claim?domain=' + request.host.split('.')[0]) try: soup = BeautifulSoup(data, 'html.parser')