kiwi-vpn/api/kiwi_vpn_api/routers/install.py

35 lines
1 KiB
Python

from fastapi import APIRouter, Depends, HTTPException, status
from ..config import DB, CRYPT_CONTEXT
from ..db.model import Certificate, DistinguishedName, User, UserCapability
router = APIRouter(prefix="/install")
async def is_installed():
return DB.table_exists(User)
@router.get("/check_installed")
async def check_installed(is_installed: bool = Depends(is_installed)):
return {"is_installed": is_installed}
@router.get("/create_db")
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
DB.create_tables([Certificate, DistinguishedName, User, UserCapability])
admin = User.create(name="admin", password=CRYPT_CONTEXT.hash("secret"))
UserCapability.create(user=admin, capability="admin")
User.create(name="johndoe", password=CRYPT_CONTEXT.hash("secret"))
DB.close()