108 lines
3.0 KiB
Python
108 lines
3.0 KiB
Python
import os
|
|
from flask import Flask, render_template, abort, jsonify, request
|
|
from comics_data import COMICS, FULL_WIDTH_DEFAULT
|
|
|
|
app = Flask(__name__)
|
|
|
|
# Configuration
|
|
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', 'your-secret-key')
|
|
|
|
|
|
def is_full_width(comic):
|
|
"""Determine if a comic should be full width based on global and per-comic settings"""
|
|
# If comic explicitly sets full_width, use that value
|
|
if 'full_width' in comic:
|
|
return comic['full_width']
|
|
# Otherwise use the global default
|
|
return FULL_WIDTH_DEFAULT
|
|
|
|
|
|
def enrich_comic(comic):
|
|
"""Add computed properties to comic data"""
|
|
if comic is None:
|
|
return None
|
|
enriched = comic.copy()
|
|
enriched['full_width'] = is_full_width(comic)
|
|
return enriched
|
|
|
|
|
|
def get_comic_by_number(number):
|
|
"""Get a comic by its number"""
|
|
for comic in COMICS:
|
|
if comic['number'] == number:
|
|
return enrich_comic(comic)
|
|
return None
|
|
|
|
|
|
def get_latest_comic():
|
|
"""Get the most recent comic"""
|
|
if COMICS:
|
|
return enrich_comic(COMICS[-1])
|
|
return None
|
|
|
|
|
|
@app.route('/')
|
|
def index():
|
|
"""Home page - shows latest comic"""
|
|
comic = get_latest_comic()
|
|
if not comic:
|
|
return render_template('index.html', title='Latest Comic',
|
|
comic=None, total_comics=0)
|
|
return render_template('index.html', title='Latest Comic',
|
|
comic=comic, total_comics=len(COMICS))
|
|
|
|
|
|
@app.route('/comic/<int:comic_id>')
|
|
def comic(comic_id):
|
|
"""View a specific comic"""
|
|
comic = get_comic_by_number(comic_id)
|
|
if not comic:
|
|
abort(404)
|
|
return render_template('comic.html', title=f"Comic #{comic_id}",
|
|
comic=comic, total_comics=len(COMICS))
|
|
|
|
|
|
@app.route('/archive')
|
|
def archive():
|
|
"""Archive page showing all comics"""
|
|
# Reverse order to show newest first
|
|
comics = [enrich_comic(comic) for comic in reversed(COMICS)]
|
|
return render_template('archive.html', title='Archive',
|
|
comics=comics)
|
|
|
|
|
|
@app.route('/about')
|
|
def about():
|
|
"""About page"""
|
|
return render_template('about.html', title='About')
|
|
|
|
|
|
@app.route('/api/comics')
|
|
def api_comics():
|
|
"""API endpoint - returns all comics as JSON"""
|
|
return jsonify([enrich_comic(comic) for comic in COMICS])
|
|
|
|
|
|
@app.route('/api/comics/<int:comic_id>')
|
|
def api_comic(comic_id):
|
|
"""API endpoint - returns a specific comic as JSON"""
|
|
comic = get_comic_by_number(comic_id)
|
|
if not comic:
|
|
return jsonify({'error': 'Comic not found'}), 404
|
|
return jsonify(comic)
|
|
|
|
|
|
@app.errorhandler(404)
|
|
def page_not_found(e):
|
|
"""404 error handler"""
|
|
# Return JSON for API requests
|
|
if request.path.startswith('/api/'):
|
|
return jsonify({'error': 'Not found'}), 404
|
|
# Return HTML for regular pages
|
|
return render_template('404.html', title='Page Not Found'), 404
|
|
|
|
|
|
if __name__ == '__main__':
|
|
port = int(os.environ.get('PORT', 3000))
|
|
debug = os.environ.get('DEBUG', 'False').lower() in ('true', '1', 't')
|
|
app.run(debug=debug, port=port) |