2024-03-24 18:56:52 -05:00
|
|
|
import os
|
|
|
|
import random
|
2024-03-29 16:05:24 -05:00
|
|
|
from flask import Flask, send_from_directory, session, render_template
|
2024-03-24 18:56:52 -05:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
2024-03-29 16:05:24 -05:00
|
|
|
app.config['TRAP_HTTP_EXCEPTIONS']=True
|
2024-03-27 22:39:22 -05:00
|
|
|
app.secret_key = '420-69-LOL' # For using client side session cookies
|
2024-03-24 18:56:52 -05:00
|
|
|
|
|
|
|
# Site choosing logic
|
|
|
|
def current_website_dir():
|
2024-03-27 22:39:22 -05:00
|
|
|
# Script must run from root dir containing all websites dirs
|
|
|
|
# OR change the ROOT_DIR path :)
|
2024-03-28 11:13:50 -05:00
|
|
|
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
|
2024-03-28 11:48:34 -05:00
|
|
|
WEBSITE_DIRS = [name for name in os.listdir(ROOT_DIR)
|
|
|
|
if not name.startswith('.')
|
|
|
|
and os.path.isdir(os.path.join(ROOT_DIR, name))]
|
2024-03-27 22:39:22 -05:00
|
|
|
session.pop('website_dir', None) # Clear website_dir if exist
|
|
|
|
match session['requests']: #Match Nth page request
|
2024-03-27 21:52:53 -05:00
|
|
|
case 5:
|
|
|
|
session['website_dir'] = (os.path.join(ROOT_DIR, 'Marvel'))
|
|
|
|
case 6:
|
|
|
|
session['website_dir'] = (os.path.join(ROOT_DIR, 'Escape'))
|
2024-03-27 22:39:22 -05:00
|
|
|
session.pop('requests', None) # Reset request count
|
2024-03-27 21:52:53 -05:00
|
|
|
case _:
|
2024-03-27 22:39:22 -05:00
|
|
|
for dir in WEBSITE_DIRS:
|
|
|
|
match dir: # Remove request specific pages
|
2024-03-29 16:05:24 -05:00
|
|
|
case 'Escape' | 'Marvel' | 'templates':
|
2024-03-28 10:33:00 -05:00
|
|
|
WEBSITE_DIRS.remove(dir)
|
2024-03-27 21:52:53 -05:00
|
|
|
session['website_dir'] = random.choice(WEBSITE_DIRS)
|
2024-03-27 16:18:03 -05:00
|
|
|
|
2024-03-27 22:39:22 -05:00
|
|
|
@app.route('/<path:filename>', methods=['GET']) # Make static files available
|
2024-03-24 18:56:52 -05:00
|
|
|
def static_proxy(filename):
|
2024-03-29 16:05:24 -05:00
|
|
|
return send_from_directory(session['website_dir'], filename)
|
2024-03-24 18:56:52 -05:00
|
|
|
|
2024-03-27 22:39:22 -05:00
|
|
|
@app.route('/', methods=['GET']) # Serve site index.html
|
2024-03-24 18:56:52 -05:00
|
|
|
def index():
|
2024-03-27 22:39:22 -05:00
|
|
|
if 'requests' in session: # init requests count
|
2024-03-27 18:17:11 -05:00
|
|
|
session['requests'] += 1
|
|
|
|
else:
|
|
|
|
session['requests'] = 1
|
2024-03-27 22:39:22 -05:00
|
|
|
current_website_dir() # Choose website dir
|
2024-03-29 16:05:24 -05:00
|
|
|
return send_from_directory(session['website_dir'], 'index.html')
|
2024-03-28 10:33:00 -05:00
|
|
|
|
2024-03-29 16:05:24 -05:00
|
|
|
@app.errorhandler(Exception) # Handle uncaught exceptions per code type
|
|
|
|
def handle_error(e):
|
|
|
|
try:
|
|
|
|
if e.code < 400:
|
|
|
|
return Flask.Response.force_type(e, Flask.request.environ)
|
|
|
|
elif e.code == 404:
|
|
|
|
return render_template('404.html', error='404'),404
|
|
|
|
raise e
|
|
|
|
except:
|
|
|
|
return render_template('500.html', error='500'),500
|
2024-03-24 18:56:52 -05:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-03-27 20:12:22 -05:00
|
|
|
app.run()
|