rustdesk_web/backend/main.py

73 lines
2.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import sqlite3
import datetime
app = FastAPI()
# Создаем соединение с базой данных
conn = sqlite3.connect("/db/rustdesk.db", check_same_thread=False)
cursor = conn.cursor()
# Создаем таблицу, если её нет
cursor.execute("""
CREATE TABLE IF NOT EXISTS installs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
rust_id TEXT,
computer_name TEXT,
install_time TEXT
)
""")
conn.commit()
# Модель данных
class InstallData(BaseModel):
rust_id: str
computer_name: str
install_time: str
# Маршрут для получения всех записей
@app.get("/api/installs")
def get_installs():
cursor.execute("SELECT * FROM installs")
rows = cursor.fetchall()
# Преобразуем результаты в список словарей
result = []
for row in rows:
result.append({
"id": row[0],
"rust_id": row[1],
"computer_name": row[2],
"install_time": row[3]
})
return result
# Маршрут для добавления новой записи
@app.post("/api/install")
def add_install(data: InstallData):
cursor.execute("INSERT INTO installs (rust_id, computer_name, install_time) VALUES (?, ?, ?)",
(data.rust_id, data.computer_name, data.install_time))
conn.commit()
return {"status": "success"}
# Добавляем маршрут для удаления записи
@app.delete("/api/delete/{install_id}")
async def delete_install(install_id: int):
try:
cursor.execute("DELETE FROM installs WHERE id = ?", (install_id,))
conn.commit()
if cursor.rowcount == 0:
raise HTTPException(status_code=404, detail="Запись не найдена")
return {"status": "success"}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
from fastapi.middleware.cors import CORSMiddleware
app.add_middleware(
CORSMiddleware,
allow_origins=["http://10.0.0.10:8080", "http://localhost:8080"], # Добавляем localhost
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)