From 112d86d827f5434f90a4f3e8f55983df421eb4ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Tue, 15 Mar 2022 22:42:29 +0000 Subject: [PATCH] collapse "db" subpkg, clean install router --- api/kiwi_vpn_api/{db/model.py => db.py} | 2 +- api/kiwi_vpn_api/db/__init__.py | 0 api/kiwi_vpn_api/main.py | 2 +- api/kiwi_vpn_api/routers/auth.py | 2 +- api/kiwi_vpn_api/routers/install.py | 35 ++++++++++++++++--------- api/kiwi_vpn_api/routers/user.py | 2 +- 6 files changed, 27 insertions(+), 16 deletions(-) rename api/kiwi_vpn_api/{db/model.py => db.py} (97%) delete mode 100644 api/kiwi_vpn_api/db/__init__.py diff --git a/api/kiwi_vpn_api/db/model.py b/api/kiwi_vpn_api/db.py similarity index 97% rename from api/kiwi_vpn_api/db/model.py rename to api/kiwi_vpn_api/db.py index eae5ba1..cb912cd 100644 --- a/api/kiwi_vpn_api/db/model.py +++ b/api/kiwi_vpn_api/db.py @@ -6,7 +6,7 @@ from typing import Optional from peewee import (BooleanField, CharField, DateTimeField, ForeignKeyField, Model) -from ..config import CRYPT_CONTEXT, DB +from .config import CRYPT_CONTEXT, DB class BaseModel(Model): diff --git a/api/kiwi_vpn_api/db/__init__.py b/api/kiwi_vpn_api/db/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/api/kiwi_vpn_api/main.py b/api/kiwi_vpn_api/main.py index e60fe3d..cd481a2 100755 --- a/api/kiwi_vpn_api/main.py +++ b/api/kiwi_vpn_api/main.py @@ -20,7 +20,7 @@ api = FastAPI( "url": "https://opensource.org/licenses/mit-license.php", }, docs_url="/docs" if not PRODUCTION_MODE else None, - redoc_url=None, + redoc_url="/redoc" if not PRODUCTION_MODE else None, ) api.include_router(install.router) diff --git a/api/kiwi_vpn_api/routers/auth.py b/api/kiwi_vpn_api/routers/auth.py index fafa35b..52896c8 100644 --- a/api/kiwi_vpn_api/routers/auth.py +++ b/api/kiwi_vpn_api/routers/auth.py @@ -7,7 +7,7 @@ from pydantic import BaseModel from ..config import (ACCESS_TOKEN_EXPIRE_MINUTES, ALGORITHM, CRYPT_CONTEXT, SECRET_KEY) -from ..db.model import User +from ..db import User router = APIRouter(prefix="/auth") SCHEME = OAuth2PasswordBearer( diff --git a/api/kiwi_vpn_api/routers/install.py b/api/kiwi_vpn_api/routers/install.py index a1c06d3..52a36ba 100644 --- a/api/kiwi_vpn_api/routers/install.py +++ b/api/kiwi_vpn_api/routers/install.py @@ -1,7 +1,8 @@ -from fastapi import APIRouter, Depends, HTTPException, status +from fastapi import APIRouter, Depends, status +from fastapi.responses import JSONResponse -from ..config import DB, CRYPT_CONTEXT -from ..db.model import Certificate, DistinguishedName, User, UserCapability +from ..config import CRYPT_CONTEXT, DB +from ..db import Certificate, DistinguishedName, User, UserCapability router = APIRouter(prefix="/install") @@ -10,20 +11,30 @@ async def is_installed(): return DB.table_exists(User) -@router.get("/check_installed") +@router.get("/check_installed", responses={ + status.HTTP_200_OK: { + "model": bool, + }, +}) async def check_installed(is_installed: bool = Depends(is_installed)): - return {"is_installed": is_installed} + return is_installed -@router.get("/create_db") +@router.get("/create_db", responses={ + status.HTTP_200_OK: { + "description": "Database created", + "content": None, + }, + status.HTTP_400_BAD_REQUEST: { + "description": "Could not create Database", + "content": None, + } +}) async def create_db(is_installed: bool = Depends(is_installed)): - credentials_exception = HTTPException( - status_code=status.HTTP_401_UNAUTHORIZED, - detail="Could not validate credentials", - headers={"WWW-Authenticate": "Bearer"}, - ) if is_installed: - raise credentials_exception + return JSONResponse( + status_code=status.HTTP_400_BAD_REQUEST, + ) DB.create_tables([Certificate, DistinguishedName, User, UserCapability]) diff --git a/api/kiwi_vpn_api/routers/user.py b/api/kiwi_vpn_api/routers/user.py index 8a96b89..9409fa4 100644 --- a/api/kiwi_vpn_api/routers/user.py +++ b/api/kiwi_vpn_api/routers/user.py @@ -7,7 +7,7 @@ from jose import JWTError, jwt from pydantic import BaseModel from ..config import ALGORITHM, SECRET_KEY -from ..db.model import User as db_User +from ..db import User as db_User from .auth import SCHEME router = APIRouter(prefix="/user")