Skip to content

Commit

Permalink
fix: use saldo_ws.config.Settings
Browse files Browse the repository at this point in the history
  • Loading branch information
kod-kristoff committed Mar 11, 2024
1 parent 34bd5ee commit d1e7268
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/sblex/fm_server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class Settings(BaseSettings):
morphology_path: str
otel: OTelSettings = OTelSettings()
otel: OTelSettings
model_config = SettingsConfigDict(env_file=".env", extra="ignore")


Expand Down
3 changes: 1 addition & 2 deletions src/sblex/main/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from sblex.main import telemetry
from sblex.main.bootstrap import Settings, bootstrap_app

__all__ = ["bootstrap_app", "telemetry", "Settings"]
__all__ = ["bootstrap_app", "Settings"]
__version__ = "0.2.3"


Expand Down
1 change: 0 additions & 1 deletion src/sblex/main/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from typing import Optional, Tuple, TypedDict

import environs
from sblex.main import telemetry

Settings = TypedDict(
"Settings",
Expand Down
42 changes: 42 additions & 0 deletions src/sblex/saldo_ws/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from typing import Optional

from pydantic import BaseModel
from pydantic_settings import BaseSettings, SettingsConfigDict
from sblex.telemetry.settings import OTelSettings


class AppSettings(BaseModel):
base_url: Optional[str] = None
root_path: str = ""
template_directory: str = "templates"
# model_config = SettingsConfigDict(env_file=".env", extra="ignore")


class MatomoSettings(BaseModel):
matomo_url: Optional[str] = None
matomo_idsite: Optional[int] = None
matomo_token: Optional[str] = None
# model_config = SettingsConfigDict(env_file=".env", extra="ignore", env_prefix="TRACKING_")


class FrontendSettings(BaseModel):
tracking: MatomoSettings = MatomoSettings()


class Settings(BaseSettings):
semantic_path: str
fm_server_url: str
fm_server_url: str
otel: OTelSettings
app: AppSettings = AppSettings()
frontend: FrontendSettings = FrontendSettings()
tracking: MatomoSettings
model_config = SettingsConfigDict(
env_file=".env", extra="ignore", env_nested_delimiter="__", env_prefix="SALDO_WS__"
)


def read_settings_from_env() -> Settings:
otel = OTelSettings(_env_prefix="SALDO_WS_")
settings = Settings(otel=otel)
return settings
6 changes: 6 additions & 0 deletions src/sblex/saldo_ws/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from sblex.saldo_ws import config, server

settings = config.read_settings_from_env()
print(f"{settings=}")

# app = server.create_saldo_ws_server(settings=settings)
50 changes: 24 additions & 26 deletions src/sblex/saldo_ws/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,37 @@
from fastapi.staticfiles import StaticFiles
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
from sblex import main
from sblex.main import telemetry
from sblex.saldo_ws import routes, tasks, templating
from sblex import main, telemetry
from sblex.saldo_ws import config, routes, tasks, templating

logger = logging.getLogger(__name__)


def create_webapp(
*,
env: environs.Env | None = None,
config: dict | None = None,
) -> FastAPI:
app_context, env = main.bootstrap_app(env=env, config=config)
def create_saldo_ws_server(*, settings: config.Settings | None = None) -> FastAPI:
# app_context, env = main.bootstrap_app(env=env, config=config)

telemetry.init_otel_logging(env=env)
logger.warning("loaded settings", extra={"settings": str(app_context.settings)})
telemetry.init_otel_logging(settings.otel)
logger.warning("loaded settings", extra={"settings": str(settings)})
logger.debug("loading telemetry")
telemetry.init_otel_tracing("sblex-server", env=env)
telemetry.init_otel_tracing(settings.otel, fallback_name="saldo-ws")
HTTPXClientInstrumentor().instrument()

logger.debug("creating app")
webapp = FastAPI(
title="Saldo WS",
version=main.get_version(),
openapi_url=f"{app_context.settings['webapp.root_path']}/openapi.json",
openapi_url=f"{settings.app.root_path}/openapi.json",
docs_url=None,
redoc_url="/",
root_path=app_context.settings["webapp.root_path"],
root_path=settings.app.root_path,
lifespan=tasks.lifespan,
) # , lifespan=lifespan)

webapp.state.app_context = app_context
webapp.state.config = app_context.settings
# webapp.state.app_context = app_context
webapp.state.settings = settings

# Configure templates
webapp.state.templates = templating.init_template_engine(app_context.settings)
webapp.state.templates = templating.init_template_engine(settings.app)

# Add middlewares (in reverse order)
webapp.add_middleware(BrotliMiddleware, gzip_fallback=True)
Expand All @@ -55,15 +50,18 @@ def create_webapp(
allow_methods=["*"],
allow_headers=["*"],
)
if webapp.state.config["tracking.matomo.url"]:
logger.info("adding MatomoMiddleware")
webapp.add_middleware(
MatomoMiddleware,
idsite=webapp.state.config["tracking.matomo.idsite"],
matomo_url=webapp.state.config["tracking.matomo.url"],
access_token=webapp.state.config["tracking.matomo.token"],
exclude_patterns=[".*/html.*"],
)
if webapp.state.settings.tracking.matomo_url:
if webapp.state.settings.tracking.matomo_idsite is None:
logger.error("TRACKING_MATOMO_URL is set but not TRACKING_MATOMO_IDSITE")
else:
logger.info("adding MatomoMiddleware")
webapp.add_middleware(
MatomoMiddleware,
idsite=webapp.state.settings.tracking.matomo_idsite,
matomo_url=webapp.state.settings.tracking.matomo_url,
access_token=webapp.state.settings.tracking.matomo_token,
exclude_patterns=[".*/html.*"],
)
else:
logger.warning(
"NOT tracking to Matomo, please set TRACKING_MATOMO_URL and TRACKING_MATOMO_IDSITE."
Expand Down
4 changes: 2 additions & 2 deletions src/sblex/saldo_ws/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def start_app() -> None:

def load_lookup_lid(app: FastAPI) -> None:
logger.info("loading lookup lid")
lookup_lid = MemLookupLid.from_tsv_path(app.state.config["semantic.path"])
lookup_lid = MemLookupLid.from_tsv_path(app.state.settings.semantic_path)
app.state._lookup_lid = lookup_lid


Expand All @@ -33,7 +33,7 @@ def load_morphology(app: FastAPI) -> None:
@asynccontextmanager
async def lifespan(app: FastAPI):
logger.info("startup")
app.state._fm_client = httpx.AsyncClient(base_url=app.state.config["fm.server.url"])
app.state._fm_client = httpx.AsyncClient(base_url=app.state.settings.fm_server_url)
load_lookup_lid(app)
yield
await app.state._fm_client.aclose()
Expand Down
18 changes: 8 additions & 10 deletions src/sblex/saldo_ws/templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,34 @@
import jinja2
from fastapi import Request
from fastapi.templating import Jinja2Templates
from sblex import main
from sblex.saldo_ws import config


def build_context(
request: Request, *, title: str, service: str, show_bar: bool = True, **kwargs
) -> dict[str, Any]:
settings = request.app.state.config
settings: config.Settings = request.app.state.settings
return {
"request": request,
"bar": show_bar,
"title": title,
"tracking_base_url": settings["tracking.matomo.frontend.base_url"],
"tracking_site_id": settings["tracking.matomo.frontend.site_id"],
"tracking_base_url": settings.frontend.tracking.matomo_url,
"tracking_site_id": settings.frontend.tracking.matomo_idsite,
**kwargs,
}


def init_template_engine(settings: main.Settings) -> Jinja2Templates:
templates = Jinja2Templates(directory="templates")
def init_template_engine(settings: config.AppSettings) -> Jinja2Templates:
templates = Jinja2Templates(directory=settings.template_directory)
templates.env.globals["url_for"] = custom_url_for
return templates


@jinja2.pass_context
def custom_url_for(context: dict, name: str, **path_params) -> str:
request: Request = context["request"]
if base_url := request.app.state.config.get("webapp.base_url"):
http_url = request.app.url_path_for(name, **path_params).make_absolute_url(
base_url
)
if base_url := request.app.state.settings.app.base_url:
http_url = request.app.url_path_for(name, **path_params).make_absolute_url(base_url)
else:
http_url = request.url_for(name, **path_params)
return http_url
31 changes: 22 additions & 9 deletions tests/e2e/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
from httpx import AsyncClient
from sblex.fm_server.config import Settings as FmSettings
from sblex.fm_server.server import create_fm_server
from sblex.telemetry.settings import OTelSettings
from sblex.saldo_ws.config import Settings as SaldoWsSettings
from sblex.saldo_ws.deps import get_fm_client
from sblex.saldo_ws.server import create_webapp
from sblex.saldo_ws.server import create_saldo_ws_server
from sblex.telemetry.settings import OTelSettings


@pytest.fixture(name="env")
Expand All @@ -22,12 +23,20 @@ def fixture_env() -> environs.Env:

@pytest.fixture(name="webapp")
def fixture_webapp(env: environs.Env, fm_client: AsyncClient) -> FastAPI:
webapp = create_webapp(
config={
"semantic.path": "assets/testing/saldo.txt",
"morphology.path": "assets/testing/saldo.lex",
},
env=env,
webapp = create_saldo_ws_server(
settings=SaldoWsSettings(
semantic_path="assets/testing/saldo.txt",
otel=OTelSettings(
otel_service_name="saldo-ws",
debug_log_otel_to_console=False,
debug_log_otel_to_provider=False,
),
)
# config={
# "semantic.path": "assets/testing/saldo.txt",
# "morphology.path": "assets/testing/saldo.lex",
# },
# env=env,
)

def override_fm_client() -> AsyncClient:
Expand All @@ -49,7 +58,11 @@ def fixture_fm_server(env: environs.Env) -> FastAPI:
return create_fm_server(
settings=FmSettings(
morphology_path="assets/testing/saldo.lex",
otel=OTelSettings(debug_log_otel_to_console=False, debug_log_otel_to_provider=False),
otel=OTelSettings(
otel_service_name="fm-server",
debug_log_otel_to_console=False,
debug_log_otel_to_provider=False,
),
)
)

Expand Down

0 comments on commit d1e7268

Please sign in to comment.