Commit 55c465ab authored by BODERE's avatar BODERE
Browse files

fix: use settings

parent 8037b265
Pipeline #6488 passed with stage
in 1 minute and 42 seconds
#!/usr/bin/env python
# -*- coding: utf-8 -*
"""Opensearx configuration"""
from typing import List
from pydantic import BaseSettings, BaseModel, HttpUrl
from pydantic.tools import lru_cache
from starlette.config import Config
from starlette.templating import Jinja2Templates
......@@ -8,9 +12,27 @@ from opensearx_ws.opensearch.engine import (IfremerOpensearchEngine,
JPLOpensearchEngine)
config = Config()
class OpensearhEngine(BaseModel):
name: str
root_path: HttpUrl
timeout: float = 10.0
class Settings(BaseSettings):
context_path: str = ""
templates_path: str = "templates"
opensearch_engins: List[OpensearhEngine] = list()
@lru_cache()
def get_settings():
return Settings()
SCRIPT_NAME = config("SCRIPT_NAME", default="")
settings = get_settings()
# jinja2 templates utility
j2_templates = Jinja2Templates(directory=settings.templates_path)
# opensearch engines executed
opensearch_engines = {
......@@ -24,7 +46,6 @@ opensearch_engines = {
JPLOpensearchEngine(root_path='https://cmr.earthdata.nasa.gov/opensearch/granules.atom', timeout=20.0)
}
# jinja2 templates utility
j2_templates = Jinja2Templates(directory="templates")
......@@ -7,6 +7,7 @@ Execute requests on opensearch engines and merge the results.
"""
import asyncio
import os
import uvicorn
from fastapi import Depends, FastAPI
......@@ -14,14 +15,12 @@ from fastapi.responses import ORJSONResponse
from starlette.requests import Request
from starlette.responses import JSONResponse, Response
from opensearx_ws.config import j2_templates, opensearch_engines, SCRIPT_NAME
from opensearx_ws.config import j2_templates, opensearch_engines, settings
from opensearx_ws.opensearch import merge_opensearch_responses
from opensearx_ws.opensearch.model import (OpensearchQueryParameters,
OpensearchResponseFormat)
app = FastAPI(
root_path=SCRIPT_NAME,
)
app = FastAPI(root_path=settings.context_path)
@app.get("/engines")
......@@ -74,6 +73,7 @@ async def granules(
# if format is `atom` -> return the response as an ATOM document using a jinja2 template
if response_fmt == OpensearchResponseFormat.atom:
print(os.path.abspath(settings.templates_path))
return j2_templates.TemplateResponse(
name="granules.atom",
context={"request": request, "response": merged_response},
......
......@@ -43,7 +43,7 @@ class OpensearchEngine:
except httpx.ReadTimeout:
opensearch_response.errors = ["Timeout occurred"]
except Exception as e:
print(str(e))
print(str(e.with_traceback()))
opensearch_response.errors = str(e).split('\n')
finally:
# if opensearch_response.header is None and not opensearch_response.errors:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment