Spaces:
Running
Running
Upload 11 files
Browse files
main.py
CHANGED
@@ -2,6 +2,7 @@ import os
|
|
2 |
import sys
|
3 |
import logging
|
4 |
import requests
|
|
|
5 |
from fastapi import FastAPI, HTTPException, Depends, Request
|
6 |
from fastapi.middleware.cors import CORSMiddleware
|
7 |
from fastapi.openapi.docs import get_swagger_ui_html, get_redoc_html
|
@@ -9,6 +10,8 @@ from fastapi.openapi.utils import get_openapi
|
|
9 |
from fastapi.staticfiles import StaticFiles
|
10 |
import uvicorn
|
11 |
from dotenv import load_dotenv
|
|
|
|
|
12 |
|
13 |
# Configure logging
|
14 |
logging.basicConfig(
|
@@ -163,6 +166,96 @@ async def startup_db_client():
|
|
163 |
else:
|
164 |
logger.info("Auth token has the expected JWT format")
|
165 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
166 |
# Initialize database connection
|
167 |
connected = False
|
168 |
|
@@ -566,6 +659,9 @@ async def startup_db_client():
|
|
566 |
logger.info("Journals table created successfully")
|
567 |
|
568 |
logger.info("All database tables created successfully")
|
|
|
|
|
|
|
569 |
except Exception as e:
|
570 |
error_msg = f"Failed to create database tables: {str(e)}"
|
571 |
logger.error(error_msg)
|
@@ -886,10 +982,6 @@ async def direct_register(request: Request):
|
|
886 |
Register a new user using direct HTTP API.
|
887 |
This is a workaround for issues with the libsql-experimental driver.
|
888 |
"""
|
889 |
-
import time
|
890 |
-
import json
|
891 |
-
from pydantic import BaseModel, EmailStr
|
892 |
-
|
893 |
class UserCreate(BaseModel):
|
894 |
email: EmailStr
|
895 |
password: str
|
@@ -976,7 +1068,6 @@ async def direct_register(request: Request):
|
|
976 |
|
977 |
# Hash the password
|
978 |
logger.info(f"[{request_id}] Hashing password")
|
979 |
-
from passlib.context import CryptContext
|
980 |
pwd_context = CryptContext(schemes=["argon2"], deprecated="auto")
|
981 |
hashed_password = pwd_context.hash(user.password)
|
982 |
logger.info(f"[{request_id}] Password hashed successfully")
|
|
|
2 |
import sys
|
3 |
import logging
|
4 |
import requests
|
5 |
+
import time
|
6 |
from fastapi import FastAPI, HTTPException, Depends, Request
|
7 |
from fastapi.middleware.cors import CORSMiddleware
|
8 |
from fastapi.openapi.docs import get_swagger_ui_html, get_redoc_html
|
|
|
10 |
from fastapi.staticfiles import StaticFiles
|
11 |
import uvicorn
|
12 |
from dotenv import load_dotenv
|
13 |
+
from passlib.context import CryptContext
|
14 |
+
from pydantic import BaseModel, EmailStr
|
15 |
|
16 |
# Configure logging
|
17 |
logging.basicConfig(
|
|
|
166 |
else:
|
167 |
logger.info("Auth token has the expected JWT format")
|
168 |
|
169 |
+
# Function to create a super user if it doesn't exist
|
170 |
+
def create_super_user():
|
171 |
+
"""
|
172 |
+
Create a super user with admin privileges if it doesn't exist.
|
173 |
+
Uses the email [email protected] as specified.
|
174 |
+
"""
|
175 |
+
# Generate a unique identifier for this operation
|
176 |
+
operation_id = f"create_super_user_{int(time.time())}"
|
177 |
+
logger.info(f"[{operation_id}] Starting super user creation check")
|
178 |
+
|
179 |
+
# Password hashing context
|
180 |
+
pwd_context = CryptContext(
|
181 |
+
schemes=["argon2"],
|
182 |
+
argon2__time_cost=4,
|
183 |
+
argon2__memory_cost=102400,
|
184 |
+
argon2__parallelism=8,
|
185 |
+
argon2__salt_len=16
|
186 |
+
)
|
187 |
+
|
188 |
+
# Super user details
|
189 |
+
super_user_email = "[email protected]"
|
190 |
+
super_user_password = "TestPassword123!" # This is just a default password
|
191 |
+
|
192 |
+
try:
|
193 |
+
# Check if super user already exists
|
194 |
+
logger.info(f"[{operation_id}] Checking if super user already exists")
|
195 |
+
query = "SELECT id, is_admin FROM users WHERE email = ?"
|
196 |
+
existing_user = app.db_conn.execute(query, (super_user_email,)).fetchone()
|
197 |
+
|
198 |
+
if existing_user:
|
199 |
+
user_id = existing_user[0]
|
200 |
+
is_admin = existing_user[1] if len(existing_user) > 1 else 0
|
201 |
+
|
202 |
+
logger.info(f"[{operation_id}] Super user already exists with ID: {user_id}, is_admin: {is_admin}")
|
203 |
+
|
204 |
+
# If user exists but is not admin, make them admin
|
205 |
+
if not is_admin:
|
206 |
+
logger.info(f"[{operation_id}] Updating user to have admin privileges")
|
207 |
+
update_query = "UPDATE users SET is_admin = 1 WHERE id = ?"
|
208 |
+
app.db_conn.execute(update_query, (user_id,))
|
209 |
+
app.db_conn.commit()
|
210 |
+
logger.info(f"[{operation_id}] User updated to admin successfully")
|
211 |
+
|
212 |
+
return
|
213 |
+
|
214 |
+
# User doesn't exist, create it
|
215 |
+
logger.info(f"[{operation_id}] Super user does not exist, creating now")
|
216 |
+
|
217 |
+
# Hash the password
|
218 |
+
hashed_password = pwd_context.hash(super_user_password)
|
219 |
+
logger.info(f"[{operation_id}] Password hashed successfully")
|
220 |
+
|
221 |
+
# Insert the super user with admin privileges
|
222 |
+
insert_query = "INSERT INTO users (email, hashed_password, is_admin) VALUES (?, ?, 1)"
|
223 |
+
cursor = app.db_conn.execute(insert_query, (super_user_email, hashed_password))
|
224 |
+
app.db_conn.commit()
|
225 |
+
|
226 |
+
# Try to get the user ID
|
227 |
+
user_id = None
|
228 |
+
try:
|
229 |
+
user_id = cursor.lastrowid
|
230 |
+
logger.info(f"[{operation_id}] Got lastrowid: {user_id}")
|
231 |
+
except Exception as e:
|
232 |
+
logger.warning(f"[{operation_id}] Could not get lastrowid: {str(e)}")
|
233 |
+
|
234 |
+
# If lastrowid is not available, try to get the ID using a query
|
235 |
+
try:
|
236 |
+
id_query = "SELECT id FROM users WHERE email = ?"
|
237 |
+
id_result = app.db_conn.execute(id_query, (super_user_email,)).fetchone()
|
238 |
+
if id_result:
|
239 |
+
user_id = id_result[0]
|
240 |
+
logger.info(f"[{operation_id}] Got ID from query: {user_id}")
|
241 |
+
except Exception as e2:
|
242 |
+
logger.error(f"[{operation_id}] Error getting ID from query: {str(e2)}")
|
243 |
+
|
244 |
+
# Verify the super user was created
|
245 |
+
verify_query = "SELECT id, email, is_admin FROM users WHERE email = ?"
|
246 |
+
verify_result = app.db_conn.execute(verify_query, (super_user_email,)).fetchone()
|
247 |
+
|
248 |
+
if verify_result:
|
249 |
+
logger.info(f"[{operation_id}] Super user created successfully with ID: {verify_result[0]}")
|
250 |
+
else:
|
251 |
+
logger.error(f"[{operation_id}] Failed to verify super user after creation")
|
252 |
+
|
253 |
+
except Exception as e:
|
254 |
+
logger.error(f"[{operation_id}] Error creating super user: {str(e)}")
|
255 |
+
# We'll continue even if super user creation fails
|
256 |
+
# This allows the server to start and function without a super user
|
257 |
+
logger.warning(f"[{operation_id}] Continuing server startup despite super user creation error")
|
258 |
+
|
259 |
# Initialize database connection
|
260 |
connected = False
|
261 |
|
|
|
659 |
logger.info("Journals table created successfully")
|
660 |
|
661 |
logger.info("All database tables created successfully")
|
662 |
+
|
663 |
+
# Create super user if it doesn't exist
|
664 |
+
create_super_user()
|
665 |
except Exception as e:
|
666 |
error_msg = f"Failed to create database tables: {str(e)}"
|
667 |
logger.error(error_msg)
|
|
|
982 |
Register a new user using direct HTTP API.
|
983 |
This is a workaround for issues with the libsql-experimental driver.
|
984 |
"""
|
|
|
|
|
|
|
|
|
985 |
class UserCreate(BaseModel):
|
986 |
email: EmailStr
|
987 |
password: str
|
|
|
1068 |
|
1069 |
# Hash the password
|
1070 |
logger.info(f"[{request_id}] Hashing password")
|
|
|
1071 |
pwd_context = CryptContext(schemes=["argon2"], deprecated="auto")
|
1072 |
hashed_password = pwd_context.hash(user.password)
|
1073 |
logger.info(f"[{request_id}] Password hashed successfully")
|