Spaces:
Runtime error
Runtime error
| #!/usr/bin/env python | |
| """ | |
| Initialize JSON database with admin user | |
| """ | |
| import json | |
| import os | |
| import sys | |
| import bcrypt | |
| from datetime import datetime | |
| # Add project to path | |
| sys.path.insert(0, os.path.dirname(__file__)) | |
| # Database paths | |
| DB_DIR = os.path.join(os.path.dirname(__file__), 'data') | |
| USERS_FILE = os.path.join(DB_DIR, 'users.json') | |
| BLACKLISTED_TOKENS_FILE = os.path.join(DB_DIR, 'blacklisted_tokens.json') | |
| REFRESH_TOKENS_FILE = os.path.join(DB_DIR, 'refresh_tokens.json') | |
| def create_initial_data(): | |
| """Create initial data with admin user""" | |
| # Ensure directory exists | |
| os.makedirs(DB_DIR, exist_ok=True) | |
| print(f"? Directory created/verified: {DB_DIR}") | |
| # Create users.json with admin user | |
| admin_password = "admin@123" | |
| password_hash = bcrypt.hashpw(admin_password.encode('utf-8'), bcrypt.gensalt()) | |
| users_data = { | |
| "admin": { | |
| "id": 1, | |
| "username": "admin", | |
| "password_hash": password_hash.decode('utf-8'), | |
| "role": "admin", | |
| "created_at": datetime.now().isoformat() | |
| } | |
| } | |
| with open(USERS_FILE, 'w', encoding='utf-8') as f: | |
| json.dump(users_data, f, indent=2, ensure_ascii=False) | |
| print(f"? Created {USERS_FILE}") | |
| print(f" - Username: admin") | |
| print(f" - Password: admin@123 (hashed)") | |
| print(f" - Role: admin") | |
| # Create empty blacklisted_tokens.json | |
| with open(BLACKLISTED_TOKENS_FILE, 'w', encoding='utf-8') as f: | |
| json.dump({}, f, indent=2) | |
| print(f"? Created {BLACKLISTED_TOKENS_FILE}") | |
| # Create empty refresh_tokens.json | |
| with open(REFRESH_TOKENS_FILE, 'w', encoding='utf-8') as f: | |
| json.dump({}, f, indent=2) | |
| print(f"? Created {REFRESH_TOKENS_FILE}") | |
| print("\n? Database initialization complete!") | |
| print("\nYou can now use:") | |
| print(" POST /auth/login") | |
| print(" {\"username\": \"admin\", \"password\": \"admin@123\"}") | |
| if __name__ == '__main__': | |
| try: | |
| create_initial_data() | |
| except Exception as e: | |
| print(f"? Error: {e}") | |
| sys.exit(1) | |