aziac commited on
Commit
7017186
·
1 Parent(s): 5d9bc9a

added get captcha endpoint

Browse files
Files changed (1) hide show
  1. app/main.py +31 -8
app/main.py CHANGED
@@ -1,23 +1,46 @@
1
- from fastapi import FastAPI
 
 
 
2
  from fastapi.middleware.cors import CORSMiddleware
3
 
4
  # Initialize the FastAPI app
5
  app = FastAPI()
6
 
7
  # --- CORS Middleware ---
8
- # This is crucial for allowing your frontend to communicate with this backend.
9
- # origins = ["*"] allows all origins, which is fine for this project.
10
- # For a production application, you might want to restrict this to your frontend's domain.
11
  app.add_middleware(
12
  CORSMiddleware,
13
- allow_origins=["*"], # Allows all origins
14
  allow_credentials=True,
15
- allow_methods=["*"], # Allows all methods (GET, POST, etc.)
16
- allow_headers=["*"], # Allows all headers
17
  )
18
 
 
 
 
 
 
19
  # --- API Endpoints ---
20
  @app.get("/")
21
  async def read_root():
22
  """A simple root endpoint to check if the API is running."""
23
- return {"message": "Welcome to the Captcha Solver API!"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import random
3
+ from pathlib import Path
4
+ from fastapi import FastAPI, HTTPException
5
  from fastapi.middleware.cors import CORSMiddleware
6
 
7
  # Initialize the FastAPI app
8
  app = FastAPI()
9
 
10
  # --- CORS Middleware ---
 
 
 
11
  app.add_middleware(
12
  CORSMiddleware,
13
+ allow_origins=["*"],
14
  allow_credentials=True,
15
+ allow_methods=["*"],
16
+ allow_headers=["*"],
17
  )
18
 
19
+ # --- Constants ---
20
+ # Define the path to the directory containing captcha images
21
+ IMAGE_DIR = Path("static/images")
22
+
23
+
24
  # --- API Endpoints ---
25
  @app.get("/")
26
  async def read_root():
27
  """A simple root endpoint to check if the API is running."""
28
+ return {"message": "Welcome to the Captcha Solver API!"}
29
+
30
+
31
+ @app.get("/get_captcha")
32
+ async def get_captcha():
33
+ """
34
+ Returns the filename of a random captcha image from the static/images directory.
35
+ """
36
+ if not IMAGE_DIR.is_dir():
37
+ raise HTTPException(status_code=500, detail="Image directory not found on server.")
38
+
39
+ image_files = [f for f in os.listdir(IMAGE_DIR) if f.endswith(('.png'))]
40
+
41
+ if not image_files:
42
+ raise HTTPException(status_code=404, detail="No captcha images found.")
43
+
44
+ random_image_filename = random.choice(image_files)
45
+
46
+ return {"filename": random_image_filename}