collapse "db" subpkg, clean install router

This commit is contained in:
Jörn-Michael Miehe 2022-03-15 22:42:29 +00:00
parent e76de17ff5
commit 112d86d827
6 changed files with 27 additions and 16 deletions

View file

@ -6,7 +6,7 @@ from typing import Optional
from peewee import (BooleanField, CharField, DateTimeField, ForeignKeyField, from peewee import (BooleanField, CharField, DateTimeField, ForeignKeyField,
Model) Model)
from ..config import CRYPT_CONTEXT, DB from .config import CRYPT_CONTEXT, DB
class BaseModel(Model): class BaseModel(Model):

View file

@ -20,7 +20,7 @@ api = FastAPI(
"url": "https://opensource.org/licenses/mit-license.php", "url": "https://opensource.org/licenses/mit-license.php",
}, },
docs_url="/docs" if not PRODUCTION_MODE else None, 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) api.include_router(install.router)

View file

@ -7,7 +7,7 @@ from pydantic import BaseModel
from ..config import (ACCESS_TOKEN_EXPIRE_MINUTES, ALGORITHM, CRYPT_CONTEXT, from ..config import (ACCESS_TOKEN_EXPIRE_MINUTES, ALGORITHM, CRYPT_CONTEXT,
SECRET_KEY) SECRET_KEY)
from ..db.model import User from ..db import User
router = APIRouter(prefix="/auth") router = APIRouter(prefix="/auth")
SCHEME = OAuth2PasswordBearer( SCHEME = OAuth2PasswordBearer(

View file

@ -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 ..config import CRYPT_CONTEXT, DB
from ..db.model import Certificate, DistinguishedName, User, UserCapability from ..db import Certificate, DistinguishedName, User, UserCapability
router = APIRouter(prefix="/install") router = APIRouter(prefix="/install")
@ -10,20 +11,30 @@ async def is_installed():
return DB.table_exists(User) 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)): 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)): 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: if is_installed:
raise credentials_exception return JSONResponse(
status_code=status.HTTP_400_BAD_REQUEST,
)
DB.create_tables([Certificate, DistinguishedName, User, UserCapability]) DB.create_tables([Certificate, DistinguishedName, User, UserCapability])

View file

@ -7,7 +7,7 @@ from jose import JWTError, jwt
from pydantic import BaseModel from pydantic import BaseModel
from ..config import ALGORITHM, SECRET_KEY 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 from .auth import SCHEME
router = APIRouter(prefix="/user") router = APIRouter(prefix="/user")