ChiloAi commited on
Commit
7cad798
·
verified ·
1 Parent(s): f748d8f

Upload main.py

Browse files
Files changed (1) hide show
  1. main.py +55 -0
main.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException, Depends
2
+ from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
3
+ import uuid
4
+ import os
5
+ import sqlite3
6
+
7
+ app = FastAPI()
8
+
9
+ PROMPT_SECRET = os.getenv("CHILO_JSON")
10
+
11
+ DB_PATH = "/tmp/api_keys.db" # chemin garanti accessible en écriture
12
+
13
+ # Initialise directement la base de données
14
+ conn = sqlite3.connect(DB_PATH)
15
+ cursor = conn.cursor()
16
+ cursor.execute("""
17
+ CREATE TABLE IF NOT EXISTS api_keys (
18
+ key TEXT PRIMARY KEY
19
+ )
20
+ """)
21
+ conn.commit()
22
+ conn.close()
23
+
24
+ token_auth_scheme = HTTPBearer()
25
+
26
+ def verify_api_key(credentials: HTTPAuthorizationCredentials = Depends(token_auth_scheme)):
27
+ token = credentials.credentials
28
+ conn = sqlite3.connect(DB_PATH)
29
+ cursor = conn.cursor()
30
+ cursor.execute("SELECT 1 FROM api_keys WHERE key = ?", (token,))
31
+ result = cursor.fetchone()
32
+ conn.close()
33
+ if result is None:
34
+ raise HTTPException(status_code=401, detail="Clé API invalide")
35
+ return token
36
+
37
+ @app.post("/signup")
38
+ def signup():
39
+ new_key = str(uuid.uuid4())
40
+ conn = sqlite3.connect(DB_PATH)
41
+ cursor = conn.cursor()
42
+ try:
43
+ cursor.execute("INSERT OR IGNORE INTO api_keys (key) VALUES (?)", (new_key,))
44
+ conn.commit()
45
+ finally:
46
+ conn.close()
47
+ return {"api_key": new_key}
48
+
49
+ @app.post("/instructions")
50
+ def process_instructions(api_key: str = Depends(verify_api_key)):
51
+ return {"message": "Instructions traitées"}
52
+
53
+ @app.get("/")
54
+ def read_root():
55
+ return {"message": "API is running"}