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

47 lines
1.2 KiB
Python
Raw Normal View History

from fastapi import APIRouter, Depends, status
from fastapi.responses import JSONResponse
2022-03-15 17:38:24 +00:00
from ..config import CRYPT_CONTEXT, DB
from ..db import Certificate, DistinguishedName, User, UserCapability
2022-03-15 17:38:24 +00:00
router = APIRouter(prefix="/install")
async def is_installed():
return DB.table_exists(User)
@router.get("/check_installed", responses={
status.HTTP_200_OK: {
"model": bool,
},
})
2022-03-15 17:38:24 +00:00
async def check_installed(is_installed: bool = Depends(is_installed)):
return is_installed
@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,
}
})
2022-03-15 17:38:24 +00:00
async def create_db(is_installed: bool = Depends(is_installed)):
if is_installed:
return JSONResponse(
status_code=status.HTTP_400_BAD_REQUEST,
)
2022-03-15 17:38:24 +00:00
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()