summaryrefslogtreecommitdiff
path: root/src/main.py
diff options
context:
space:
mode:
authorKristofer Hallin <kristofer@sunet.se>2022-04-12 13:49:05 +0200
committerKristofer Hallin <kristofer@sunet.se>2022-04-12 13:49:05 +0200
commit509bf7fe6a4589d525b21f179ce8cb730c0d4e59 (patch)
tree975714991716efe60ce67e82e143c825a8eb27c0 /src/main.py
parent16f5009ac0d630c5f25c9d6cb4e8fb026ae628f9 (diff)
parent2aebcdeca17f9b46d90f5255dd4d03caa358701e (diff)
Merged main and updated stuff.
Diffstat (limited to 'src/main.py')
-rwxr-xr-xsrc/main.py41
1 files changed, 26 insertions, 15 deletions
diff --git a/src/main.py b/src/main.py
index e6bb8e2..a62d77c 100755
--- a/src/main.py
+++ b/src/main.py
@@ -1,12 +1,10 @@
+import json
import os
import sys
import time
import uvicorn
-
-from fastapi import Depends
-from fastapi import FastAPI
-from fastapi import Request
+from fastapi import Depends, FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from fastapi_jwt_auth import AuthJWT
@@ -14,9 +12,7 @@ from fastapi_jwt_auth.exceptions import AuthJWTException
from pydantic import BaseModel
from db.dictionary import DictDB
-from db.index import CouchIindex
-from db.sql import Log
-from db.sql import Scanner
+from db.schema import get_index_keys
app = FastAPI()
@@ -41,8 +37,8 @@ async def mock_x_total_count_header(request: Request, call_next):
for i in range(10):
try:
db = DictDB()
- except Exception:
- print(f"Database not responding, will try again soon. Attempt {i + 1} of 10.")
+ except Exception as e:
+ print(f"Database not responding, will try again soon: {e}")
else:
break
time.sleep(1)
@@ -73,7 +69,7 @@ def get_data(key=None, limit=25, skip=0, ip=None,
return db.get(key)
selectors = dict()
- indexes = CouchIindex().dict()
+ indexes = get_index_keys()
selectors['domain'] = domain
if ip and 'ip' in indexes:
@@ -169,17 +165,32 @@ async def get_key(key=None, Authorize: AuthJWT = Depends()):
return JSONResponse(content={"status": "success", "docs": data})
-@ app.post('/sc/v0/add')
+@app.post('/sc/v0/add')
async def add(data: Request, Authorize: AuthJWT = Depends()):
-
- # Maybe we should protect this enpoint too and let the scanner use
- # a JWT token as well.
# Authorize.jwt_required()
- json_data = await data.json()
+ try:
+ json_data = await data.json()
+ except json.decoder.JSONDecodeError:
+ return JSONResponse(
+ content={
+ "status": "error",
+ "message": "Invalid JSON.",
+ },
+ status_code=400,
+ )
key = db.add(json_data)
+ if isinstance(key, str):
+ return JSONResponse(
+ content={
+ "status": "error",
+ "message": key,
+ },
+ status_code=400,
+ )
+
return JSONResponse(content={"status": "success", "docs": key})