-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
515 lines (428 loc) Β· 23.2 KB
/
app.py
File metadata and controls
515 lines (428 loc) Β· 23.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
# app.py β complete micro-MVP
import os, uuid
from datetime import datetime, timedelta, date
from typing import Optional
from dotenv import load_dotenv
from fastapi import FastAPI, Request, Form, Depends, HTTPException
from fastapi.responses import HTMLResponse, RedirectResponse, JSONResponse, FileResponse
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles
from sqlmodel import Session, select, func
from app_helpers.services.ai_providers import (
AIConfigurationError,
get_ai_provider_config,
validate_ai_provider_config,
)
# Load environment variables first
load_dotenv()
# Validate AI provider configuration early to fail fast on misconfiguration
try:
validate_ai_provider_config()
except AIConfigurationError as exc:
raise RuntimeError(f"Invalid AI provider configuration: {exc}") from exc
# Cache current AI provider configuration for later use
CURRENT_AI_PROVIDER_CONFIG = get_ai_provider_config()
# Database path is now configured in models.py via intelligent detection
from models import engine, User, Prayer, InviteToken, Session as SessionModel, PrayerMark, PrayerSkip, AuthenticationRequest, AuthApproval, AuthAuditLog, SecurityLog, PrayerAttribute, PrayerActivityLog
from sqlmodel import text
import sqlite3
# βββββββββ Config βββββββββ
SESSION_DAYS = 14
# TOKEN_EXP_H moved to app_helpers/services/token_service.py for centralization
MAX_AUTH_REQUESTS_PER_HOUR = 3 # Rate limit for auth requests
MAX_FAILED_ATTEMPTS = 5 # Max failed login attempts before temporary block
BLOCK_DURATION_MINUTES = 15 # How long to block after max failed attempts
# Multi-device authentication settings
MULTI_DEVICE_AUTH_ENABLED = os.getenv("MULTI_DEVICE_AUTH_ENABLED", "true").lower() == "true"
REQUIRE_APPROVAL_FOR_EXISTING_USERS = os.getenv("REQUIRE_APPROVAL_FOR_EXISTING_USERS", "true").lower() == "true"
REQUIRE_INVITE_LOGIN_VERIFICATION = os.getenv("REQUIRE_INVITE_LOGIN_VERIFICATION", "false").lower() == "true"
PEER_APPROVAL_COUNT = int(os.getenv("PEER_APPROVAL_COUNT", "2"))
# Auto-migration settings
AUTO_MIGRATE_ON_STARTUP = os.getenv("AUTO_MIGRATE_ON_STARTUP", "false").lower() == "true"
# Text Archive Settings
TEXT_ARCHIVE_ENABLED = os.getenv("TEXT_ARCHIVE_ENABLED", "true").lower() == "true"
TEXT_ARCHIVE_BASE_DIR = os.getenv("TEXT_ARCHIVE_BASE_DIR", "./text_archives")
TEXT_ARCHIVE_COMPRESSION_AFTER_DAYS = int(os.getenv("TEXT_ARCHIVE_COMPRESSION_AFTER_DAYS", "365"))
# Prayer Mode Settings
PRAYER_MODE_ENABLED = os.getenv("PRAYER_MODE_ENABLED", "true").lower() == "true"
# Daily Priority Feature Flag
DAILY_PRIORITY_ENABLED = os.getenv("DAILY_PRIORITY_ENABLED", "false").lower() == "true"
# Prayer Categorization Feature Flags
PRAYER_CATEGORIZATION_ENABLED = os.getenv("PRAYER_CATEGORIZATION_ENABLED", "false").lower() == "true"
PRAYER_CATEGORY_BADGES_ENABLED = os.getenv("PRAYER_CATEGORY_BADGES_ENABLED", "false").lower() == "true"
PRAYER_CATEGORY_FILTERING_ENABLED = os.getenv("PRAYER_CATEGORY_FILTERING_ENABLED", "false").lower() == "true"
AI_CATEGORIZATION_ENABLED = os.getenv("AI_CATEGORIZATION_ENABLED", "false").lower() == "true"
KEYWORD_FALLBACK_ENABLED = os.getenv("KEYWORD_FALLBACK_ENABLED", "false").lower() == "true"
CATEGORIZATION_CIRCUIT_BREAKER_ENABLED = os.getenv("CATEGORIZATION_CIRCUIT_BREAKER_ENABLED", "false").lower() == "true"
SAFETY_SCORING_ENABLED = os.getenv("SAFETY_SCORING_ENABLED", "false").lower() == "true"
HIGH_SAFETY_FILTER_ENABLED = os.getenv("HIGH_SAFETY_FILTER_ENABLED", "false").lower() == "true"
SAFETY_BADGES_VISIBLE = os.getenv("SAFETY_BADGES_VISIBLE", "false").lower() == "true"
SPECIFICITY_BADGES_ENABLED = os.getenv("SPECIFICITY_BADGES_ENABLED", "false").lower() == "true"
CATEGORY_FILTER_DROPDOWN_ENABLED = os.getenv("CATEGORY_FILTER_DROPDOWN_ENABLED", "false").lower() == "true"
FILTER_PERSISTENCE_ENABLED = os.getenv("FILTER_PERSISTENCE_ENABLED", "false").lower() == "true"
OFFLINE_PWA_ENABLED = os.getenv("OFFLINE_PWA_ENABLED", "true").lower() == "true"
CATEGORIZATION_METADATA_EXPORT = os.getenv("CATEGORIZATION_METADATA_EXPORT", "false").lower() == "true"
HISTORICAL_CATEGORIZATION_ENABLED = os.getenv("HISTORICAL_CATEGORIZATION_ENABLED", "false").lower() == "true"
ADMIN_CATEGORIZATION_OVERRIDE = os.getenv("ADMIN_CATEGORIZATION_OVERRIDE", "false").lower() == "true"
CATEGORIZATION_CACHING_ENABLED = os.getenv("CATEGORIZATION_CACHING_ENABLED", "false").lower() == "true"
BACKGROUND_CATEGORIZATION_ENABLED = os.getenv("BACKGROUND_CATEGORIZATION_ENABLED", "false").lower() == "true"
BATCH_CATEGORIZATION_ENABLED = os.getenv("BATCH_CATEGORIZATION_ENABLED", "false").lower() == "true"
# Use shared templates instance with filters registered
from app_helpers.shared_templates import templates
# (Provider instances are created on demand via app_helpers.services.ai_providers)
# βββββββββ Import extracted helper functions for backward compatibility βββββββββ
# These imports maintain all existing function entry points in app.py namespace
from app_helpers.services.auth_helpers import (
create_session, current_user, require_full_auth, is_admin,
create_auth_request, approve_auth_request, get_pending_requests_for_approval,
log_auth_action, log_security_event, check_rate_limit,
validate_session_security, cleanup_expired_requests
)
from app_helpers.services.prayer_helpers import (
get_feed_counts, find_compatible_prayer_partner,
todays_prompt, generate_prayer
)
from app_helpers.services.invite_helpers import (
get_invite_tree, get_user_descendants, get_user_invite_path,
get_invite_stats, _build_user_tree_node, _collect_descendants, _calculate_max_depth
)
from app_helpers.utils.database_helpers import migrate_database
from app_helpers.utils.enhanced_migration import MigrationManager
# βββββββββ Import extracted route modules βββββββββ
from app_helpers.routes.auth_routes import router as auth_router
from app_helpers.routes.prayer_routes import router as prayer_router
from app_helpers.routes.admin_routes import router as admin_router
from app_helpers.routes.user_routes import router as user_router
from app_helpers.routes.invite_routes import router as invite_router
from app_helpers.routes.general_routes import router as general_router
from app_helpers.routes.changelog_routes import router as changelog_router
from app_helpers.routes.archive_routes import router as archive_router
from app_helpers.routes.file_routes import router as file_router
from app_helpers.routes.email_settings_routes import router as email_settings_router
from app_helpers.routes.public_routes import router as public_router
app = FastAPI()
# Health check endpoint for deployment monitoring
@app.get("/health")
async def health_check():
"""Health check endpoint that verifies database connectivity"""
try:
# Check database connectivity
db_path = "thywill.db"
if not os.path.exists(db_path):
return JSONResponse(
content={
"status": "unhealthy",
"error": "Database file not found"
},
status_code=500
)
# Try to connect and run a simple query
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute("SELECT 1")
conn.close()
return JSONResponse(
content={
"status": "healthy",
"database": "connected",
"timestamp": datetime.now().isoformat()
},
status_code=200
)
except Exception as e:
return JSONResponse(
content={
"status": "unhealthy",
"error": str(e)
},
status_code=500
)
# Test route for share functionality
@app.get("/test-share", response_class=HTMLResponse)
async def test_share(request: Request):
"""Test page for share functionality across different browsers"""
return templates.TemplateResponse("test_share.html", {
"request": request,
"test_url": "https://thywill.com/claim/abcd1234test5678",
"test_title": "Join ThyWill Prayer Community"
})
if OFFLINE_PWA_ENABLED:
@app.get("/service-worker.js", include_in_schema=False)
async def service_worker_asset():
"""Serve the PWA service worker from the root scope."""
return FileResponse("static/js/service-worker.js", media_type="application/javascript")
@app.get("/manifest.webmanifest", include_in_schema=False)
async def manifest_asset():
"""Expose the web app manifest for install prompts."""
return FileResponse("static/manifest.webmanifest", media_type="application/manifest+json")
# Mount static files
app.mount("/static", StaticFiles(directory="static"), name="static")
# Include authentication routes
app.include_router(auth_router)
# Include public routes (must be before prayer routes for root route handling)
app.include_router(public_router)
# Include prayer routes
app.include_router(prayer_router)
# Include admin routes
app.include_router(admin_router)
# Include user routes
app.include_router(user_router)
# Include archive routes
app.include_router(archive_router)
# Include file routes
app.include_router(file_router)
# Include invite routes
app.include_router(invite_router)
# Include general routes
app.include_router(general_router)
# Include changelog routes
app.include_router(changelog_router)
# Email settings routes
app.include_router(email_settings_router)
# Custom exception handler for 401 unauthorized errors
@app.exception_handler(401)
async def unauthorized_exception_handler(request: Request, exc: HTTPException):
"""Custom handler for 401 unauthorized errors to show user-friendly page"""
reason = exc.detail if exc.detail else "no_session"
return templates.TemplateResponse("unauthorized.html", {
"request": request,
"reason": reason,
"return_url": request.url.path,
"MULTI_DEVICE_AUTH_ENABLED": MULTI_DEVICE_AUTH_ENABLED
}, status_code=401)
# Custom exception handler for 404 not found errors
@app.exception_handler(404)
async def not_found_exception_handler(request: Request, exc: HTTPException):
"""Custom handler for 404 not found errors to show user-friendly page"""
# For API routes, return JSON responses
if request.url.path.startswith("/api/"):
return JSONResponse(
status_code=404,
content={"detail": str(exc.detail) if exc.detail else "Not found"}
)
# Check if this is an auth-related 404
if request.url.path.startswith("/auth/") and "Authentication request not found" in str(exc.detail):
return templates.TemplateResponse("auth_not_found.html", {
"request": request,
"MULTI_DEVICE_AUTH_ENABLED": MULTI_DEVICE_AUTH_ENABLED
}, status_code=404)
# For other 404s, show generic error page
return templates.TemplateResponse("error.html", {
"request": request,
"error_code": 404,
"error_title": "Page Not Found",
"error_message": "The page you're looking for doesn't exist or has been moved."
}, status_code=404)
# Custom exception handler for 500 internal server errors
@app.exception_handler(500)
async def internal_error_exception_handler(request: Request, exc: Exception):
"""Custom handler for 500 internal server errors"""
return templates.TemplateResponse("error.html", {
"request": request,
"error_code": 500,
"error_title": "Internal Server Error",
"error_message": "Something went wrong on our end. Please try again later."
}, status_code=500)
# Custom exception handler for 403 forbidden errors
@app.exception_handler(403)
async def forbidden_exception_handler(request: Request, exc: HTTPException):
"""Custom handler for 403 forbidden errors"""
# For API routes, return JSON responses
if request.url.path.startswith("/api/"):
return JSONResponse(
status_code=403,
content={"detail": str(exc.detail) if exc.detail else "Forbidden"}
)
return templates.TemplateResponse("error.html", {
"request": request,
"error_code": 403,
"error_title": "Access Forbidden",
"error_message": "You don't have permission to access this resource."
}, status_code=403)
# βββββββββ Extracted functions now imported from helper modules βββββββββ
# Auth functions: create_session, current_user, require_full_auth, is_admin, etc.
# Prayer functions: get_feed_counts, get_filtered_prayers_for_user, etc.
# Invite functions: get_invite_tree, get_user_descendants, etc.
# Database functions: migrate_database
# βββββββββ Routes βββββββββ
# Prayer routes moved to app_helpers/routes/prayer_routes.py
# Admin route moved to app_helpers/routes/admin_routes.py
# βββββββββ Invite-claim flow routes moved to app_helpers/routes/auth_routes.py βββββββββ
# βββββββββ Schema validation functions (module level for CLI access) βββββββββ
def column_exists(table_name: str, column_name: str) -> bool:
"""Check if a column exists in a table"""
with engine.connect() as conn:
from sqlalchemy import text
result = conn.execute(text(f"PRAGMA table_info({table_name})"))
columns = [row[1] for row in result.fetchall()]
return column_name in columns
def validate_schema_compatibility() -> tuple[bool, list[str]]:
"""Validate that database schema matches model expectations"""
required_columns = {
'invitetoken': ['token', 'created_by_user', 'usage_count', 'max_uses', 'expires_at', 'used_by_user_id', 'token_type'],
'user': ['display_name', 'created_at', 'invited_by_username', 'invite_token_used', 'welcome_message_dismissed', 'text_file_path'],
'prayer': ['id', 'author_username', 'text', 'generated_prayer', 'project_tag', 'created_at', 'flagged', 'text_file_path'],
'session': ['id', 'username', 'created_at', 'expires_at', 'auth_request_id', 'device_info', 'ip_address', 'is_fully_authenticated'],
'prayermark': ['id', 'username', 'prayer_id', 'created_at', 'text_file_path'],
'role': ['id', 'name', 'description', 'permissions', 'created_at', 'created_by', 'is_system_role'],
'user_roles': ['id', 'user_id', 'role_id', 'granted_by', 'granted_at', 'expires_at']
}
missing_columns = []
for table, columns in required_columns.items():
for column in columns:
if not column_exists(table, column):
missing_columns.append(f"{table}.{column}")
is_valid = len(missing_columns) == 0
return is_valid, missing_columns
# βββββββββ Startup: seed first invite βββββββββ
@app.on_event("startup")
def startup():
# Schedule daily priority cleanup task
import asyncio
from app_helpers.services.prayer_helpers import expire_old_priorities
async def daily_cleanup():
"""Daily task to expire old priority prayers (if auto-expiration is enabled)"""
import time
while True:
try:
# Only run cleanup if auto-expiration is enabled
if not os.getenv('DAILY_PRIORITY_AUTO_EXPIRE', 'false').lower() == 'true':
# If auto-expiration is disabled, sleep for 24 hours and check again
await asyncio.sleep(86400) # 24 hours
continue
# Run at midnight
current_time = datetime.now()
next_midnight = current_time.replace(hour=0, minute=0, second=0, microsecond=0) + timedelta(days=1)
sleep_seconds = (next_midnight - current_time).total_seconds()
await asyncio.sleep(sleep_seconds)
# Expire old priorities
with Session(engine) as s:
expired_count = expire_old_priorities(s)
if expired_count > 0:
print(f"ποΈ Expired {expired_count} old daily priority prayers")
except Exception as e:
print(f"β οΈ Error in daily priority cleanup: {e}")
await asyncio.sleep(3600) # Retry in 1 hour if error occurs
# Start the cleanup task
asyncio.create_task(daily_cleanup())
# Auto-migration on startup (if enabled and using file-based database)
from models import DATABASE_PATH
if AUTO_MIGRATE_ON_STARTUP and DATABASE_PATH != ':memory:':
print("π Auto-migration enabled - checking for pending migrations...")
try:
migration_manager = MigrationManager()
# Check for migration lock from previous failed attempt
if migration_manager.is_migration_locked():
print("β οΈ Migration lock detected - checking for partial migrations...")
migration_manager.handle_partial_migration_recovery()
# Check for pending migrations
pending = migration_manager.get_pending_migrations()
if pending:
print(f"π Auto-applying {len(pending)} pending migrations...")
# Check if any migration requires maintenance mode
requires_maintenance = any(
migration_manager.should_enable_maintenance_mode(migration)
for migration in pending
)
if requires_maintenance:
print("β οΈ Some migrations require maintenance mode - skipping auto-migration")
print(" Please run migrations manually: ./thywill migrate new")
else:
# Apply migrations automatically
applied = migration_manager.auto_migrate_on_startup()
migration_manager.validate_schema_integrity()
if applied:
print(f"β
Auto-migration completed: {', '.join(applied)}")
else:
print("β
No migrations applied")
else:
print("β
Database schema is up to date")
except Exception as e:
print(f"β Auto-migration failed: {e}")
print(" Application will continue - please run migrations manually")
# Run enhanced migrations on manual startup or fallback
elif not AUTO_MIGRATE_ON_STARTUP:
try:
migration_manager = MigrationManager()
# Check for migration lock from previous failed attempt
if migration_manager.is_migration_locked():
print("β οΈ Migration lock detected - checking for partial migrations...")
migration_manager.handle_partial_migration_recovery()
# Resolve dependencies and check pending migrations
pending = migration_manager.get_pending_migrations() # Returns in dependency order
if pending:
print(f"π Applying {len(pending)} pending migrations...")
# Check if any migration requires maintenance mode
for migration in pending:
if migration_manager.should_enable_maintenance_mode(migration):
print(f"β οΈ Migration {migration['id']} requires maintenance mode - manual deployment needed")
print(" Falling back to legacy migrations...")
migrate_database()
break
else:
# Apply migrations with locking
applied = migration_manager.auto_migrate_on_startup()
# Validate final schema state
migration_manager.validate_schema_integrity()
if applied:
print(f"β
Enhanced migrations completed: {', '.join(applied)}")
else:
print("β
No migrations needed - database is up to date")
else:
print("β
Database schema is up to date")
except Exception as e:
print(f"β Enhanced migration failed: {e}")
print(" Falling back to legacy migrations...")
# Fallback to legacy migration system
migrate_database()
# Run duplicate user migration
try:
from migrations.duplicate_user_migration import run_duplicate_user_migration
run_duplicate_user_migration()
except Exception as e:
print(f"β Duplicate user migration failed: {e}")
# Continue startup - this is not critical for basic functionality
# Run schema validation
print("π Validating database schema compatibility...")
is_valid, missing_columns = validate_schema_compatibility()
if not is_valid:
print(f"β οΈ Schema validation found {len(missing_columns)} missing columns:")
for col in missing_columns:
print(f" - {col}")
print("π§ Attempting defensive schema repairs...")
else:
print("β
Schema validation passed - all required columns present")
# Add missing columns if they don't exist
try:
with engine.connect() as conn:
from sqlalchemy import text
repairs_made = 0
# Add token_type column to invitetoken table if missing
if not column_exists('invitetoken', 'token_type'):
print("π§ Adding missing token_type column to invitetoken table...")
conn.execute(text("ALTER TABLE invitetoken ADD COLUMN token_type TEXT DEFAULT 'new_user'"))
conn.commit()
print("β
Added token_type column")
repairs_made += 1
# Add other critical missing columns as needed
# (Future defensive repairs can be added here)
if repairs_made > 0:
print(f"β
Schema repairs completed: {repairs_made} columns added")
except Exception as e:
print(f"β οΈ Schema fix warning: {e}")
# Continue startup - this is defensive, not critical
# Then seed invite using centralized token service
with Session(engine) as s:
if not s.exec(select(InviteToken)).first():
from app_helpers.services.token_service import create_system_token
token_info = create_system_token()
print("\n==== First-run invite token (admin):", token_info['token'], "====\n")
# Invite routes moved to app_helpers/routes/invite_routes.py
# User routes moved to app_helpers/routes/user_routes.py
# General routes moved to app_helpers/routes/general_routes.py
# βββββββββ Authentication request routes moved to app_helpers/routes/auth_routes.py βββββββββ
# Auth audit log route moved to app_helpers/routes/admin_routes.py
# Bulk approve route moved to app_helpers/routes/admin_routes.py
# Religious Preference Management API moved to app_helpers/routes/user_routes.py
# Logout and logged-out routes moved to app_helpers/routes/user_routes.py and app_helpers/routes/general_routes.py
# Religious stats API route moved to app_helpers/routes/admin_routes.py