kamau1 commited on
Commit
2203b30
·
verified ·
1 Parent(s): 7378c28

Upload 10 files

Browse files
Files changed (1) hide show
  1. main.py +79 -48
main.py CHANGED
@@ -18,9 +18,19 @@ app = FastAPI(
18
  # Configure CORS
19
  origins = [
20
  os.getenv("FRONTEND_URL", "http://localhost:3000"),
21
- "https://app.seamo.earth",
 
 
 
 
 
 
 
 
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
- app.db_conn = libsql.connect(
36
- "auth_server.db",
37
- sync_url=os.getenv("TURSO_DATABASE_URL"),
38
- auth_token=os.getenv("TURSO_AUTH_TOKEN")
39
- )
40
-
41
- # Create tables if they don't exist
42
- app.db_conn.execute("""
43
- CREATE TABLE IF NOT EXISTS users (
44
- id INTEGER PRIMARY KEY AUTOINCREMENT,
45
- email TEXT NOT NULL UNIQUE CHECK(LENGTH(email) <= 320),
46
- hashed_password TEXT NOT NULL CHECK(LENGTH(hashed_password) = 97),
47
- created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
48
- last_login DATETIME,
49
- is_admin BOOLEAN DEFAULT FALSE
50
- )
51
- """)
52
-
53
- app.db_conn.execute("""
54
- CREATE TABLE IF NOT EXISTS projects (
55
- id INTEGER PRIMARY KEY AUTOINCREMENT,
56
- owner_id INTEGER NOT NULL,
57
- title TEXT NOT NULL CHECK(LENGTH(title) BETWEEN 3 AND 120),
58
- description TEXT CHECK(LENGTH(description) <= 2000),
59
- geojson TEXT,
60
- storage_bucket TEXT DEFAULT 'default',
61
- created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
62
- updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
63
- FOREIGN KEY (owner_id) REFERENCES users(id)
64
- )
65
- """)
66
-
67
- app.db_conn.execute("""
68
- CREATE TABLE IF NOT EXISTS journals (
69
- id INTEGER PRIMARY KEY AUTOINCREMENT,
70
- project_id INTEGER NOT NULL,
71
- title TEXT NOT NULL CHECK(LENGTH(title) BETWEEN 3 AND 120),
72
- content TEXT CHECK(LENGTH(content) <= 50000),
73
- created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
74
- updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
75
- FOREIGN KEY (project_id) REFERENCES projects(id)
76
- )
77
- """)
78
-
79
- app.db_conn.commit()
80
- app.db_conn.sync()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
  @app.on_event("shutdown")
83
  async def shutdown_db_client():
84
- app.db_conn.close()
 
 
 
 
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("/")