diff --git a/README.md b/README.md index ee5f1df..c2bc0fd 100644 --- a/README.md +++ b/README.md @@ -96,5 +96,8 @@ For apply migrations: ### **TODO List** -- Split models for API and database +- Split models files for API and database +- new structure (src/app, src/migrations?) +- Geners table +- Poetry - Implement tests diff --git a/app/main.py b/app/main.py index e70e791..a072847 100644 --- a/app/main.py +++ b/app/main.py @@ -1,21 +1,23 @@ from datetime import datetime from pathlib import Path -from typing import List +from typing import Dict, List from alembic import command from alembic.config import Config from fastapi import FastAPI, Depends, Request, HTTPException from fastapi.responses import HTMLResponse, JSONResponse +from fastapi.templating import Jinja2Templates from sqlmodel import SQLModel, Session, select from .database import engine, get_session from .models import Author, AuthorBase, Book, BookBase, AuthorBookLink alembic_cfg = Config("alembic.ini") +templates = Jinja2Templates(directory=Path(__file__).parent / "templates") app = FastAPI( - title="My API", + title="LibraryAPI", description="This is a sample API for managing authors and books.", - version="1.0.0", + version="1.0.1", openapi_tags=[ { "name": "authors", @@ -36,6 +38,17 @@ app = FastAPI( ] ) +def get_info() -> Dict: + return { + "status": "ok", + "app_info": { + "title": app.title, + "version": app.version, + "description": app.description, + }, + "server_time": datetime.now().isoformat(), + } + # Initialize the database @app.on_event("startup") def on_startup(): @@ -45,21 +58,14 @@ def on_startup(): command.upgrade(alembic_cfg, "head") # Root endpoint -@app.get("/", tags=["misc"]) -async def root(request: Request, html: str = ""): +@app.get("/", response_class=HTMLResponse) +async def root(request: Request): + return templates.TemplateResponse("index.html", {"request": request, "data": get_info()}) - if html != "": # API response - data = { - "title": app.title, - "version": app.version, - "description": app.description, - "status": "ok" - } - return JSONResponse({"message": "Hello world!", "data": data, "time": datetime.now(), }) - else: # Browser response - with open(Path(__file__).parent / "index.html", 'r', encoding='utf-8') as file: - html_content = file.read() - return HTMLResponse(html_content) +# API Information endpoint +@app.get("/api/info", tags=["misc"]) +async def api_info(): + return JSONResponse(content=get_info()) # Create an author @app.post("/authors/", response_model=Author, tags=["authors"]) diff --git a/app/index.html b/app/templates/index.html similarity index 69% rename from app/index.html rename to app/templates/index.html index dd79520..c9b6997 100644 --- a/app/index.html +++ b/app/templates/index.html @@ -1,7 +1,9 @@ - +
-Description: {{ data.description }}
+Version: {{ data.version }}
+Current Time: {{ data.time }}
+Status: {{ data.status }}
Попробуйте Swagger UI
-Попробуйте ReDoc
-