move create_db to "install" router

This commit is contained in:
Jörn-Michael Miehe 2022-03-15 17:38:24 +00:00
parent 411c584480
commit a35aaa388c
5 changed files with 41 additions and 19 deletions

2
api/.gitignore vendored
View file

@ -150,3 +150,5 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear # and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder. # option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/ #.idea/
tmp/

View file

@ -1,7 +1,7 @@
from peewee import SqliteDatabase from peewee import SqliteDatabase
from passlib.context import CryptContext from passlib.context import CryptContext
DB = SqliteDatabase("vpn.db") DB = SqliteDatabase("tmp/vpn.db")
PRODUCTION_MODE = False PRODUCTION_MODE = False
# to get a string like this run: # to get a string like this run:

View file

@ -1,18 +0,0 @@
#!/usr/bin/env python3
from ..config import CRYPT_CONTEXT, DB
from .model import Certificate, DistinguishedName, User, UserCapability
def main():
DB.connect()
DB.create_tables([Certificate, DistinguishedName, User, UserCapability])
admin = User.create(name="admin", password=CRYPT_CONTEXT.hash("secret"))
UserCapability.create(user=admin, capability="admin")
DB.close()
if __name__ == "__main__":
main()

View file

@ -3,6 +3,8 @@
import uvicorn import uvicorn
from fastapi import FastAPI from fastapi import FastAPI
from kiwi_vpn_api.routers import install
from .config import PRODUCTION_MODE from .config import PRODUCTION_MODE
from .routers import auth, user from .routers import auth, user
@ -21,6 +23,7 @@ api = FastAPI(
redoc_url=None, redoc_url=None,
) )
api.include_router(install.router)
api.include_router(auth.router) api.include_router(auth.router)
api.include_router(user.router) api.include_router(user.router)

View file

@ -0,0 +1,35 @@
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()