Skip to content
This repository was archived by the owner on Mar 5, 2026. It is now read-only.

Commit caac5fe

Browse files
authored
Merge pull request #49 from Guide-Me-Tech:config
configuration on yaml based files
2 parents b9b0b1b + d61438c commit caac5fe

7 files changed

Lines changed: 159 additions & 29 deletions

File tree

conf/config_models.py

Lines changed: 87 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55

66
import dotenv
7+
import yaml
78

89

910
@dataclass
@@ -40,54 +41,118 @@ class UsageCollectionMongoConfig:
4041
mongo_uri: str = "mongodb://admin:admin@localhost:27017"
4142

4243

44+
@dataclass
45+
class SmartyConfig:
46+
base_url: str = "https://smarty.smartbank.uz"
47+
48+
4349
@dataclass
4450
class AppConfig:
4551
logfire: LogfireConfig
4652
sentry: SentryConfig
4753
otel: OTELConfig
54+
smarty: SmartyConfig
4855
environment: str = "development"
4956
mongo: UsageCollectionMongoConfig | None = None
5057

5158

5259
def New() -> AppConfig:
5360
dotenv.load_dotenv(".env")
54-
# with open(".env", "r") as f:
55-
# print("ENV FILE: ", f.read())
56-
57-
# for key, value in os.environ.items():
58-
# print(f"{key}: {value}")
5961

