Initializing hub database
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
.env
|
||||||
@@ -1,13 +1,19 @@
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
import motor.motor_asyncio
|
from beanie import init_beanie
|
||||||
from beanie import Document
|
from motor.motor_asyncio import AsyncIOMotorClient
|
||||||
from fastapi_users.db import BaseOAuthAccount, BeanieBaseUser, BeanieUserDatabase
|
|
||||||
from pydantic import Field
|
|
||||||
|
|
||||||
DATABASE_URL = "mongodb://localhost:27017"
|
from hub.user import User
|
||||||
client = motor.motor_asyncio.AsyncIOMotorClient(
|
from hub.auth import AccessToken
|
||||||
DATABASE_URL, uuidRepresentation="standard"
|
|
||||||
)
|
|
||||||
db = client["database_name"]
|
|
||||||
|
|
||||||
|
MONGO_USERNAME = os.getenv("MONGO_INITDB_ROOT_USERNAME")
|
||||||
|
MONGO_PASSWORD = os.getenv("MONGO_INITDB_ROOT_PASSWORD")
|
||||||
|
|
||||||
|
DATABASE_URL = f"mongodb://{MONGO_USERNAME}:{MONGO_PASSWORD}@mongo:27017"
|
||||||
|
|
||||||
|
async def init_db():
|
||||||
|
client = AsyncIOMotorClient(DATABASE_URL, uuidRepresentation="standard")
|
||||||
|
|
||||||
|
await init_beanie(database=client.hub,
|
||||||
|
document_models=[User, AccessToken],
|
||||||
|
allow_index_dropping=True)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ from contextlib import asynccontextmanager
|
|||||||
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
|
|
||||||
|
from hub import init_db as hub_init_db
|
||||||
from hub.auth import oauth_router
|
from hub.auth import oauth_router
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
@@ -11,6 +12,7 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
|
await hub_init_db()
|
||||||
# create_db_and_tables()
|
# create_db_and_tables()
|
||||||
# create_admin_user()
|
# create_admin_user()
|
||||||
yield
|
yield
|
||||||
|
|||||||
Reference in New Issue
Block a user