Добавление API для получения всех пользователей, доработка call2api

This commit is contained in:
2026-06-11 13:18:22 +03:00
parent e49f3c4f3c
commit fe3987d2ad
2 changed files with 105 additions and 3 deletions
+54
View File
@@ -1,3 +1,4 @@
import json
import mimetypes
import os
import sqlite3
@@ -34,12 +35,27 @@ class FileServerHandler(BaseHTTPRequestHandler):
def log_message(self, format, *args):
log(format % args)
def parse_params(self):
"""Parse query parameters from the URL"""
parsed_url = urlparse(self.path)
self.params = parse_qs(parsed_url.query)
# Convert lists to single values
for key, value in self.params.items():
if isinstance(value, list) and len(value) == 1:
self.params[key] = value[0]
def send_html(self, content, status=200):
self.send_response(status)
self.send_header("Content-type", "text/html; charset=utf-8")
self.end_headers()
self.wfile.write(content.encode("utf-8"))
def send_json(self, data, status=200):
self.send_response(status)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(json.dumps(data).encode("utf-8"))
def send_redirect(self, location, status=302):
self.send_response(status)
self.send_header("Location", location)
@@ -600,6 +616,42 @@ class FileServerHandler(BaseHTTPRequestHandler):
database.DB_CONN.commit()
self.send_html("<h1>Account deleted</h1>")
def handle_api_get_all_users(self):
if self.command != "GET":
self.send_html("<h1>Method not allowed</h1>", 405)
return
# Parse query parameters
self.parse_params()
# Check for API token in query parameters or headers
token = None
if "token" in self.params:
token = self.params["token"]
else:
token = self.headers.get("Authorization", "").replace("Bearer ", "")
if token != CONFIG["security"]["api_token"]:
self.send_html("<h1>Invalid token</h1>", 403)
return
cursor = database.DB_CONN.cursor()
cursor.execute("SELECT id, username, quota_mb, used_mb FROM users")
rows = cursor.fetchall()
users = []
for row in rows:
users.append(
{
"id": row[0],
"username": row[1],
"quota_mb": row[2],
"used_mb": row[3],
}
)
self.send_json(users)
def handle_static_file(self):
if self.path.startswith("/static/"):
file_path = self.path[1:] # Remove leading slash
@@ -666,6 +718,8 @@ class FileServerHandler(BaseHTTPRequestHandler):
self.handle_static_file()
elif path == "/favicon.ico":
self.handle_favicon()
elif path == "/users":
self.handle_api_get_all_users()
else:
self.send_html("<h1>Not Found</h1>", 404)