Fancier homepage and a bit of performance improvement through aggressive caching.
This commit is contained in:
@@ -133,8 +133,13 @@ cached: dict = {
|
||||
"css_navbar": open("styles/header.css", "r").read(),
|
||||
"css_response": open("styles/human_readable.css", "r").read(),
|
||||
"css_collapsible": open("styles/collapsible.css", "r").read(),
|
||||
"css_page": open("styles/page.css", "r").read(),
|
||||
"js_collapsible": open("js/collapsible.js", "r").read(),
|
||||
"js_shortcode": open("js/shortcode.js", "r").read(),
|
||||
"favicon_svg": open("static/favicon.svg", "r").read(),
|
||||
"book_info": kjv_cur.execute(
|
||||
'SELECT "order", title_short, category, otnt, chapters FROM book_info ORDER BY "order"'
|
||||
).fetchall(),
|
||||
}
|
||||
|
||||
# Fill the chapter list with unique chapters for seeking back and forward by index.
|
||||
@@ -149,17 +154,52 @@ for book in range(1, 67):
|
||||
"SELECT c FROM fts_kjv where b = ? ORDER BY id DESC LIMIT 1;", (book,)
|
||||
).fetchone()[0]
|
||||
|
||||
# Cache book_id to bookname mapping for fast lookups
|
||||
cached["book_id_to_name"] = {}
|
||||
for row in kjv_cur.execute("SELECT b, n FROM key_english").fetchall():
|
||||
cached["book_id_to_name"][row[0]] = row[1]
|
||||
|
||||
# Organize books by testament and category for index page
|
||||
from collections import defaultdict
|
||||
testament_data = {"OT": defaultdict(list), "NT": defaultdict(list)}
|
||||
for book in cached["book_info"]:
|
||||
book_order, title, category, testament, num_chapters = book
|
||||
testament_data[testament][category].append({
|
||||
"title": title,
|
||||
"chapters": num_chapters
|
||||
})
|
||||
cached["book_info_for_index"] = dict(testament_data)
|
||||
|
||||
# Render the index page once and cache it
|
||||
with app.app_context():
|
||||
from flask import render_template_string
|
||||
template_content = open("html/index.html", "r").read()
|
||||
cached["html_index"] = render_template_string(template_content, testament_data=cached["book_info_for_index"])
|
||||
|
||||
@app.get("/js/shortcode.js")
|
||||
def serve_shortcode_js():
|
||||
return Response(cached.get("js_shortcode"), mimetype='application/javascript')
|
||||
response = Response(cached.get("js_shortcode"), mimetype='application/javascript')
|
||||
response.headers['Cache-Control'] = 'public, max-age=31536000'
|
||||
return response
|
||||
|
||||
@app.get("/js/collapsible.js")
|
||||
def serve_collapsible_js():
|
||||
return Response(cached.get("js_collapsible"), mimetype='application/javascript')
|
||||
response = Response(cached.get("js_collapsible"), mimetype='application/javascript')
|
||||
response.headers['Cache-Control'] = 'public, max-age=31536000'
|
||||
return response
|
||||
|
||||
@app.get("/styles/page.css")
|
||||
def serve_page_css():
|
||||
return send_file("styles/page.css", mimetype='text/css')
|
||||
response = Response(cached.get("css_page"), mimetype='text/css')
|
||||
response.headers['Cache-Control'] = 'public, max-age=31536000'
|
||||
return response
|
||||
|
||||
@app.get("/favicon.ico")
|
||||
@app.get("/favicon.svg")
|
||||
def serve_favicon():
|
||||
response = Response(cached.get("favicon_svg"), mimetype='image/svg+xml')
|
||||
response.headers['Cache-Control'] = 'public, max-age=31536000'
|
||||
return response
|
||||
|
||||
def generate_short_id():
|
||||
remain = random.randrange(614_656, 17_210_367) # resolves to length of 5
|
||||
@@ -174,11 +214,10 @@ def generate_short_id():
|
||||
|
||||
|
||||
def lookup_bookname(book_id):
|
||||
result = kjv_cur.execute(
|
||||
"SELECT n FROM key_english WHERE b = ?;",(book_id,)).fetchone()
|
||||
if result:
|
||||
result = result[0]
|
||||
return result
|
||||
# Ensure book_id is an integer for cache lookup
|
||||
if isinstance(book_id, str):
|
||||
book_id = int(book_id)
|
||||
return cached["book_id_to_name"].get(book_id)
|
||||
|
||||
|
||||
def lookup_book_id(bookname) -> int:
|
||||
@@ -744,7 +783,7 @@ def idx(shortcode=""):
|
||||
logging.info(f"/{shortcode}")
|
||||
return resolve_shortcode(shortcode)
|
||||
logging.info(f"/{shortcode}")
|
||||
return send_file("html/index.html")
|
||||
return cached["html_index"]
|
||||
|
||||
@app.get("/fe")
|
||||
def frontend():
|
||||
|
||||
Reference in New Issue
Block a user