feat: migrate user management from json to mongodb

This commit is contained in:
Whispering Wind
2025-09-06 22:38:32 +03:30
committed by GitHub
parent 15eac57988
commit 80c21ccd07
17 changed files with 824 additions and 994 deletions

View File

@ -0,0 +1,41 @@
import pymongo
from bson.objectid import ObjectId
class Database:
def __init__(self, db_name="blitz_panel", collection_name="users"):
try:
self.client = pymongo.MongoClient("mongodb://localhost:27017/")
self.db = self.client[db_name]
self.collection = self.db[collection_name]
self.client.server_info()
except pymongo.errors.ConnectionFailure as e:
print(f"Could not connect to MongoDB: {e}")
raise
def add_user(self, user_data):
username = user_data.pop('username', None)
if not username:
raise ValueError("Username is required")
if self.collection.find_one({"_id": username.lower()}):
return None
user_data['_id'] = username.lower()
return self.collection.insert_one(user_data)
def get_user(self, username):
return self.collection.find_one({"_id": username.lower()})
def get_all_users(self):
return list(self.collection.find({}))
def update_user(self, username, updates):
return self.collection.update_one({"_id": username.lower()}, {"$set": updates})
def delete_user(self, username):
return self.collection.delete_one({"_id": username.lower()})
try:
db = Database()
except pymongo.errors.ConnectionFailure:
db = None

View File

@ -0,0 +1,54 @@
#!/bin/bash
set -e
echo "Updating package list..."
sudo apt-get update -y
if ! command -v mongod &> /dev/null
then
echo "MongoDB not found. Installing from official repository..."
echo "Installing prerequisites..."
sudo apt-get install -y gnupg curl
echo "Importing the MongoDB public GPG key..."
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor
echo "Creating a list file for MongoDB..."
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/debian bookworm/mongodb-org/7.0 main" | \
sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
echo "Reloading local package database..."
sudo apt-get update -y
echo "Installing the MongoDB packages..."
sudo apt-get install -y mongodb-org
else
echo "MongoDB is already installed."
fi
echo "Starting and enabling MongoDB service..."
sudo systemctl start mongod
sudo systemctl enable mongod
echo "Installing pymongo for Python..."
if python3 -m pip --version &> /dev/null; then
python3 -m pip install pymongo
elif pip --version &> /dev/null; then
pip install pymongo
else
echo "pip is not installed. Please install python3-pip"
exit 1
fi
# echo "Installing MongoDB driver for Go..."
# if command -v go &> /dev/null
# then
# go get go.mongodb.org/mongo-driver/mongo
# else
# echo "Go is not installed. Skipping Go driver installation."
# fi
echo "Database setup is complete."