osnova-api-alert/config/config.py

105 lines
2.4 KiB
Python
Raw Permalink Normal View History

2024-10-08 10:45:07 +00:00
from pydantic import BaseModel
from pydantic_settings import (
BaseSettings,
SettingsConfigDict,
)
from pathlib import Path
import logging
BASE_DIR = Path(__file__).parent.parent
TEMPLATES_DIR = BASE_DIR / "web" / "templates"
STATIC_DIR = BASE_DIR / "web" / "static"
class RunConfig(BaseModel):
host: str = "0.0.0.0"
port: int = 8000
reload: bool = False
class LogConfig(BaseModel):
2024-10-08 11:26:04 +00:00
level: int = 30
2024-10-09 17:34:38 +00:00
level_to_file: int = 30
2024-10-08 11:26:04 +00:00
dateformat: str = "%Y-%m-%d %H:%M:%S"
2024-10-08 10:45:07 +00:00
format: str = (
2024-10-09 17:39:58 +00:00
"[%(asctime)s.%(msecs)03d] %(module)-25s:%(lineno)4d | %(funcName)-20s| %(levelname)-8s | %(message)s"
2024-10-08 10:45:07 +00:00
)
2024-10-08 16:10:34 +00:00
class PrefixConfig(BaseModel):
2024-10-08 11:26:04 +00:00
swagger: str = "/docs"
api_v1: str = "/api/v1"
2024-10-08 14:33:15 +00:00
tg_v1: str = api_v1 + "/tg"
2024-10-09 08:28:57 +00:00
ping: str = "/ping"
2024-10-10 10:55:50 +00:00
zbx: str = api_v1 + "/zbx"
2024-10-08 14:33:15 +00:00
2024-10-08 19:29:50 +00:00
class RedisConfig(BaseModel):
2024-10-08 16:10:34 +00:00
host: str = "localhost"
port: int = 6379
2024-10-08 21:14:07 +00:00
pwd: str | None = None
2024-10-08 16:10:34 +00:00
2024-10-09 11:26:34 +00:00
class TelegramConfig(BaseModel):
2024-10-10 10:55:50 +00:00
bot_token: str
chat_id: int
2024-10-09 11:26:34 +00:00
dashboard_tred_id: int | None = None
2024-10-09 16:28:02 +00:00
net_tred_id: int | None = None
2024-10-09 11:26:34 +00:00
2024-10-10 10:55:50 +00:00
class FromZabbix(BaseModel):
token: str
close_alert_pattern: str
2024-10-08 10:45:07 +00:00
class SwaggerConfig(BaseModel):
openapi_url: str = "/openapi.json"
title: str = "API"
oauth2_redirect_url: str = "/docs/oauth2-redirect"
swagger_js_url: str = "/static/swagger/swagger-ui-bundle.js"
swagger_css_url: str = "/static/swagger/swagger-ui.css"
swagger_favicon_url: str = "/static/swagger/favicon.png"
2024-10-08 19:32:14 +00:00
login: str
pwd: str
2024-10-08 10:45:07 +00:00
class Settings(BaseSettings):
model_config = SettingsConfigDict(
env_file=(
BASE_DIR / "config" / ".env-template",
BASE_DIR / "config" / ".env",
),
case_sensitive=False,
env_nested_delimiter="__",
env_prefix="OAA_CFG__",
)
run: RunConfig = RunConfig()
2024-10-08 21:14:07 +00:00
swagger: SwaggerConfig
2024-10-08 10:45:07 +00:00
log: LogConfig = LogConfig()
2024-10-08 16:10:34 +00:00
prefix: PrefixConfig = PrefixConfig()
2024-10-08 19:29:50 +00:00
redis: RedisConfig
2024-10-09 11:26:34 +00:00
tg: TelegramConfig
2024-10-10 10:55:50 +00:00
zbx: FromZabbix
2024-10-08 10:45:07 +00:00
conf = Settings()
2024-10-09 17:34:38 +00:00
2024-10-08 10:45:07 +00:00
logging.basicConfig(
level=conf.log.level,
2024-10-08 11:26:04 +00:00
datefmt=conf.log.dateformat,
2024-10-08 10:45:07 +00:00
format=conf.log.format,
)
2024-10-09 17:34:38 +00:00
file_handler = logging.FileHandler(BASE_DIR / "logfile.log")
file_handler.setLevel(conf.log.level_to_file)
file_handler.setFormatter(
logging.Formatter(
"[%(asctime)s.%(msecs)03d] %(module)s:%(lineno)4d | %(funcName)s| %(levelname)s | %(message)s"
)
)
logging.getLogger().addHandler(file_handler)