Add configuration and database setup for Profitra API
This commit is contained in:
parent
0b6cb085e3
commit
57fb0c193e
0
app/__init__.py
Normal file
0
app/__init__.py
Normal file
20
app/config.py
Normal file
20
app/config.py
Normal file
@ -0,0 +1,20 @@
|
||||
from pydantic_settings import BaseSettings
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
DB_HOST: str = "localhost"
|
||||
DB_PORT: int = 3306
|
||||
DB_USER: str = "root"
|
||||
DB_PASS: str = ""
|
||||
DB_NAME: str = ""
|
||||
DB_ECHO: bool = False
|
||||
|
||||
@property
|
||||
def DATABASE_URL(self) -> str:
|
||||
return f"mysql+pymysql://{self.DB_USER}:{self.DB_PASS}@{self.DB_HOST}:{self.DB_PORT}/{self.DB_NAME}"
|
||||
|
||||
class Config:
|
||||
env_file = ".env"
|
||||
|
||||
|
||||
settings = Settings()
|
||||
20
app/database.py
Normal file
20
app/database.py
Normal file
@ -0,0 +1,20 @@
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import DeclarativeBase, sessionmaker
|
||||
|
||||
from app.config import settings
|
||||
|
||||
engine = create_engine(settings.DATABASE_URL, echo=settings.DB_ECHO)
|
||||
|
||||
SessionLocal = sessionmaker(bind=engine)
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
|
||||
|
||||
def get_db():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
27
main.py
27
main.py
@ -1,8 +1,31 @@
|
||||
from fastapi import FastAPI
|
||||
from starlette.middleware.cors import CORSMiddleware
|
||||
|
||||
app = FastAPI()
|
||||
from app.routers import (
|
||||
auth_router,
|
||||
business_types_router,
|
||||
plans_router,
|
||||
subscriptions_router,
|
||||
tenants_router,
|
||||
)
|
||||
|
||||
app = FastAPI(title="Profitra API", version="1.0.0")
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
app.include_router(auth_router)
|
||||
app.include_router(business_types_router)
|
||||
app.include_router(plans_router)
|
||||
app.include_router(tenants_router)
|
||||
app.include_router(subscriptions_router)
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def root():
|
||||
return {"message": "Hello World"}
|
||||
return {"message": "Profitra API"}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user