mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-12-15 23:05:23 -05:00
refactor(♻️): update 'about' page to new composition API (#667)
* test-commit * Remove PR Name Checker * refactor(backend): ♻️ split unrelated routes into clearer router paths Add an /app and /admin router base paths to split previously grouped public/admin data into different paths. Part of a longer migration to move 'admin' operations under the admin path. * refactor(backend): ♻️ rename imports * refactor(frontend): ♻️ refactor frontend API and Pages to refelect new API design Co-authored-by: hay-kot <hay-kot@pm.me>
This commit is contained in:
@@ -4,7 +4,7 @@ from fastapi.middleware.gzip import GZipMiddleware
|
||||
|
||||
from mealie.core.config import APP_VERSION, settings
|
||||
from mealie.core.root_logger import get_logger
|
||||
from mealie.routes import backup_routes, debug_routes, migration_routes, router, utility_routes
|
||||
from mealie.routes import backup_routes, migration_routes, router, utility_routes
|
||||
from mealie.routes.about import about_router
|
||||
from mealie.routes.mealplans import meal_plan_router
|
||||
from mealie.routes.media import media_router
|
||||
@@ -45,9 +45,6 @@ def api_routers():
|
||||
# Migration Routes
|
||||
app.include_router(migration_routes.router)
|
||||
# Debug routes
|
||||
app.include_router(debug_routes.public_router)
|
||||
app.include_router(debug_routes.admin_router)
|
||||
# Utility routes
|
||||
app.include_router(utility_routes.router)
|
||||
|
||||
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
from . import auth, categories, groups, recipe, shopping_lists, tags, unit_and_foods, users
|
||||
from . import admin, app, auth, categories, groups, recipe, shopping_lists, tags, unit_and_foods, users
|
||||
|
||||
router = APIRouter(prefix="/api")
|
||||
|
||||
router.include_router(app.router)
|
||||
router.include_router(auth.router)
|
||||
router.include_router(users.router)
|
||||
router.include_router(groups.router)
|
||||
@@ -11,5 +12,5 @@ router.include_router(recipe.router)
|
||||
router.include_router(unit_and_foods.router)
|
||||
router.include_router(categories.router)
|
||||
router.include_router(tags.router)
|
||||
|
||||
router.include_router(shopping_lists.router)
|
||||
router.include_router(admin.router)
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
from . import defaults, events, notifications
|
||||
from . import events, notifications
|
||||
|
||||
about_router = APIRouter(prefix="/api/about")
|
||||
|
||||
about_router.include_router(events.router, tags=["Events: CRUD"])
|
||||
about_router.include_router(notifications.router, tags=["Events: Notifications"])
|
||||
about_router.include_router(defaults.router, tags=["Recipe: Defaults"])
|
||||
|
||||
8
mealie/routes/admin/__init__.py
Normal file
8
mealie/routes/admin/__init__.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
from . import admin_about, admin_log
|
||||
|
||||
router = APIRouter(prefix="/admin")
|
||||
|
||||
router.include_router(admin_about.router, tags=["Admin: About"])
|
||||
router.include_router(admin_log.router, tags=["Admin: Log"])
|
||||
38
mealie/routes/admin/admin_about.py
Normal file
38
mealie/routes/admin/admin_about.py
Normal file
@@ -0,0 +1,38 @@
|
||||
from fastapi import APIRouter, Depends
|
||||
from sqlalchemy.orm.session import Session
|
||||
|
||||
from mealie.core.config import APP_VERSION, get_settings
|
||||
from mealie.db.database import get_database
|
||||
from mealie.db.db_setup import generate_session
|
||||
from mealie.schema.admin.about import AdminAboutInfo, AppStatistics
|
||||
|
||||
router = APIRouter(prefix="/about")
|
||||
|
||||
|
||||
@router.get("", response_model=AdminAboutInfo)
|
||||
async def get_app_info():
|
||||
""" Get general application information """
|
||||
settings = get_settings()
|
||||
|
||||
return AdminAboutInfo(
|
||||
production=settings.PRODUCTION,
|
||||
version=APP_VERSION,
|
||||
demo_status=settings.IS_DEMO,
|
||||
api_port=settings.API_PORT,
|
||||
api_docs=settings.API_DOCS,
|
||||
db_type=settings.DB_ENGINE,
|
||||
db_url=settings.DB_URL_PUBLIC,
|
||||
default_group=settings.DEFAULT_GROUP,
|
||||
)
|
||||
|
||||
|
||||
@router.get("/statistics", response_model=AppStatistics)
|
||||
async def get_app_statistics(session: Session = Depends(generate_session)):
|
||||
db = get_database()
|
||||
return AppStatistics(
|
||||
total_recipes=db.recipes.count_all(session),
|
||||
uncategorized_recipes=db.recipes.count_uncategorized(session),
|
||||
untagged_recipes=db.recipes.count_untagged(session),
|
||||
total_users=db.users.count_all(session),
|
||||
total_groups=db.groups.count_all(session),
|
||||
)
|
||||
44
mealie/routes/admin/admin_log.py
Normal file
44
mealie/routes/admin/admin_log.py
Normal file
@@ -0,0 +1,44 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
from mealie.core.root_logger import LOGGER_FILE
|
||||
from mealie.core.security import create_file_token
|
||||
|
||||
router = APIRouter(prefix="/logs")
|
||||
|
||||
|
||||
@router.get("/{num}")
|
||||
async def get_log(num: int):
|
||||
""" Doc Str """
|
||||
with open(LOGGER_FILE, "rb") as f:
|
||||
log_text = tail(f, num)
|
||||
return log_text
|
||||
|
||||
|
||||
@router.get("")
|
||||
async def get_log_file():
|
||||
""" Returns a token to download a file """
|
||||
return {"fileToken": create_file_token(LOGGER_FILE)}
|
||||
|
||||
|
||||
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"/n".join(all_read_text.splitlines()[-total_lines_wanted:])
|
||||
8
mealie/routes/app/__init__.py
Normal file
8
mealie/routes/app/__init__.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
from . import app_about, app_defaults
|
||||
|
||||
router = APIRouter(prefix="/app")
|
||||
|
||||
router.include_router(app_about.router, tags=["App: About"])
|
||||
router.include_router(app_defaults.router, tags=["App: Defaults"])
|
||||
18
mealie/routes/app/app_about.py
Normal file
18
mealie/routes/app/app_about.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
from mealie.core.config import APP_VERSION, get_settings
|
||||
from mealie.schema.admin.about import AppInfo
|
||||
|
||||
router = APIRouter(prefix="/about")
|
||||
|
||||
|
||||
@router.get("", response_model=AppInfo)
|
||||
async def get_app_info():
|
||||
""" Get general application information """
|
||||
settings = get_settings()
|
||||
|
||||
return AppInfo(
|
||||
version=APP_VERSION,
|
||||
demo_status=settings.IS_DEMO,
|
||||
production=settings.PRODUCTION,
|
||||
)
|
||||
@@ -1,11 +1,11 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
from mealie.schema.recipe import RecipeSettings
|
||||
from mealie.schema.recipe.recipe_settings import RecipeSettings
|
||||
|
||||
router = APIRouter(prefix="/recipes")
|
||||
router = APIRouter(prefix="/defaults")
|
||||
|
||||
|
||||
@router.get("/defaults")
|
||||
@router.get("/recipe", response_model=RecipeSettings)
|
||||
async def get_recipe_settings_defaults():
|
||||
""" Returns the Default Settings for Recieps as set by ENV variables """
|
||||
|
||||
@@ -1,89 +0,0 @@
|
||||
from fastapi import Depends
|
||||
from fastapi.routing import APIRouter
|
||||
from sqlalchemy.orm.session import Session
|
||||
|
||||
from mealie.core.config import APP_VERSION, settings
|
||||
from mealie.core.root_logger import LOGGER_FILE
|
||||
from mealie.core.security import create_file_token
|
||||
from mealie.db.database import db
|
||||
from mealie.db.db_setup import generate_session
|
||||
from mealie.routes.routers import AdminAPIRouter
|
||||
from mealie.schema.admin import AppInfo, AppStatistics, DebugInfo
|
||||
|
||||
admin_router = AdminAPIRouter(prefix="/api/debug", tags=["Debug"])
|
||||
public_router = APIRouter(prefix="/api/debug", tags=["Debug"])
|
||||
|
||||
|
||||
@admin_router.get("")
|
||||
async def get_debug_info():
|
||||
""" Returns general information about the application for debugging """
|
||||
|
||||
return DebugInfo(
|
||||
production=settings.PRODUCTION,
|
||||
version=APP_VERSION,
|
||||
demo_status=settings.IS_DEMO,
|
||||
api_port=settings.API_PORT,
|
||||
api_docs=settings.API_DOCS,
|
||||
db_type=settings.DB_ENGINE,
|
||||
db_url=settings.DB_URL_PUBLIC,
|
||||
default_group=settings.DEFAULT_GROUP,
|
||||
)
|
||||
|
||||
|
||||
@admin_router.get("/statistics")
|
||||
async def get_app_statistics(session: Session = Depends(generate_session)):
|
||||
return AppStatistics(
|
||||
total_recipes=db.recipes.count_all(session),
|
||||
uncategorized_recipes=db.recipes.count_uncategorized(session),
|
||||
untagged_recipes=db.recipes.count_untagged(session),
|
||||
total_users=db.users.count_all(session),
|
||||
total_groups=db.groups.count_all(session),
|
||||
)
|
||||
|
||||
|
||||
@public_router.get("/version")
|
||||
async def get_mealie_version():
|
||||
""" Returns the current version of mealie"""
|
||||
return AppInfo(
|
||||
version=APP_VERSION,
|
||||
demo_status=settings.IS_DEMO,
|
||||
production=settings.PRODUCTION,
|
||||
)
|
||||
|
||||
|
||||
@admin_router.get("/log/{num}")
|
||||
async def get_log(num: int):
|
||||
""" Doc Str """
|
||||
with open(LOGGER_FILE, "rb") as f:
|
||||
log_text = tail(f, num)
|
||||
return log_text
|
||||
|
||||
|
||||
@admin_router.get("/log")
|
||||
async def get_log_file():
|
||||
""" Returns a token to download a file """
|
||||
return {"fileToken": create_file_token(LOGGER_FILE)}
|
||||
|
||||
|
||||
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"/n".join(all_read_text.splitlines()[-total_lines_wanted:])
|
||||
@@ -17,7 +17,7 @@ class AppInfo(CamelModel):
|
||||
demo_status: bool
|
||||
|
||||
|
||||
class DebugInfo(AppInfo):
|
||||
class AdminAboutInfo(AppInfo):
|
||||
api_port: int
|
||||
api_docs: bool
|
||||
db_type: str
|
||||
|
||||
Reference in New Issue
Block a user