Skip to content

Commit 276a3c4

Browse files
committed
refactor(api_key): use api key on backend to set up user
1 parent 30a2760 commit 276a3c4

File tree

5 files changed

+29
-7
lines changed

5 files changed

+29
-7
lines changed

backend/.env.example

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ USE_OPENAI_EMBEDDINGS=false # optional
55
OPENAI_API_KEY=sk-xxxxxxxxxxxx # optional
66
CHROMA_BATCH_SIZE=5 # optional
77
MAX_FILE_SIZE=20971520 # optional
8+
PANDAETL_API_KEY=xxx-xxx-xxx-xxx # optional if you already have a PandaETL api key

backend/app/config.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
from dotenv import load_dotenv
33
from pydantic_settings import BaseSettings
4+
from typing import Optional
45

56
# Load environment variables from .env file
67
load_dotenv()
@@ -30,6 +31,9 @@ class Settings(BaseSettings):
3031
chat_extraction_doc_threshold: float = 0.5
3132
chat_extraction_max_docs: int = 50
3233

34+
# PandaETL api key
35+
pandaetl_api_key: Optional[str] = None
36+
3337
class Config:
3438
env_file = ".env"
3539

backend/app/main.py

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
from app import models
22
from app.processing.process_queue import submit_process
3-
from app.repositories import process_repository, project_repository
3+
from app.repositories import process_repository, project_repository, user_repository
44
from fastapi import FastAPI
55
from fastapi.staticfiles import StaticFiles
66
from .database import SessionLocal
77
from fastapi.middleware.cors import CORSMiddleware
88
from app.processing.file_preprocessing import process_file
99
from .config import settings
1010
from .api import v1_router
11+
from app.schemas.user import APIKeyRequest
1112

1213
# Initialize the FastAPI app
1314
app = FastAPI()
@@ -57,6 +58,26 @@ def startup_pending_processes():
5758
print(f"Error in startup_pending_processes: {e}")
5859

5960

61+
def setup_user():
62+
try:
63+
with SessionLocal() as db:
64+
65+
if settings.pandaetl_api_key:
66+
user = user_repository.get_users(db, n=1)
67+
api_key = user_repository.get_user_api_key(db)
68+
69+
if not user:
70+
user = user_repository.create_user(db, APIKeyRequest(email="[email protected]"))
71+
72+
if not api_key:
73+
user_repository.add_user_api_key(db, user.id, settings.pandaetl_api_key)
74+
75+
print("Successfully set up user from api key")
76+
77+
except Exception as e:
78+
print(f"Error in setup user from api key: {e}")
79+
80+
6081
app.add_middleware(
6182
CORSMiddleware,
6283
allow_origins=["*"], # Allow all origins (for development)
@@ -69,6 +90,6 @@ def startup_pending_processes():
6990

7091
app.include_router(v1_router, prefix="/v1")
7192

72-
93+
setup_user()
7394
startup_pending_processes()
7495
startup_file_preprocessing()

frontend/.env.example

-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ NEXT_PUBLIC_API_URL=http://localhost:3000/api/v1
22
NEXT_PUBLIC_STORAGE_URL=http://localhost:3000/api/assets
33
NEXT_PUBLIC_MIXPANEL_TOKEN=f2e8a71ab2bde33ebf346c5abf6ba9fa
44
NEXT_PUBLIC_ROLLBAR_ACCESS_TOKEN=0df0bee895044430880278e2b2a5b2d2
5-
# NEXT_PUBLIC_API_KEY=xxx-xxx-xxx-xxx # Uncomment this if you have an API key
65
# NEXT_PUBLIC_BACKEND_URL=http://backend:5328 # Uncomment this if you're working with a docker setup

frontend/src/middleware.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@ import { GetAPIKey } from "./services/user";
88
export async function middleware(request: NextRequest) {
99
let apiKey = null;
1010
try {
11-
const envApiKey = process.env.NEXT_PUBLIC_API_KEY;
1211
const dockerBackendUrl = process.env.NEXT_PUBLIC_BACKEND_URL;
1312

14-
if (envApiKey) {
15-
apiKey = { data: { api_key: envApiKey } };
16-
} else if (dockerBackendUrl) {
13+
if (dockerBackendUrl) {
1714
const response = await axios.get<{ data: APIKeyData }>(
1815
`${dockerBackendUrl}/v1/user/get-api-key`
1916
);

0 commit comments

Comments
 (0)