60-
logfire_config = LogfireConfig(token=os.getenv("LOGFIRE_TOKEN", ""))
61-
sentry_config = SentryConfig(dsn=os.getenv("SENTRY_DSN", ""))
62+
environment = os.getenv("ENVIRONMENT", "development").lower()
63+
64+
# Load per-environment defaults from config.yaml
65+
env_cfg: dict = {}
66+
try:
67+
with open("config.yaml", "r") as f:
68+
all_configs = yaml.safe_load(f) or {}
69+
env_cfg = all_configs.get(environment, {})
70+
except FileNotFoundError:
71+
pass
72+
73+
logfire_cfg = env_cfg.get("logfire", {})
74+
sentry_cfg = env_cfg.get("sentry", {})
75+
otel_cfg = env_cfg.get("otel", {})
76+
mongo_cfg = env_cfg.get("mongo", {})
77+
smarty_cfg = env_cfg.get("smarty", {})
78+
# Resolve console_export: env var overrides yaml, yaml overrides False default
79+
console_export_env = os.getenv("CONSOLE_EXPORT")
80+
if console_export_env is not None:
81+
console_export = console_export_env.lower() in ("true", "1", "yes")
82+
else:
83+
console_export = bool(otel_cfg.get("console_export", False))
84+
85+
logfire_config = LogfireConfig(
86+
token=os.getenv("LOGFIRE_TOKEN", logfire_cfg.get("token", ""))
87+
)
88+
sentry_config = SentryConfig(dsn=os.getenv("SENTRY_DSN", sentry_cfg.get("dsn", "")))
6289
otel_config = OTELConfig(
63-
service_name=os.getenv("SERVICE_NAME", "ui_server"),
90+
service_name=os.getenv(
91+
"SERVICE_NAME", otel_cfg.get("service_name", "ui_server")
92+
),
6493
resource_attributes=os.getenv(
6594
"RESOURCE_ATTRIBUTES",
66-
"deployment.environment=PRODUCTION,service.namespace=ui-server",
95+
otel_cfg.get(
96+
"resource_attributes",
97+
"deployment.environment=production,service.namespace=ui-server",
98+
),
99+
),
100+
bsp_schedule_delay=int(
101+
os.getenv("BSP_SCHEDULE_DELAY", otel_cfg.get("bsp_schedule_delay", 5000))
102+
),
103+
bsp_max_queue_size=int(
104+
os.getenv("BSP_MAX_QUEUE_SIZE", otel_cfg.get("bsp_max_queue_size", 2048))
105+
),
106+
bsp_max_export_batch_size=int(
107+
os.getenv(
108+
"BSP_MAX_EXPORT_BATCH_SIZE",
109+
otel_cfg.get("bsp_max_export_batch_size", 512),
110+
)
111+
),
112+
bsp_export_timeout=int(
113+
os.getenv("BSP_EXPORT_TIMEOUT", otel_cfg.get("bsp_export_timeout", 30000))
114+
),
115+
traces_sampler=os.getenv(
116+
"TRACES_SAMPLER", otel_cfg.get("traces_sampler", "parentbased_traceidratio")
117+
),
118+
traces_sampler_arg=float(
119+
os.getenv("TRACES_SAMPLER_ARG", otel_cfg.get("traces_sampler_arg", 1.0))
67120
),
68-
bsp_schedule_delay=int(os.getenv("BSP_SCHEDULE_DELAY", 5000)),
69-
bsp_max_queue_size=int(os.getenv("BSP_MAX_QUEUE_SIZE", 2048)),
70-
bsp_max_export_batch_size=int(os.getenv("BSP_MAX_EXPORT_BATCH_SIZE", 512)),
71-
bsp_export_timeout=int(os.getenv("BSP_EXPORT_TIMEOUT", 30000)),
72-
traces_sampler=os.getenv("TRACES_SAMPLER", "parentbased_traceidratio"),
73-
traces_sampler_arg=float(os.getenv("TRACES_SAMPLER_ARG", 1.0)),
74121
exporter_otlp_endpoint=os.getenv(
75-
"OTEL_EXPORTER_OTLP_ENDPOINT", "http://localhost:4318/v1/traces"
122+
"OTEL_EXPORTER_OTLP_ENDPOINT",
123+
otel_cfg.get("exporter_otlp_endpoint", "http://localhost:4318/v1/traces"),
76124
),
77-
exporter_otlp_headers=os.getenv("EXPORTER_OTLP_HEADERS", None),
78-
console_export=bool(os.getenv("CONSOLE_EXPORT", False)),
125+
exporter_otlp_headers=os.getenv(
126+
"EXPORTER_OTLP_HEADERS", otel_cfg.get("exporter_otlp_headers")
127+
),
128+
console_export=console_export,
79129
)
80130
return AppConfig(
81-
environment=os.getenv("ENVIRONMENT", "development"),
131+
environment=environment,
82132
logfire=logfire_config,
83133
sentry=sentry_config,
84134
otel=otel_config,
85135
mongo=UsageCollectionMongoConfig(
86-
database_name=os.getenv("MONGO_DATABASE_NAME", "usage"),
87-
collection_name=os.getenv("MONGO_COLLECTION_NAME", "ui_server"),
88-
mongo_uri=os.getenv("MONGO_URI", "mongodb://admin:admin@localhost:27017"),
136+
database_name=os.getenv(
137+
"MONGO_DATABASE_NAME", mongo_cfg.get("database_name", "usage")
138+
),
139+
collection_name=os.getenv(
140+
"MONGO_COLLECTION_NAME", mongo_cfg.get("collection_name", "ui_server")
141+
),
142+
mongo_uri=os.getenv(
143+
"MONGO_URI",
144+
mongo_cfg.get("mongo_uri", "mongodb://admin:admin@localhost:27017"),
145+
),
146+
),
147+
smarty=SmartyConfig(
148+
base_url=os.getenv(
149+
"SMARTY_BASE_URL",
150+
smarty_cfg.get("base_url", "https://smarty.smartbank.uz"),
151+
),
89152
),
90153
)
91154

92155

93156
config = New()
157+
158+
print("Config: ", config)

conf/logger_conf.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,21 @@ def setup_logging(logfile: str) -> structlog.stdlib.BoundLogger:
2626
# --- 1. Create handlers ---
2727
console_handler = logging.StreamHandler(sys.stdout)
2828
log_level = logging.DEBUG
29-
if os.getenv("ENVIRONMENT", "development").lower() == "development":
29+
if os.getenv("LOG_LEVEL", "DEBUG").upper() == "DEBUG":
3030
log_level = logging.DEBUG
31-
else:
31+
elif os.getenv("LOG_LEVEL", "DEBUG").upper() == "INFO":
3232
log_level = logging.INFO
33+
elif os.getenv("LOG_LEVEL", "DEBUG").upper() == "WARNING":
34+
log_level = logging.WARNING
35+
elif os.getenv("LOG_LEVEL", "DEBUG").upper() == "ERROR":
36+
log_level = logging.ERROR
37+
elif os.getenv("LOG_LEVEL", "DEBUG").upper() == "CRITICAL":
38+
log_level = logging.CRITICAL
39+
elif os.getenv("LOG_LEVEL", "DEBUG").upper() == "FATAL":
40+
log_level = logging.FATAL
41+
else:
42+
log_level = logging.DEBUG
43+
3344
console_handler.setLevel(log_level)
3445
console_handler.setFormatter(logging.Formatter(fmt="%(message)s"))
3546

