Spaces:
Running
Running
Upload 10 files
Browse files
main.py
CHANGED
@@ -18,9 +18,19 @@ app = FastAPI(
|
|
18 |
# Configure CORS
|
19 |
origins = [
|
20 |
os.getenv("FRONTEND_URL", "http://localhost:3000"),
|
21 |
-
"https://
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
]
|
23 |
|
|
|
|
|
24 |
app.add_middleware(
|
25 |
CORSMiddleware,
|
26 |
allow_origins=origins,
|
@@ -32,56 +42,77 @@ app.add_middleware(
|
|
32 |
# Database connection
|
33 |
@app.on_event("startup")
|
34 |
async def startup_db_client():
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
@app.on_event("shutdown")
|
83 |
async def shutdown_db_client():
|
84 |
-
|
|
|
|
|
|
|
|
|
85 |
|
86 |
# Root endpoint
|
87 |
@app.get("/")
|
|
|
18 |
# Configure CORS
|
19 |
origins = [
|
20 |
os.getenv("FRONTEND_URL", "http://localhost:3000"),
|
21 |
+
"https://seamo.earth",
|
22 |
+
"https://seamoo.netlify.app",
|
23 |
+
"https://seamo-ai-ai-server.hf.space",
|
24 |
+
"https://seamo-ai-auth-server.hf.space",
|
25 |
+
"https://seamo-ai-scraper-server.hf.space",
|
26 |
+
"http://localhost:3000", # For local development
|
27 |
+
"http://localhost:8000", # Local AI server
|
28 |
+
"http://localhost:8001", # Local Auth server
|
29 |
+
"http://localhost:8002", # Local Scraper server
|
30 |
]
|
31 |
|
32 |
+
print(f"CORS origins configured: {origins}")
|
33 |
+
|
34 |
app.add_middleware(
|
35 |
CORSMiddleware,
|
36 |
allow_origins=origins,
|
|
|
42 |
# Database connection
|
43 |
@app.on_event("startup")
|
44 |
async def startup_db_client():
|
45 |
+
# Get environment variables
|
46 |
+
db_url = os.getenv("TURSO_DATABASE_URL")
|
47 |
+
auth_token = os.getenv("TURSO_AUTH_TOKEN")
|
48 |
+
|
49 |
+
if not db_url or not auth_token:
|
50 |
+
error_msg = "Missing Turso credentials. TURSO_DATABASE_URL and TURSO_AUTH_TOKEN must be set."
|
51 |
+
print(error_msg)
|
52 |
+
raise Exception(error_msg)
|
53 |
+
|
54 |
+
try:
|
55 |
+
# Use the direct URL connection method (recommended for Hugging Face)
|
56 |
+
print(f"Connecting to Turso database at: {db_url}")
|
57 |
+
app.db_conn = libsql.connect(db_url, auth_token=auth_token.strip())
|
58 |
+
|
59 |
+
# Test connection
|
60 |
+
result = app.db_conn.execute("SELECT 1").fetchone()
|
61 |
+
print(f"Connection test successful: {result}")
|
62 |
+
|
63 |
+
# Create tables if they don't exist
|
64 |
+
app.db_conn.execute("""
|
65 |
+
CREATE TABLE IF NOT EXISTS users (
|
66 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
67 |
+
email TEXT NOT NULL UNIQUE CHECK(LENGTH(email) <= 320),
|
68 |
+
hashed_password TEXT NOT NULL CHECK(LENGTH(hashed_password) = 97),
|
69 |
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
70 |
+
last_login DATETIME,
|
71 |
+
is_admin BOOLEAN DEFAULT FALSE
|
72 |
+
)
|
73 |
+
""")
|
74 |
+
|
75 |
+
app.db_conn.execute("""
|
76 |
+
CREATE TABLE IF NOT EXISTS projects (
|
77 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
78 |
+
owner_id INTEGER NOT NULL,
|
79 |
+
title TEXT NOT NULL CHECK(LENGTH(title) BETWEEN 3 AND 120),
|
80 |
+
description TEXT CHECK(LENGTH(description) <= 2000),
|
81 |
+
geojson TEXT,
|
82 |
+
storage_bucket TEXT DEFAULT 'default',
|
83 |
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
84 |
+
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
85 |
+
FOREIGN KEY (owner_id) REFERENCES users(id)
|
86 |
+
)
|
87 |
+
""")
|
88 |
+
|
89 |
+
app.db_conn.execute("""
|
90 |
+
CREATE TABLE IF NOT EXISTS journals (
|
91 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
92 |
+
project_id INTEGER NOT NULL,
|
93 |
+
title TEXT NOT NULL CHECK(LENGTH(title) BETWEEN 3 AND 120),
|
94 |
+
content TEXT CHECK(LENGTH(content) <= 50000),
|
95 |
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
96 |
+
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
97 |
+
FOREIGN KEY (project_id) REFERENCES projects(id)
|
98 |
+
)
|
99 |
+
""")
|
100 |
+
|
101 |
+
app.db_conn.commit()
|
102 |
+
print("Database tables created successfully")
|
103 |
+
|
104 |
+
except Exception as e:
|
105 |
+
error_msg = f"Failed to connect to Turso database: {str(e)}"
|
106 |
+
print(error_msg)
|
107 |
+
raise Exception(error_msg)
|
108 |
|
109 |
@app.on_event("shutdown")
|
110 |
async def shutdown_db_client():
|
111 |
+
try:
|
112 |
+
app.db_conn.close()
|
113 |
+
print("Database connection closed successfully")
|
114 |
+
except Exception as e:
|
115 |
+
print(f"Error closing database connection: {str(e)}")
|
116 |
|
117 |
# Root endpoint
|
118 |
@app.get("/")
|