add function add startup
This commit is contained in:
parent
6b0a5a7b65
commit
680634aeee
5
app/dependencies/database.py
Normal file
5
app/dependencies/database.py
Normal file
@ -0,0 +1,5 @@
|
||||
from pymongo import MongoClient
|
||||
import os
|
||||
|
||||
client = MongoClient(host=os.environ["MONGO_HOST"], username=os.environ["MONGO_USER"], password=os.environ["MONGO_PASSWORD"])
|
||||
database = client[os.environ["MONGO_DATABASE"]]
|
11
app/dependencies/user_add.py
Normal file
11
app/dependencies/user_add.py
Normal file
@ -0,0 +1,11 @@
|
||||
from ..models import users
|
||||
from ..dependencies import database
|
||||
from passlib.context import CryptContext
|
||||
|
||||
|
||||
def add(username="", password="", roles="User", disabled=False):
|
||||
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||
user = users.User(username=username, password=pwd_context.hash(password), roles=roles, disabled=disabled)
|
||||
user_repository = users.UserRepository(database=database.database)
|
||||
user_repository.save(user)
|
||||
print("{O} added".format(username))
|
@ -1,6 +1,7 @@
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Annotated
|
||||
|
||||
from pymongo import MongoClient
|
||||
from fastapi import Depends, HTTPException, status
|
||||
from fastapi.security import OAuth2PasswordBearer
|
||||
from jose import JWTError, jwt
|
||||
|
@ -1,13 +1,18 @@
|
||||
from fastapi import FastAPI
|
||||
|
||||
from .routers import users, token
|
||||
|
||||
from .dependencies import user_add
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
app.include_router(users.router)
|
||||
app.include_router(token.router)
|
||||
|
||||
|
||||
@app.on_event("startup")
|
||||
async def startup_event():
|
||||
user_add.add(username="Peter93", password="toto", roles="Admin")
|
||||
|
||||
@app.get("/")
|
||||
async def root():
|
||||
return {"message": "Hello World !"}
|
@ -1,5 +1,5 @@
|
||||
from fastapi import APIRouter, Depends
|
||||
from ..dependencies import users_active, permissions_checker
|
||||
from ..dependencies import users_active, permissions_checker, database
|
||||
from ..models import users
|
||||
from typing import Annotated
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user