feat: add database configuration and health check
This commit is contained in:
parent
3d53a94d59
commit
3701c50711
9 changed files with 72 additions and 2 deletions
5
backend/hermes/.env
Normal file
5
backend/hermes/.env
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
DATABASE_URL=postgresql+psycopg://olympus:FsFs03285310!!!@olympus-db:5432/olympus
|
||||||
|
|
||||||
|
APP_NAME=Hermes API
|
||||||
|
|
||||||
|
APP_VERSION=0.1.0
|
||||||
Binary file not shown.
BIN
backend/hermes/app/core/__pycache__/config.cpython-314.pyc
Normal file
BIN
backend/hermes/app/core/__pycache__/config.cpython-314.pyc
Normal file
Binary file not shown.
16
backend/hermes/app/core/config.py
Normal file
16
backend/hermes/app/core/config.py
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||||
|
|
||||||
|
|
||||||
|
class Settings(BaseSettings):
|
||||||
|
app_name: str = "Hermes API"
|
||||||
|
app_version: str = "0.1.0"
|
||||||
|
|
||||||
|
database_url: str
|
||||||
|
|
||||||
|
model_config = SettingsConfigDict(
|
||||||
|
env_file=".env",
|
||||||
|
case_sensitive=False
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
settings = Settings()
|
||||||
BIN
backend/hermes/app/db/__pycache__/database.cpython-314.pyc
Normal file
BIN
backend/hermes/app/db/__pycache__/database.cpython-314.pyc
Normal file
Binary file not shown.
BIN
backend/hermes/app/db/__pycache__/health.cpython-314.pyc
Normal file
BIN
backend/hermes/app/db/__pycache__/health.cpython-314.pyc
Normal file
Binary file not shown.
29
backend/hermes/app/db/database.py
Normal file
29
backend/hermes/app/db/database.py
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy.orm import DeclarativeBase
|
||||||
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
|
from app.core.config import settings
|
||||||
|
|
||||||
|
|
||||||
|
engine = create_engine(
|
||||||
|
settings.database_url,
|
||||||
|
echo=False
|
||||||
|
)
|
||||||
|
|
||||||
|
SessionLocal = sessionmaker(
|
||||||
|
autocommit=False,
|
||||||
|
autoflush=False,
|
||||||
|
bind=engine
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class Base(DeclarativeBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def get_db():
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
yield db
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
12
backend/hermes/app/db/health.py
Normal file
12
backend/hermes/app/db/health.py
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
from sqlalchemy import text
|
||||||
|
|
||||||
|
from app.db.database import engine
|
||||||
|
|
||||||
|
|
||||||
|
def check_database() -> bool:
|
||||||
|
try:
|
||||||
|
with engine.connect() as connection:
|
||||||
|
connection.execute(text("SELECT 1"))
|
||||||
|
return True
|
||||||
|
except Exception:
|
||||||
|
return False
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
|
|
||||||
|
from app.db.health import check_database
|
||||||
|
|
||||||
app = FastAPI(
|
app = FastAPI(
|
||||||
title="Hermes API",
|
title="Hermes API",
|
||||||
version="0.1.0",
|
version="0.1.0",
|
||||||
|
|
@ -9,9 +11,15 @@ app = FastAPI(
|
||||||
|
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
def root():
|
def root():
|
||||||
return {"service": "Hermes", "status": "running"}
|
return {
|
||||||
|
"service": "Hermes",
|
||||||
|
"status": "running"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@app.get("/health")
|
@app.get("/health")
|
||||||
def health():
|
def health():
|
||||||
return {"status": "healthy"}
|
return {
|
||||||
|
"status": "healthy",
|
||||||
|
"database": check_database()
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue