move create_db to "install" router
This commit is contained in:
parent
411c584480
commit
a35aaa388c
5 changed files with 41 additions and 19 deletions
2
api/.gitignore
vendored
2
api/.gitignore
vendored
|
@ -150,3 +150,5 @@ cython_debug/
|
|||
# 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.
|
||||
#.idea/
|
||||
|
||||
tmp/
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
from peewee import SqliteDatabase
|
||||
from passlib.context import CryptContext
|
||||
|
||||
DB = SqliteDatabase("vpn.db")
|
||||
DB = SqliteDatabase("tmp/vpn.db")
|
||||
PRODUCTION_MODE = False
|
||||
|
||||
# to get a string like this run:
|
||||
|
|
|
@ -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()
|
|
@ -3,6 +3,8 @@
|
|||
import uvicorn
|
||||
from fastapi import FastAPI
|
||||
|
||||
from kiwi_vpn_api.routers import install
|
||||
|
||||
from .config import PRODUCTION_MODE
|
||||
from .routers import auth, user
|
||||
|
||||
|
@ -21,6 +23,7 @@ api = FastAPI(
|
|||
redoc_url=None,
|
||||
)
|
||||
|
||||
api.include_router(install.router)
|
||||
api.include_router(auth.router)
|
||||
api.include_router(user.router)
|
||||
|
||||
|
|
35
api/kiwi_vpn_api/routers/install.py
Normal file
35
api/kiwi_vpn_api/routers/install.py
Normal 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()
|
Loading…
Reference in a new issue