Fancier homepage and a bit of performance improvement through aggressive caching.

This commit is contained in:
2025-10-27 13:33:44 -07:00
parent 0106f0b9ac
commit 17a7be36bd
4 changed files with 251 additions and 174 deletions
+48 -9
View File
@@ -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():
+172 -161
View File
@@ -1,77 +1,105 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>KJV API - Legacy Edition</title>
<style>html,body{background-color:#1a1a1a}</style>
<link rel="icon" type="image/svg+xml" href="/favicon.svg">
<link rel="stylesheet" href="/styles/page.css">
<script src="/js/collapsible.js" defer></script>
<style>
/* Modern KJV API Styling - Index Page */
:root {
--color-bg-primary: #1a1a1a;
--color-bg-secondary: #252525;
--color-bg-tertiary: #2f2f2f;
--color-text-primary: #e8e8e8;
--color-text-secondary: #b8b8b8;
--color-text-muted: #888888;
--color-accent-primary: #6b9bd1;
--color-accent-hover: #85aede;
--color-link: #6b9bd1;
--color-link-hover: #85aede;
--color-border: #3a3a3a;
--color-shadow: rgba(0, 0, 0, 0.3);
--font-ui: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
--font-scripture: Georgia, "Crimson Text", "Times New Roman", serif;
--font-mono: "SF Mono", Monaco, "Cascadia Code", "Courier New", monospace;
--space-xs: 0.25rem;
--space-sm: 0.5rem;
--space-md: 1rem;
--space-lg: 1.5rem;
--space-xl: 2rem;
--content-max-width: 820px;
--border-radius: 8px;
--border-radius-sm: 4px;
--transition-fast: 150ms ease;
/* Bible Book Styles */
.book-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
gap: 0.5rem;
padding: 0.5rem 0;
}
* {
box-sizing: border-box;
}
body {
background-color: var(--color-bg-primary);
.book-button {
background-color: var(--color-bg-tertiary);
border: 1px solid var(--color-border);
border-radius: var(--border-radius-sm);
padding: 0.75rem 1rem;
text-align: left;
color: var(--color-text-primary);
margin: 0;
padding: var(--space-xl);
transition: all var(--transition-fast);
cursor: pointer;
font-family: var(--font-ui);
line-height: 1.6;
font-weight: 600;
font-size: 0.95rem;
width: 100%;
}
html {
font-family: var(--font-ui);
font-size: 16px;
.book-button:hover {
background-color: var(--color-accent-primary);
color: var(--color-bg-primary);
border-color: var(--color-accent-primary);
transform: translateY(-1px);
}
.container {
max-width: var(--content-max-width);
margin: 0 auto;
padding: var(--space-lg);
.book-button.active {
background-color: var(--color-accent-primary);
color: var(--color-bg-primary);
border-color: var(--color-accent-primary);
}
h1 {
color: var(--color-text-primary);
font-size: 2.5rem;
margin-bottom: var(--space-lg);
font-weight: 700;
.chapter-grid {
display: none;
grid-template-columns: repeat(auto-fill, minmax(45px, 1fr));
gap: 0.4rem;
padding: 1rem;
background-color: var(--color-bg-secondary);
border: 1px solid var(--color-border);
border-radius: var(--border-radius-sm);
margin-top: 0.5rem;
margin-bottom: 0.5rem;
}
.chapter-link {
background-color: var(--color-bg-tertiary);
border: 1px solid var(--color-border);
border-radius: var(--border-radius-sm);
padding: 0.5rem;
text-align: center;
color: var(--color-text-primary);
font-size: 0.85rem;
transition: all var(--transition-fast);
display: block;
}
form {
.chapter-link:hover {
background-color: var(--color-accent-primary);
color: var(--color-bg-primary);
text-decoration: none;
transform: translateY(-1px);
}
.quick-actions {
display: flex;
gap: 0.75rem;
justify-content: center;
flex-wrap: wrap;
margin: 1.5rem 0;
}
.quick-btn {
background-color: var(--color-accent-primary);
color: var(--color-bg-primary);
padding: 0.75rem 1.5rem;
border-radius: var(--border-radius-sm);
font-weight: 600;
transition: all var(--transition-fast);
display: inline-block;
}
.quick-btn:hover {
background-color: var(--color-accent-hover);
transform: translateY(-1px);
text-decoration: none;
}
.search-section {
background-color: var(--color-bg-secondary);
padding: var(--space-xl);
border-radius: var(--border-radius);
@@ -80,124 +108,105 @@ form {
box-shadow: 0 2px 8px var(--color-shadow);
}
label {
display: block;
margin-bottom: var(--space-sm);
.testament-header {
color: var(--color-accent-primary);
font-size: 1.8rem;
margin-top: 2rem;
margin-bottom: 1rem;
font-weight: 700;
border-bottom: 2px solid var(--color-accent-primary);
padding-bottom: 0.5rem;
}
.category-header {
color: var(--color-text-secondary);
font-size: 1.1rem;
margin-top: 1.5rem;
margin-bottom: 0.75rem;
font-weight: 600;
color: var(--color-text-primary);
}
a {
color: var(--color-link);
text-decoration: none;
transition: color var(--transition-fast);
}
a:hover {
color: var(--color-link-hover);
text-decoration: underline;
}
input[type=text] {
width: 100%;
padding: var(--space-sm) var(--space-md);
margin: var(--space-sm) 0;
font-size: 1rem;
font-family: var(--font-ui);
background-color: var(--color-bg-tertiary);
color: var(--color-text-primary);
border: 2px solid var(--color-border);
border-radius: var(--border-radius-sm);
transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}
input[type=text]:focus {
outline: none;
border-color: var(--color-accent-primary);
box-shadow: 0 0 0 3px rgba(107, 155, 209, 0.1);
}
input[type=button],
input[type=submit],
input[type=reset] {
font-family: var(--font-ui);
font-weight: 600;
background-color: var(--color-accent-primary);
border: none;
color: var(--color-bg-primary);
padding: var(--space-sm) var(--space-lg);
text-decoration: none;
margin: var(--space-sm) 0;
cursor: pointer;
border-radius: var(--border-radius-sm);
transition: background-color var(--transition-fast), transform var(--transition-fast);
font-size: 1rem;
}
input[type=button]:hover,
input[type=submit]:hover,
input[type=reset]:hover {
background-color: var(--color-accent-hover);
transform: translateY(-1px);
}
code {
background-color: var(--color-bg-tertiary);
color: var(--color-text-primary);
padding: 2px 6px;
border-radius: var(--border-radius-sm);
font-family: var(--font-mono);
font-size: 0.9em;
}
pre {
background-color: var(--color-bg-secondary);
border: 1px solid var(--color-border);
border-radius: var(--border-radius);
padding: var(--space-lg);
overflow-x: auto;
line-height: 1.6;
}
pre code {
background-color: transparent;
padding: 0;
font-size: 0.95rem;
}
@media (max-width: 768px) {
body {
padding: var(--space-md);
.book-grid {
grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
}
h1 {
font-size: 2rem;
.quick-actions {
flex-direction: column;
}
form {
padding: var(--space-lg);
.quick-btn {
width: 100%;
}
}
</style>
</head>
<body>
<h1>KJV API (Legacy edition)</h1>
<div class="container">
<h1>KJV API (Legacy edition)</h1>
<form action="/find">
<label for='kw'>Keyword Search</label><br>
<input type="text" name="kw"><br><br>
<input type="hidden" name="view" value="rich"">
<input type="submit">
<form>
<div class="search-section">
<form action="/find">
<label for='kw'>Search Scripture</label><br>
<input type="text" name="kw" id="kw" placeholder="Enter verse reference or search keywords..."><br><br>
<input type="hidden" name="view" value="rich">
<input type="submit" value="Search">
</form>
<div class="quick-actions">
<a href="/random?view=rich" class="quick-btn">Random Verse</a>
<a href="/votd?view=rich" class="quick-btn">Verse of the Day</a>
</div>
</div>
<pre>
<code>
API ENDPOINT SPECIFICATION v 0.3
<!-- OLD TESTAMENT -->
<h2 class="testament-header">Old Testament</h2>
{% for category, books in testament_data.OT.items() %}
<h3 class="category-header">{{ category }}</h3>
<div class="book-grid">
{% for book in books %}
<div>
<button class="book-button collapsible" data-book="{{ book.title }}">
{{ book.title }}
</button>
<div class="chapter-grid content">
{% for chapter in range(1, book.chapters + 1) %}
<a href="/show?kw={{ book.title }} {{ chapter }}&view=rich" class="chapter-link">{{ chapter }}</a>
{% endfor %}
</div>
</div>
{% endfor %}
</div>
{% endfor %}
<!-- NEW TESTAMENT -->
<h2 class="testament-header">New Testament</h2>
{% for category, books in testament_data.NT.items() %}
<h3 class="category-header">{{ category }}</h3>
<div class="book-grid">
{% for book in books %}
<div>
<button class="book-button collapsible" data-book="{{ book.title }}">
{{ book.title }}
</button>
<div class="chapter-grid content">
{% for chapter in range(1, book.chapters + 1) %}
<a href="/show?kw={{ book.title }} {{ chapter }}&view=rich" class="chapter-link">{{ chapter }}</a>
{% endfor %}
</div>
</div>
{% endfor %}
</div>
{% endfor %}
<!-- API DOCUMENTATION -->
<button class="collapsible" style="margin-top: 2rem;">API Documentation</button>
<div class="content">
<pre><code>API ENDPOINT SPECIFICATION v 0.3
================================
<a href="/find">
/find</a> - Tries to show a verse, if it's not a verse
<a href="/find">/find</a> - Tries to show a verse, if it's not a verse
it will try to search your query. If it's a
single query it will try to find a definition
for the word as well. Only avaliable in "Rich"
@@ -213,9 +222,9 @@ URL PARAMETERS
==============
view=
- json - json-encoded response
- plain - plaintext response
- rich - pretty response with css
default: json
- plain - very simple html response
- rich - pretty response with css and js
Required parameter
kw=
- Used with /search /show/ /define /find
@@ -257,7 +266,6 @@ radio selector for choosing your output format.
- /define
- /show
EXAMPLES
========
api.tld/random
@@ -266,7 +274,10 @@ api.tld/show?kw=john3:16,1john5:7&view=plain
BUGS
====
Suggestions, bug reports or improvements can be reported to <a href="https://1611.social/@tyler">@tyler@1611.social</a>
</code>
</pre>
Suggestions, bug reports or improvements can be reported to <a href="https://nicecrew.digital/@tyler">@tyler@nicecrew.digital</a></code></pre>
</div>
</div>
</body>
</html>
+24 -2
View File
@@ -3,12 +3,34 @@ var i;
for (i = 0; i < coll.length; i++) {
coll[i].addEventListener("click", function() {
this.classList.toggle("active");
var content = this.nextElementSibling;
if (content.style.display === "block") {
var isCurrentlyOpen = content.style.display === "block" || content.style.display === "grid";
// If this collapsible has a parent with book-grid class, close all siblings in that grid
var parentGrid = this.closest('.book-grid');
if (parentGrid) {
var siblings = parentGrid.querySelectorAll('.collapsible');
for (var j = 0; j < siblings.length; j++) {
if (siblings[j] !== this) {
siblings[j].classList.remove("active");
var siblingContent = siblings[j].nextElementSibling;
if (siblingContent) {
siblingContent.style.display = "none";
}
}
}
}
// Toggle current element
this.classList.toggle("active");
if (isCurrentlyOpen) {
content.style.display = "none";
} else {
if (content.classList.contains('chapter-grid')) {
content.style.display = "grid";
} else {
content.style.display = "block";
}
}
});
}
+5
View File
@@ -0,0 +1,5 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">
<path fill="#6b9bd1" d="M16 4 C10 4 6 6 4 8 L4 26 C6 24 10 22 16 22 C22 22 26 24 28 26 L28 8 C26 6 22 4 16 4 Z"/>
<path fill="#85aede" d="M16 4 L16 22 C22 22 26 24 28 26 L28 8 C26 6 22 4 16 4 Z"/>
<line x1="16" y1="4" x2="16" y2="22" stroke="#3a3a3a" stroke-width="0.5"/>
</svg>

After

Width:  |  Height:  |  Size: 346 B