config.yaml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
development:
2+
logfire:
3+
token: ""
4+
sentry:
5+
dsn: ""
6+
otel:
7+
service_name: ui_server
8+
resource_attributes: "deployment.environment=development,service.namespace=ui-server"
9+
bsp_schedule_delay: 5000
10+
bsp_max_queue_size: 2048
11+
bsp_max_export_batch_size: 512
12+
bsp_export_timeout: 30000
13+
traces_sampler: parentbased_traceidratio
14+
traces_sampler_arg: 1.0
15+
exporter_otlp_endpoint: "http://localhost:4318/v1/traces"
16+
exporter_otlp_headers: null
17+
console_export: false
18+
mongo:
19+
database_name: usage
20+
collection_name: ui_server
21+
mongo_uri: "mongodb://admin:admin@localhost:27017"
22+
smarty:
23+
base_url: "https://smarty-test.smartbank.uz"
24+
25+
production:
26+
logfire:
27+
token: ""
28+
sentry:
29+
dsn: ""
30+
otel:
31+
service_name: ui_server
32+
resource_attributes: "deployment.environment=production,service.namespace=ui-server"
33+
bsp_schedule_delay: 5000
34+
bsp_max_queue_size: 2048
35+
bsp_max_export_batch_size: 512
36+
bsp_export_timeout: 30000
37+
traces_sampler: parentbased_traceidratio
38+
traces_sampler_arg: 1.0
39+
exporter_otlp_endpoint: "http://tempo:4318/v1/traces"
40+
exporter_otlp_headers: null
41+
console_export: false
42+
mongo:
43+
database_name: usage
44+
collection_name: ui_server
45+
mongo_uri: "mongodb://admin:admin@mongo:27017"
46+
smarty:
47+
base_url: "https://smarty.smartbank.uz"

functions_to_format/functions/products.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import structlog
2525
from models.context import Context
2626
from .base_strategy import FunctionStrategy
27+
from conf import config
2728

2829
# Import smarty_ui components
2930
from smarty_ui import (
@@ -393,7 +394,7 @@ def make_product_state(
393394
typed=dv.DivActionSubmit(
394395
container_id=cart_container,
395396
request=dv.DivActionSubmitRequest(
396-
url=f"https://smarty.smartbank.uz/chat/v3/tools/call?function_name=add_product_to_cart&chat_id={chat_id}&arguments={json.dumps({'offer_id': p.offer_id, 'quantity': 1})}",
397+
url=f"{config.smarty.base_url}/chat/v3/tools/call?function_name=add_product_to_cart&chat_id={chat_id}&arguments={json.dumps({'offer_id': p.offer_id, 'quantity': 1})}",
397398
method=dv.RequestMethod.POST,
398399
headers=[dv.RequestHeader(name="api-key", value=api_key)],
399400
),

src/server.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@
4444
version = "0"
4545
logger.info("Starting server", env=os.getenv("ENVIRONMENT"), version=version)
4646

47-
app = FastAPI()
47+
app = FastAPI(
48+
title="UI Server",
49+
description="UI Server to generate UI json representations using Yandex Divkit",
50+
version=version,
51+
openapi_url="/ui_server/openapi.json",
52+
)
4853

4954
# Setup telemetry
5055
setup_telemetry(app, service_name="ui_server", version=version)
@@ -72,7 +77,7 @@
7277
logger.info(f"Deleted file: {file}")
7378

7479
# Serve static files
75-
app.mount("/ui_server/static", StaticFiles(directory="static"), name="static")
80+
app.mount("/static", StaticFiles(directory="static"), name="static")
7681

7782

7883
# https://www.youtbe.com/watch?v=NTP4XdTjRK0

telemetry/setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ def setup_telemetry(
8282
_prometheus_reader = PrometheusMetricReader()
8383
metric_readers.append(_prometheus_reader)
8484

85-
if environment == "development" or config.otel.console_export:
85+
if config.otel.console_export:
8686
# Add console exporter for development
87+
print("Adding console exporter for development")
8788
console_metric_exporter = ConsoleMetricExporter()
8889
metric_readers.append(
8990
PeriodicExportingMetricReader(

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)