isbn-sort/isbn_sort/__init__.py

38 lines
786 B
Python
Raw Normal View History

2024-04-08 12:18:12 +02:00
from flask import Flask, redirect, g, send_file
2024-04-03 18:30:04 +02:00
import secrets
2024-04-08 12:18:12 +02:00
import os
2024-04-03 16:54:54 +02:00
2024-04-05 17:24:02 +02:00
from .app import bp as app_bp
2024-04-03 16:54:54 +02:00
app = Flask(__name__)
app.config["DATABASE"] = "data/books.sqlite"
2024-04-03 18:30:04 +02:00
app.config["SECRET_KEY"] = secrets.token_hex()
2024-04-03 16:54:54 +02:00
2024-04-05 17:24:02 +02:00
app.register_blueprint(app_bp)
2024-04-03 16:54:54 +02:00
2024-04-08 12:18:12 +02:00
@app.route("/")
def index():
return redirect("/app", 301)
@app.route("/favicon.ico")
def favicon():
return send_file("static/favicon.ico")
2024-04-05 17:24:02 +02:00
@app.route("/<path>")
def handle_redirect(path):
2024-04-08 12:18:12 +02:00
path = path if path.startswith("/") else "/"+path
2024-04-05 17:24:02 +02:00
if not path.startswith("/app"):
return redirect("/app"+path, 301)
abort(404)
2024-04-03 18:30:04 +02:00
2024-04-03 16:54:54 +02:00
@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