File size: 1,179 Bytes
0ab5549
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from fastapi import FastAPI, File, UploadFile, HTTPException
from fastapi.middleware.cors import CORSMiddleware
import cloudinary
import cloudinary.uploader
import os


    # Cloudinary Configuration
cloudinary.config(
        cloud_name='dys8zymcx',
        api_key='754948374267897',
        api_secret='rgvtpvNivIVWoBB7Od2_lE7VzLI'
)


# FastAPI App
app = FastAPI()

# CORS Middleware (Agar frontend se requests allow karna hai)
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # Saare origins allow karein (production mein specific origins use karein)
    allow_methods=["*"],
    allow_headers=["*"],
)

# Image Upload Endpoint
@app.post("/upload-image/")
async def upload_image(file: UploadFile = File(...)):
    try:
        # File ko Cloudinary par upload karein
        result = cloudinary.uploader.upload(file.file, folder="user_images")
        return {"message": "Image uploaded successfully!", "url": result["secure_url"]}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

# Root Endpoint
@app.get("/")
def read_root():
    return {"message": "FastAPI server is running!"}