diff --git a/mealie/app.py b/mealie/app.py index ee1f44d2c..29f2fe347 100644 --- a/mealie/app.py +++ b/mealie/app.py @@ -6,6 +6,7 @@ from fastapi.staticfiles import StaticFiles from app_config import PORT, PRODUCTION, WEB_PATH, docs_url, redoc_url from routes import ( backup_routes, + debug_routes, meal_routes, migration_routes, setting_routes, @@ -68,6 +69,7 @@ def api_routers(): app.include_router(user_routes.router) # Migration Routes app.include_router(migration_routes.router) + app.include_router(debug_routes.router) if PRODUCTION: diff --git a/mealie/app_config.py b/mealie/app_config.py index 671da0d9e..608ebafe2 100644 --- a/mealie/app_config.py +++ b/mealie/app_config.py @@ -37,7 +37,7 @@ REQUIRED_DIRS = [ SQLITE_DIR, ] - +APP_VERSION = "v0.2.0" # General PRODUCTION = os.environ.get("ENV") PORT = int(os.getenv("mealie_port", 9000)) @@ -55,7 +55,7 @@ SQLITE_FILE = None DATABASE_TYPE = os.getenv("db_type", "sqlite") # mongo, sqlite if DATABASE_TYPE == "sqlite": USE_SQL = True - SQLITE_FILE = SQLITE_DIR.joinpath("mealie.sqlite") + SQLITE_FILE = SQLITE_DIR.joinpath(f"mealie_{APP_VERSION}.sqlite") else: raise Exception( diff --git a/mealie/routes/debug_routes.py b/mealie/routes/debug_routes.py new file mode 100644 index 000000000..562ad68d3 --- /dev/null +++ b/mealie/routes/debug_routes.py @@ -0,0 +1,62 @@ +import json +import os + +from app_config import DEBUG_DIR +from fastapi import APIRouter +from fastapi.responses import HTMLResponse +from utils.logger import LOGGER_FILE + +router = APIRouter(prefix="/api/debug", tags=["Debug"]) + + +@router.get("/last-recipe-json") +async def get_last_recipe_json(): + """ Doc Str """ + + with open(DEBUG_DIR.joinpath("last_recipe.json"), "r") as f: + return json.loads(f.read()) + + +@router.get("/log/{num}", response_class=HTMLResponse) +async def get_log(num: int): + """ Doc Str """ + with open(LOGGER_FILE, "rb") as f: + log_text = tail(f, num) + HTML_RESPONSE = f""" + + + Mealie Log + + +

+ {log_text} +

+ + + """ + + return HTML_RESPONSE + + +def tail(f, lines=20): + total_lines_wanted = lines + + BLOCK_SIZE = 1024 + f.seek(0, 2) + block_end_byte = f.tell() + lines_to_go = total_lines_wanted + block_number = -1 + blocks = [] + while lines_to_go > 0 and block_end_byte > 0: + if block_end_byte - BLOCK_SIZE > 0: + f.seek(block_number * BLOCK_SIZE, 2) + blocks.append(f.read(BLOCK_SIZE)) + else: + f.seek(0, 0) + blocks.append(f.read(block_end_byte)) + lines_found = blocks[-1].count(b"\n") + lines_to_go -= lines_found + block_end_byte -= BLOCK_SIZE + block_number -= 1 + all_read_text = b"".join(reversed(blocks)) + return b"
".join(all_read_text.splitlines()[-total_lines_wanted:])