isbn-sort/isbn_sort/__init__.py

45 lines
1021 B
Python

from flask import Flask, redirect, g, send_file
import secrets
import os
from .app import bp as app_bp
app = Flask(__name__)
app.config["DATABASE"] = "data/books.sqlite"
app.config["SECRET_KEY"] = secrets.token_hex()
if not os.path.exists(".google-books-key"):
print("No Google books api key found")
app.config["GOOGLE_BOOKS_KEY"] = None
else:
with open(".google-books-key") as f:
app.config["GOOGLE_BOOKS_KEY"] = f.read().strip()
app.register_blueprint(app_bp)
@app.route("/")
def index():
return redirect("/app", 301)
@app.route("/favicon.ico")
def favicon():
return send_file("static/favicon.ico")
@app.route("/<path>")
def handle_redirect(path):
path = path if path.startswith("/") else "/"+path
if not path.startswith("/app"):
return redirect("/app"+path, 301)
abort(404)
@app.after_request
def after_request(response):
"""Automatically close db after each request"""
if ('db' in g) and (g.db is not None):
g.db.close()
return response