boyinfuture commited on
Commit
82fcdc5
·
1 Parent(s): dc0728a

fixed cors-origin

Browse files
Files changed (1) hide show
  1. backend/main.py +30 -17
backend/main.py CHANGED
@@ -1,14 +1,14 @@
1
  # from fastapi import FastAPI, Depends, HTTPException
2
  # from fastapi.middleware.cors import CORSMiddleware
3
  # from sqlalchemy.orm import Session
 
4
  # from uuid import UUID
 
5
  # import models.analysis_job as model
6
  # import schemas
7
  # from core.database import SessionLocal, engine
8
- # # Import only our single, main task
9
  # from tasks.main_task import run_full_analysis
10
 
11
- # # This ensures tables are created on startup (good for development)
12
  # model.Base.metadata.create_all(bind=engine)
13
 
14
  # app = FastAPI(
@@ -24,7 +24,6 @@
24
  # allow_headers=["*"],
25
  # )
26
 
27
- # # Dependency to get a database session for each API request
28
  # def get_db():
29
  # db = SessionLocal()
30
  # try:
@@ -34,30 +33,37 @@
34
 
35
  # @app.post("/jobs", response_model=schemas.Job, status_code=201)
36
  # def create_analysis_job(job_request: schemas.JobCreate, db: Session = Depends(get_db)):
37
- # """
38
- # Creates a new analysis job and dispatches the main background task.
39
- # """
40
  # db_job = model.AnalysisJob(ticker=job_request.ticker.upper())
41
  # db.add(db_job)
42
  # db.commit()
43
  # db.refresh(db_job)
44
 
45
- # # Dispatch our single, all-in-one analysis task to run in the background
46
  # run_full_analysis.delay(str(db_job.id), db_job.ticker)
47
 
48
- # # Immediately return the created job to the frontend
49
  # return db_job
50
 
51
  # @app.get("/jobs/{job_id}", response_model=schemas.Job)
52
  # def get_job_status(job_id: UUID, db: Session = Depends(get_db)):
53
- # """
54
- # Allows the frontend to poll for the status and result of an analysis job.
55
- # """
56
  # db_job = db.query(model.AnalysisJob).filter(model.AnalysisJob.id == job_id).first()
57
  # if db_job is None:
58
  # raise HTTPException(status_code=404, detail="Job not found")
59
  # return db_job
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
 
63
 
@@ -65,10 +71,10 @@
65
  from fastapi import FastAPI, Depends, HTTPException
66
  from fastapi.middleware.cors import CORSMiddleware
67
  from sqlalchemy.orm import Session
68
- from sqlalchemy import desc # Import desc for ordering
69
  from uuid import UUID
70
- from typing import List # Import List for the history endpoint
71
- import models.analysis_job as model
72
  import schemas
73
  from core.database import SessionLocal, engine
74
  from tasks.main_task import run_full_analysis
@@ -80,13 +86,21 @@ app = FastAPI(
80
  version="0.1.0",
81
  )
82
 
 
 
 
 
 
 
 
83
  app.add_middleware(
84
  CORSMiddleware,
85
- allow_origins=["*"],
86
  allow_credentials=True,
87
- allow_methods=["*"],
88
  allow_headers=["*"],
89
  )
 
90
 
91
  def get_db():
92
  db = SessionLocal()
@@ -113,7 +127,6 @@ def get_job_status(job_id: UUID, db: Session = Depends(get_db)):
113
  raise HTTPException(status_code=404, detail="Job not found")
114
  return db_job
115
 
116
- # --- NEW ENDPOINT FOR HISTORY PANEL ---
117
  @app.get("/jobs", response_model=List[schemas.Job])
118
  def get_jobs_history(db: Session = Depends(get_db)):
119
  db_jobs = db.query(model.AnalysisJob).order_by(desc(model.AnalysisJob.created_at)).limit(20).all()
 
1
  # from fastapi import FastAPI, Depends, HTTPException
2
  # from fastapi.middleware.cors import CORSMiddleware
3
  # from sqlalchemy.orm import Session
4
+ # from sqlalchemy import desc # Import desc for ordering
5
  # from uuid import UUID
6
+ # from typing import List # Import List for the history endpoint
7
  # import models.analysis_job as model
8
  # import schemas
9
  # from core.database import SessionLocal, engine
 
10
  # from tasks.main_task import run_full_analysis
11
 
 
12
  # model.Base.metadata.create_all(bind=engine)
13
 
14
  # app = FastAPI(
 
24
  # allow_headers=["*"],
25
  # )
26
 
 
27
  # def get_db():
28
  # db = SessionLocal()
29
  # try:
 
33
 
34
  # @app.post("/jobs", response_model=schemas.Job, status_code=201)
35
  # def create_analysis_job(job_request: schemas.JobCreate, db: Session = Depends(get_db)):
 
 
 
36
  # db_job = model.AnalysisJob(ticker=job_request.ticker.upper())
37
  # db.add(db_job)
38
  # db.commit()
39
  # db.refresh(db_job)
40
 
 
41
  # run_full_analysis.delay(str(db_job.id), db_job.ticker)
42
 
 
43
  # return db_job
44
 
45
  # @app.get("/jobs/{job_id}", response_model=schemas.Job)
46
  # def get_job_status(job_id: UUID, db: Session = Depends(get_db)):
 
 
 
47
  # db_job = db.query(model.AnalysisJob).filter(model.AnalysisJob.id == job_id).first()
48
  # if db_job is None:
49
  # raise HTTPException(status_code=404, detail="Job not found")
50
  # return db_job
51
 
52
+ # # --- NEW ENDPOINT FOR HISTORY PANEL ---
53
+ # @app.get("/jobs", response_model=List[schemas.Job])
54
+ # def get_jobs_history(db: Session = Depends(get_db)):
55
+ # db_jobs = db.query(model.AnalysisJob).order_by(desc(model.AnalysisJob.created_at)).limit(20).all()
56
+ # return db_jobs
57
+
58
+
59
+
60
+
61
+
62
+
63
+
64
+
65
+
66
+
67
 
68
 
69
 
 
71
  from fastapi import FastAPI, Depends, HTTPException
72
  from fastapi.middleware.cors import CORSMiddleware
73
  from sqlalchemy.orm import Session
74
+ from sqlalchemy import desc
75
  from uuid import UUID
76
+ from typing import List
77
+ import db_models.analysis_job as model # Make sure this is db_models, not models
78
  import schemas
79
  from core.database import SessionLocal, engine
80
  from tasks.main_task import run_full_analysis
 
86
  version="0.1.0",
87
  )
88
 
89
+ # --- THIS IS THE CRITICAL FIX FOR DEPLOYMENT ---
90
+ # Define the specific domains that are allowed to access our API
91
+ allowed_origins = [
92
+ "http://localhost:5173", # For your local development
93
+ "https://quantitative-analysis-platform.vercel.app" # Your live Vercel URL
94
+ ]
95
+
96
  app.add_middleware(
97
  CORSMiddleware,
98
+ allow_origins=allowed_origins,
99
  allow_credentials=True,
100
+ allow_methods=["GET", "POST", "OPTIONS"],
101
  allow_headers=["*"],
102
  )
103
+ # --- END OF FIX ---
104
 
105
  def get_db():
106
  db = SessionLocal()
 
127
  raise HTTPException(status_code=404, detail="Job not found")
128
  return db_job
129
 
 
130
  @app.get("/jobs", response_model=List[schemas.Job])
131
  def get_jobs_history(db: Session = Depends(get_db)):
132
  db_jobs = db.query(model.AnalysisJob).order_by(desc(model.AnalysisJob.created_at)).limit(20).all()