root and project info change

This commit is contained in:
2025-06-04 02:21:48 +03:00
parent 35ad2ebcab
commit 236f4faeb3
4 changed files with 39 additions and 27 deletions

View File

@@ -96,5 +96,8 @@ For apply migrations:
### **TODO List** ### **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 - Implement tests

View File

@@ -1,21 +1,23 @@
from datetime import datetime from datetime import datetime
from pathlib import Path from pathlib import Path
from typing import List from typing import Dict, List
from alembic import command from alembic import command
from alembic.config import Config from alembic.config import Config
from fastapi import FastAPI, Depends, Request, HTTPException from fastapi import FastAPI, Depends, Request, HTTPException
from fastapi.responses import HTMLResponse, JSONResponse from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.templating import Jinja2Templates
from sqlmodel import SQLModel, Session, select from sqlmodel import SQLModel, Session, select
from .database import engine, get_session from .database import engine, get_session
from .models import Author, AuthorBase, Book, BookBase, AuthorBookLink from .models import Author, AuthorBase, Book, BookBase, AuthorBookLink
alembic_cfg = Config("alembic.ini") alembic_cfg = Config("alembic.ini")
templates = Jinja2Templates(directory=Path(__file__).parent / "templates")
app = FastAPI( app = FastAPI(
title="My API", title="LibraryAPI",
description="This is a sample API for managing authors and books.", description="This is a sample API for managing authors and books.",
version="1.0.0", version="1.0.1",
openapi_tags=[ openapi_tags=[
{ {
"name": "authors", "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 # Initialize the database
@app.on_event("startup") @app.on_event("startup")
def on_startup(): def on_startup():
@@ -45,21 +58,14 @@ def on_startup():
command.upgrade(alembic_cfg, "head") command.upgrade(alembic_cfg, "head")
# Root endpoint # Root endpoint
@app.get("/", tags=["misc"]) @app.get("/", response_class=HTMLResponse)
async def root(request: Request, html: str = ""): async def root(request: Request):
return templates.TemplateResponse("index.html", {"request": request, "data": get_info()})
if html != "": # API response # API Information endpoint
data = { @app.get("/api/info", tags=["misc"])
"title": app.title, async def api_info():
"version": app.version, return JSONResponse(content=get_info())
"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)
# Create an author # Create an author
@app.post("/authors/", response_model=Author, tags=["authors"]) @app.post("/authors/", response_model=Author, tags=["authors"])

View File

@@ -1,7 +1,9 @@
<!doctype html> <!doctype html>
<html> <html lang="en">
<head> <head>
<title>Добро пожаловать в API</title> <meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>{{ data.title }}</title>
<style> <style>
body { body {
font-family: Arial, sans-serif; font-family: Arial, sans-serif;
@@ -41,14 +43,14 @@
</style> </style>
</head> </head>
<body> <body>
<h1>Добро пожаловать в API!</h1> <h1>Welcome to {{ data.title }}!</h1>
<p>Description: {{ data.description }}</p>
<p>Version: {{ data.version }}</p>
<p>Current Time: {{ data.time }}</p>
<p>Status: {{ data.status }}</p>
<ul> <ul>
<li> <li><a href="/docs">Swagger UI</a></li>
<p>Попробуйте <a href="/docs">Swagger UI</a></p> <li><a href="/redoc">ReDoc</a></li>
</li>
<li>
<p>Попробуйте <a href="/redoc">ReDoc</a></p>
</li>
</ul> </ul>
</body> </body>
</html> </html>

View File

@@ -6,3 +6,4 @@ python-decouple
alembic alembic
pytest pytest
httpx httpx
jinja2