Init
This commit is contained in:
parent
604ee83631
commit
d3e990384d
9 changed files with 118 additions and 0 deletions
0
app/__init__.py
Normal file
0
app/__init__.py
Normal file
4
app/__main__.py
Normal file
4
app/__main__.py
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
if __name__ == '__main__':
|
||||
from main import run
|
||||
|
||||
run()
|
||||
0
app/api/__init__.py
Normal file
0
app/api/__init__.py
Normal file
8
app/api/v1.py
Normal file
8
app/api/v1.py
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
from fastapi import APIRouter
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/", summary="Main API")
|
||||
async def root():
|
||||
return {"message": "Witaj w API v12"}
|
||||
29
app/main.py
Normal file
29
app/main.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
from fastapi import FastAPI, Request
|
||||
from fastapi.responses import HTMLResponse
|
||||
from jinja2 import Environment, FileSystemLoader, select_autoescape
|
||||
|
||||
from app.api.v1 import router as api_v1_router
|
||||
|
||||
# Inicjalizacja Jinja2
|
||||
templates_env = Environment(
|
||||
loader=FileSystemLoader("app/templates"),
|
||||
autoescape=select_autoescape(["html", "xml"]),
|
||||
)
|
||||
|
||||
app = FastAPI(title="Karl", version="0.1.0")
|
||||
|
||||
# Rejestracja routera API pod /api/v1
|
||||
app.include_router(api_v1_router, prefix="/api/v1", tags=["v1"])
|
||||
|
||||
|
||||
# Przykładowy endpoint HTML
|
||||
@app.get("/", response_class=HTMLResponse)
|
||||
async def index(request: Request) -> HTMLResponse:
|
||||
template = templates_env.get_template("index.html")
|
||||
html = template.render(title="Strona główna", request=request)
|
||||
return HTMLResponse(content=html)
|
||||
|
||||
|
||||
def run() -> None:
|
||||
import uvicorn
|
||||
uvicorn.run("app.main:app", host="127.0.0.1", port=8000, reload=True)
|
||||
39
app/templates/index.html
Normal file
39
app/templates/index.html
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
<!doctype html>
|
||||
<html lang="pl">
|
||||
<head>
|
||||
<meta charset="utf-8"/>
|
||||
<title>{{ title }}</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
||||
<style>
|
||||
body {
|
||||
font-family: system-ui, -apple-system, Segoe UI, Roboto, Ubuntu, Cantarell, Noto Sans, Helvetica, Arial, "Apple Color Emoji", "Segoe UI Emoji";
|
||||
margin: 2rem;
|
||||
}
|
||||
|
||||
code {
|
||||
background: #f4f4f4;
|
||||
padding: 0.2rem 0.4rem;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
a {
|
||||
color: #0b5fff;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
a:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>{{ title }}</h1>
|
||||
<p>To jest prosta strona Jinja2 serwowana przez FastAPI.</p>
|
||||
<ul>
|
||||
<li>UI OpenAPI: <a href="/docs">/docs</a></li>
|
||||
<li>OpenAPI JSON: <a href="/openapi.json">/openapi.json</a></li>
|
||||
<li>API v1: <a href="/api/v1/">/api/v1/</a></li>
|
||||
</ul>
|
||||
<p>Uruchamianie serwera lokalnie: <code>uv run app</code> lub <code>uv run uvicorn app.main:app --reload</code></p>
|
||||
</body>
|
||||
</html>
|
||||
Loading…
Add table
Add a link
Reference in a new issue