Update app.py

Added error handling for 404
This commit is contained in:
Eric Lay 2024-03-28 10:33:00 -05:00 committed by GitHub
parent 4e6300a3a2
commit c27f37717a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 20 additions and 4 deletions

20
app.py
View File

@ -1,15 +1,19 @@
import os import os
import random import random
from flask import Flask, send_from_directory, session from flask import Flask, send_from_directory, session, Blueprint, render_template
app = Flask(__name__) app = Flask(__name__)
app.secret_key = '420-69-LOL' # For using client side session cookies app.secret_key = '420-69-LOL' # For using client side session cookies
# Script must run from root dir containing all websites dirs
# OR change the ROOT_DIR path :)
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
# Point jinja to templates
site = Blueprint('site', __name__, template_folder='Templates')
# Site choosing logic # Site choosing logic
def current_website_dir(): def current_website_dir():
# Script must run from root dir containing all websites dirs # Script must run from root dir containing all websites dirs
# OR change the ROOT_DIR path :) # OR change the ROOT_DIR path :)
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
WEBSITE_DIRS = [name for name in os.listdir(ROOT_DIR) if not name.startswith('.') and os.path.isdir(os.path.join(ROOT_DIR, name))] WEBSITE_DIRS = [name for name in os.listdir(ROOT_DIR) if not name.startswith('.') and os.path.isdir(os.path.join(ROOT_DIR, name))]
session.pop('website_dir', None) # Clear website_dir if exist session.pop('website_dir', None) # Clear website_dir if exist
match session['requests']: #Match Nth page request match session['requests']: #Match Nth page request
@ -25,11 +29,16 @@ def current_website_dir():
WEBSITE_DIRS.remove(dir) WEBSITE_DIRS.remove(dir)
case 'Marvel': case 'Marvel':
WEBSITE_DIRS.remove(dir) WEBSITE_DIRS.remove(dir)
case 'Templates':
WEBSITE_DIRS.remove(dir)
session['website_dir'] = random.choice(WEBSITE_DIRS) session['website_dir'] = random.choice(WEBSITE_DIRS)
@app.route('/<path:filename>', methods=['GET']) # Make static files available @app.route('/<path:filename>', methods=['GET']) # Make static files available
def static_proxy(filename): def static_proxy(filename):
try:
return send_from_directory(session['website_dir'], filename) return send_from_directory(session['website_dir'], filename)
except KeyError:
return render_template('404.html')
@app.route('/', methods=['GET']) # Serve site index.html @app.route('/', methods=['GET']) # Serve site index.html
def index(): def index():
@ -38,7 +47,14 @@ def index():
else: else:
session['requests'] = 1 session['requests'] = 1
current_website_dir() # Choose website dir current_website_dir() # Choose website dir
try:
return send_from_directory(session['website_dir'], 'index.html') return send_from_directory(session['website_dir'], 'index.html')
except KeyError:
return render_template('404.html')
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html')
if __name__ == "__main__": if __name__ == "__main__":
app.run() app.run()