Compare commits
2 commits
4959f1987c
...
900131fae8
| Author | SHA1 | Date | |
|---|---|---|---|
| 900131fae8 | |||
| d66ce5a026 |
2 changed files with 32 additions and 12 deletions
|
|
@ -32,15 +32,38 @@ class DBType(Enum):
|
||||||
|
|
||||||
|
|
||||||
class DBConfig(BaseModel):
|
class DBConfig(BaseModel):
|
||||||
db_type: DBType = DBType.sqlite
|
type: DBType = DBType.sqlite
|
||||||
|
user: str | None = None
|
||||||
|
password: str | None = None
|
||||||
|
host: str | None = None
|
||||||
|
database: str | None = "./tmp/vpn.db"
|
||||||
|
|
||||||
|
mysql_driver: str = "pymysql"
|
||||||
|
mysql_args: list[str] = ["charset=utf8mb4"]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
async def db_engine(self) -> Engine:
|
async def db_engine(self) -> Engine:
|
||||||
|
if self.type is DBType.sqlite:
|
||||||
|
# SQLite backend
|
||||||
return create_engine(
|
return create_engine(
|
||||||
"sqlite:///./tmp/vpn.db",
|
f"sqlite:///{self.database}",
|
||||||
connect_args={"check_same_thread": False},
|
connect_args={"check_same_thread": False},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
elif self.type is DBType.mysql:
|
||||||
|
# MySQL backend
|
||||||
|
if self.mysql_args:
|
||||||
|
args_str = "?" + "&".join(self.mysql_args)
|
||||||
|
else:
|
||||||
|
args_str = ""
|
||||||
|
|
||||||
|
return create_engine(
|
||||||
|
f"mysql+{self.mysql_driver}://"
|
||||||
|
f"{self.user}:{self.password}@{self.host}"
|
||||||
|
f"/{self.database}{args_str}",
|
||||||
|
pool_recycle=3600,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class JWTConfig(BaseModel):
|
class JWTConfig(BaseModel):
|
||||||
secret: str | None = None
|
secret: str | None = None
|
||||||
|
|
|
||||||
|
|
@ -48,20 +48,17 @@ async def set_config(
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
async def add_user(
|
async def add_user(
|
||||||
user_name: str,
|
user: schemas.UserCreate,
|
||||||
user_password: str,
|
|
||||||
current_config: Config | None = Depends(Config.load),
|
current_config: Config | None = Depends(Config.load),
|
||||||
db: Session | None = Depends(Connection.get),
|
db: Session | None = Depends(Connection.get),
|
||||||
):
|
):
|
||||||
if current_config is None:
|
if current_config is None:
|
||||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
user.capabilities.append("admin")
|
||||||
|
|
||||||
schemas.User.create(
|
schemas.User.create(
|
||||||
db=db,
|
db=db,
|
||||||
user=schemas.UserCreate(
|
user=user,
|
||||||
name=user_name,
|
|
||||||
password=user_password,
|
|
||||||
capabilities=["admin"],
|
|
||||||
),
|
|
||||||
crypt_context=await current_config.crypto.crypt_context,
|
crypt_context=await current_config.crypto.crypt_context,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue