Vladyslav Nalyvaiko commited on
Commit
d0fe271
·
1 Parent(s): 7b36638

Add API key

Browse files
Files changed (1) hide show
  1. app.py +13 -2
app.py CHANGED
@@ -3,7 +3,7 @@ import os
3
  import shutil
4
  import logging
5
  import uvicorn
6
- from fastapi import FastAPI, File, UploadFile
7
  from fastapi.middleware.cors import CORSMiddleware
8
 
9
  # This ensures our models are downloaded and config is set before anything
@@ -36,7 +36,18 @@ async def root():
36
  return {"status": "ok", "message": "API is running"}
37
 
38
  @app.post("/process")
39
- async def process_pdf(file: UploadFile = File(...)):
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  file_path = os.path.join(INBOX_DIR, file.filename)
42
  with open(file_path, "wb") as out_file:
 
3
  import shutil
4
  import logging
5
  import uvicorn
6
+ from fastapi import FastAPI, File, UploadFile, Header, HTTPException
7
  from fastapi.middleware.cors import CORSMiddleware
8
 
9
  # This ensures our models are downloaded and config is set before anything
 
36
  return {"status": "ok", "message": "API is running"}
37
 
38
  @app.post("/process")
39
+ async def process_pdf(
40
+ file: UploadFile = File(...),
41
+ x_api_key: str = Header(None, alias="X-API-Key")
42
+ ):
43
+ # Get the secret key from environment variable
44
+ api_key = os.getenv("SECRET_KEY")
45
+
46
+ if not x_api_key:
47
+ raise HTTPException(status_code=401, detail="API key is missing")
48
+
49
+ if x_api_key != api_key:
50
+ raise HTTPException(status_code=401, detail="Invalid API key")
51
 
52
  file_path = os.path.join(INBOX_DIR, file.filename)
53
  with open(file_path, "wb") as out_file: