Charan5775 commited on
Commit
d30a516
·
verified ·
1 Parent(s): 7519ed7

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +27 -7
main.py CHANGED
@@ -9,11 +9,11 @@ from collections import defaultdict
9
  import secrets
10
  from config import DOCS_USERNAME, DOCS_PASSWORD
11
 
12
- # Initialize security
13
  security = HTTPBasic()
14
 
15
  def verify_credentials(credentials: HTTPBasicCredentials = Depends(security)):
16
- """Verify HTTP Basic Auth credentials"""
17
  is_username_correct = secrets.compare_digest(credentials.username, DOCS_USERNAME)
18
  is_password_correct = secrets.compare_digest(credentials.password, DOCS_PASSWORD)
19
 
@@ -25,17 +25,37 @@ def verify_credentials(credentials: HTTPBasicCredentials = Depends(security)):
25
  )
26
  return credentials
27
 
28
- # Initialize FastAPI with auth for docs
29
  app = FastAPI(
30
  title="File Sharing API",
31
  description="API for file sharing service",
32
  version="1.0.0",
33
- docs_url="/docs",
34
- redoc_url="/redoc",
35
- dependencies=[Depends(verify_credentials)],
36
- openapi_url="/openapi.json"
37
  )
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  # Create uploads directory if it doesn't exist
40
  UPLOAD_DIR = "uploads"
41
  if not os.path.exists(UPLOAD_DIR):
 
9
  import secrets
10
  from config import DOCS_USERNAME, DOCS_PASSWORD
11
 
12
+ # Initialize security for docs only
13
  security = HTTPBasic()
14
 
15
  def verify_credentials(credentials: HTTPBasicCredentials = Depends(security)):
16
+ """Verify HTTP Basic Auth credentials for docs only"""
17
  is_username_correct = secrets.compare_digest(credentials.username, DOCS_USERNAME)
18
  is_password_correct = secrets.compare_digest(credentials.password, DOCS_PASSWORD)
19
 
 
25
  )
26
  return credentials
27
 
28
+ # Initialize FastAPI with auth for docs only
29
  app = FastAPI(
30
  title="File Sharing API",
31
  description="API for file sharing service",
32
  version="1.0.0",
33
+ docs_url=None, # Disable default docs
34
+ redoc_url=None # Disable default redoc
 
 
35
  )
36
 
37
+ # Create protected docs routes
38
+ @app.get("/docs", include_in_schema=False)
39
+ async def get_documentation(credentials: HTTPBasicCredentials = Depends(verify_credentials)):
40
+ from fastapi.openapi.docs import get_swagger_ui_html
41
+ return get_swagger_ui_html(openapi_url="/openapi.json", title="API Documentation")
42
+
43
+ @app.get("/redoc", include_in_schema=False)
44
+ async def get_redoc(credentials: HTTPBasicCredentials = Depends(verify_credentials)):
45
+ from fastapi.openapi.docs import get_redoc_html
46
+ return get_redoc_html(openapi_url="/openapi.json", title="API Documentation")
47
+
48
+ @app.get("/openapi.json", include_in_schema=False)
49
+ async def get_openapi(credentials: HTTPBasicCredentials = Depends(verify_credentials)):
50
+ from fastapi.openapi.utils import get_openapi
51
+ openapi_schema = get_openapi(
52
+ title="File Sharing API",
53
+ version="1.0.0",
54
+ description="API for file sharing service",
55
+ routes=app.routes
56
+ )
57
+ return openapi_schema
58
+
59
  # Create uploads directory if it doesn't exist
60
  UPLOAD_DIR = "uploads"
61
  if not os.path.exists(UPLOAD_DIR):