2022-03-16 00:51:40 +00:00
|
|
|
from secrets import token_hex
|
2022-03-16 00:23:57 +00:00
|
|
|
|
2022-03-16 14:10:49 +00:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
2022-03-17 17:06:00 +00:00
|
|
|
from sqlalchemy.orm import Session
|
2022-03-15 17:38:24 +00:00
|
|
|
|
2022-03-18 17:36:44 +00:00
|
|
|
from ..config import (CONFIG_FILE, BaseConfig, get_db, has_config,
|
2022-03-17 22:47:31 +00:00
|
|
|
load_config)
|
|
|
|
from ..db import crud, schemas
|
2022-03-15 17:38:24 +00:00
|
|
|
|
|
|
|
router = APIRouter(prefix="/install")
|
|
|
|
|
|
|
|
|
2022-03-16 00:23:57 +00:00
|
|
|
@router.get(
|
2022-03-16 00:51:40 +00:00
|
|
|
"/config",
|
2022-03-16 00:23:57 +00:00
|
|
|
response_model=BaseConfig,
|
|
|
|
responses={
|
2022-03-16 13:28:15 +00:00
|
|
|
status.HTTP_403_FORBIDDEN: {
|
|
|
|
"description": "Must be admin",
|
2022-03-16 00:23:57 +00:00
|
|
|
"content": None,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
2022-03-16 00:51:40 +00:00
|
|
|
async def get_config(
|
2022-03-16 13:28:15 +00:00
|
|
|
config: BaseConfig = Depends(load_config),
|
|
|
|
has_config: bool = Depends(has_config),
|
2022-03-16 00:23:57 +00:00
|
|
|
):
|
2022-03-16 13:28:15 +00:00
|
|
|
if has_config:
|
2022-03-16 14:10:49 +00:00
|
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
2022-03-16 00:23:57 +00:00
|
|
|
|
2022-03-16 13:28:15 +00:00
|
|
|
return config
|
2022-03-16 00:23:57 +00:00
|
|
|
|
|
|
|
|
2022-03-16 00:51:40 +00:00
|
|
|
@router.put(
|
|
|
|
"/config",
|
2022-03-16 00:23:57 +00:00
|
|
|
responses={
|
|
|
|
status.HTTP_200_OK: {
|
|
|
|
"content": None,
|
|
|
|
},
|
|
|
|
status.HTTP_403_FORBIDDEN: {
|
|
|
|
"description": "Must be admin",
|
|
|
|
"content": None,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
2022-03-16 00:51:40 +00:00
|
|
|
async def set_config(
|
2022-03-16 00:23:57 +00:00
|
|
|
config: BaseConfig,
|
2022-03-16 13:28:15 +00:00
|
|
|
has_config: bool = Depends(has_config),
|
2022-03-16 00:23:57 +00:00
|
|
|
):
|
2022-03-16 13:28:15 +00:00
|
|
|
if has_config:
|
2022-03-16 14:10:49 +00:00
|
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
2022-03-16 00:23:57 +00:00
|
|
|
|
2022-03-16 00:51:40 +00:00
|
|
|
if config.jwt.secret is None:
|
|
|
|
config.jwt.secret = token_hex(32)
|
|
|
|
|
2022-03-18 17:36:44 +00:00
|
|
|
await config.connect_db()
|
2022-03-16 14:54:42 +00:00
|
|
|
|
2022-03-16 13:28:15 +00:00
|
|
|
with open(CONFIG_FILE, "w") as kv:
|
|
|
|
kv.write(config.json(indent=2))
|
2022-03-16 00:23:57 +00:00
|
|
|
|
|
|
|
|
2022-03-16 14:10:49 +00:00
|
|
|
@router.get("/db", responses={
|
2022-03-15 22:42:29 +00:00
|
|
|
status.HTTP_200_OK: {
|
|
|
|
"model": bool,
|
|
|
|
},
|
|
|
|
})
|
2022-03-17 22:47:31 +00:00
|
|
|
async def check_db():
|
2022-03-17 17:06:00 +00:00
|
|
|
return True
|
2022-03-15 22:42:29 +00:00
|
|
|
|
|
|
|
|
2022-03-16 14:10:49 +00:00
|
|
|
@router.put(
|
|
|
|
"/db",
|
2022-03-16 00:23:57 +00:00
|
|
|
responses={
|
|
|
|
status.HTTP_200_OK: {
|
|
|
|
"content": None,
|
|
|
|
},
|
|
|
|
status.HTTP_400_BAD_REQUEST: {
|
2022-03-16 14:10:49 +00:00
|
|
|
"description": "Database exists",
|
2022-03-16 00:23:57 +00:00
|
|
|
"content": None,
|
|
|
|
},
|
2022-03-15 22:42:29 +00:00
|
|
|
},
|
2022-03-16 00:23:57 +00:00
|
|
|
)
|
2022-03-16 14:10:49 +00:00
|
|
|
async def create_db(
|
|
|
|
admin_name: str,
|
|
|
|
admin_password: str,
|
2022-03-18 17:36:44 +00:00
|
|
|
config: BaseConfig = Depends(load_config),
|
2022-03-17 22:47:31 +00:00
|
|
|
db: Session = Depends(get_db),
|
2022-03-16 14:10:49 +00:00
|
|
|
):
|
2022-03-17 17:06:00 +00:00
|
|
|
# if await has_tables(db):
|
|
|
|
# raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
|
2022-03-15 17:38:24 +00:00
|
|
|
|
2022-03-17 17:06:00 +00:00
|
|
|
# cryptContext = await config.crypto.cryptContext
|
2022-03-17 22:47:31 +00:00
|
|
|
|
2022-03-18 17:36:44 +00:00
|
|
|
if db is None:
|
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
2022-03-17 17:06:00 +00:00
|
|
|
crud.create_user(db, schemas.UserCreate(
|
2022-03-16 14:10:49 +00:00
|
|
|
name=admin_name,
|
2022-03-17 17:06:00 +00:00
|
|
|
password=admin_password,
|
2022-03-18 17:36:44 +00:00
|
|
|
crypt_context=config.crypt_context,
|
2022-03-17 17:06:00 +00:00
|
|
|
))
|
|
|
|
crud.add_user_capability(db, user_name=admin_name, capability="admin")
|