93 lines
2.8 KiB
Python
93 lines
2.8 KiB
Python
from fastapi import FastAPI, Request, HTTPException
|
|
from fastapi.responses import HTMLResponse
|
|
from fastapi.staticfiles import StaticFiles
|
|
from fastapi.templating import Jinja2Templates
|
|
from sqlmodel import Field, Session, SQLModel, create_engine, select
|
|
|
|
import json
|
|
from models import ordinateur
|
|
|
|
app = FastAPI()
|
|
|
|
ordi1 = ordinateur()
|
|
|
|
app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
|
|
|
|
templates = Jinja2Templates(directory="templates")
|
|
|
|
# Code above omitted
|
|
|
|
sqlite_file_name = "database.db"
|
|
sqlite_url = f"sqlite:///{sqlite_file_name}"
|
|
|
|
connect_args = {"check_same_thread": False}
|
|
engine = create_engine(sqlite_url, connect_args=connect_args)
|
|
|
|
# Code below omitted
|
|
|
|
@app.get("/ordi1", response_class=HTMLResponse)
|
|
async def get_ordi1_info(request: Request):
|
|
return templates.TemplateResponse(
|
|
request=request, name="item.html", context={"ordi": ordi1}
|
|
)
|
|
|
|
@app.post("/endpoint")
|
|
async def receive_info(request: Request):
|
|
# Lire le body brut
|
|
body = await request.body()
|
|
print(body)
|
|
|
|
|
|
# Parser le JSON
|
|
try:
|
|
data = json.loads(body)
|
|
except json.JSONDecodeError:
|
|
raise HTTPException(status_code=400, detail="Invalid JSON")
|
|
|
|
|
|
|
|
# Debug
|
|
hardware = data.get('HARDWARE',{})
|
|
software = data.get('SOFTWARE',{})
|
|
|
|
print("Infos reçues :", data)
|
|
ordi1.mb_serial = hardware.get("mb_serial", "")
|
|
ordi1.hostname = hardware.get("hostname", "")
|
|
ordi1.cpu = hardware.get("cpu", "")
|
|
ordi1.cpu_id = hardware.get("cpu_id", "")
|
|
ordi1.memory_mb = hardware.get("memory_mb", "")
|
|
ordi1.ram_size = hardware.get("ram_size", "")
|
|
ordi1.ram_gen = hardware.get("ram_gen", "")
|
|
ordi1.sizes = hardware.get("sizes", "")
|
|
ordi1.cpu_freq_min = hardware.get("cpu_freq_min", "")
|
|
ordi1.cpu_freq_cur = hardware.get("cpu_freq_cur", "")
|
|
ordi1.cpu_freq_max = hardware.get("cpu_freq_max", "")
|
|
ordi1.gpu_model = hardware.get("gpu_model", "")
|
|
ordi1.chassis_serial = hardware.get("chassis_serial", "")
|
|
ordi1.cpu_cores_nb = hardware.get("cpu_cores_nb", "")
|
|
ordi1.cpu_threads_nb = hardware.get("cpu_threads_nb", "")
|
|
ordi1.ram_number = hardware.get("ram_number", "")
|
|
ordi1.ram_slots_nb = hardware.get("ram_slots_nb", "")
|
|
ordi1.mac_adresse = hardware.get("mac_adress", "")
|
|
|
|
ordi1.os = software.get("os", "")
|
|
ordi1.arch = software.get("arch", "")
|
|
ordi1.desktop = software.get("desktop", "")
|
|
ordi1.wm = software.get("wm", "")
|
|
ordi1.kernel = software.get("kernel", "")
|
|
|
|
#Test
|
|
print(f"Le serial de la mb est {ordi1.mb_serial}")
|
|
print(f"Hostname est {ordi1.hostname}")
|
|
print(f"Your cpu is {ordi1.cpu}")
|
|
print(f"Le id de ce cpu est {ordi1.cpu_id}")
|
|
print(f"le memory en megabytes {ordi1.memory_mb}")
|
|
print(f"Voici numéro de ram {ordi1.ram_number}")
|
|
return ({"status": "ok"})
|
|
|
|
|
|
#@app.get("/ordi1")
|
|
#async def get_ordi1_info():
|
|
# return ordi1
|