Skip to content

Commit

Permalink
feat(settings): add Docker container variable to settings
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulPHPE committed Feb 15, 2024
1 parent c2a1c03 commit 2f236a9
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
7 changes: 4 additions & 3 deletions bases/ecoindex/cli/arguments_handler.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import os
from tempfile import NamedTemporaryFile
from typing import Set
from urllib.parse import urlparse, urlunparse

from ecoindex.cli.crawl import EcoindexSpider
from ecoindex.models import WindowSize
from ecoindex.config import Settings

from pydantic import AnyHttpUrl, validate_call
from pydantic.types import FilePath
from scrapy.crawler import CrawlerProcess
Expand Down Expand Up @@ -39,7 +40,7 @@ def get_urls_recursive(main_url: str) -> Set[str]:
parsed_url = urlparse(main_url)
netloc = parsed_url.netloc
domain = netloc
if os.environ.get('DOCKER_CONTAINER') == 'true':
if Settings().DOCKER_CONTAINER:
if (parsed_url.hostname == "localhost"):
domain = "host.docker.internal"
netloc = netloc.replace('localhost', 'host.docker.internal')
Expand All @@ -65,7 +66,7 @@ def get_url_from_args(urls_arg: list[AnyHttpUrl]) -> set[AnyHttpUrl]:
urls_from_args = set()
for url in urls_arg:
parsed_url = urlparse(str(url))
if os.environ.get('DOCKER_CONTAINER') == 'true':
if Settings().DOCKER_CONTAINER:
if (parsed_url.hostname == "localhost"):
replaced_netloc = parsed_url.netloc.replace('localhost', 'host.docker.internal')
url = AnyHttpUrl(urlunparse((parsed_url.scheme, replaced_netloc, parsed_url.path, parsed_url.params, parsed_url.query, parsed_url.fragment)))
Expand Down
2 changes: 2 additions & 0 deletions components/ecoindex/config/settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from pydantic_settings import BaseSettings, SettingsConfigDict


Expand All @@ -8,6 +9,7 @@ class Settings(BaseSettings):
CORS_ALLOWED_ORIGINS: list = ["*"]
DAILY_LIMIT_PER_HOST: int = 0
DATABASE_URL: str = "sqlite+aiosqlite:///db.sqlite3"
DOCKER_CONTAINER: bool = os.environ.get('DOCKER_CONTAINER', default='False').lower() == 'true'
DEBUG: bool = False
ENABLE_SCREENSHOT: bool = False
EXCLUDED_HOSTS: list[str] = ["localhost", "127.0.0.1"]
Expand Down
2 changes: 1 addition & 1 deletion projects/ecoindex_cli/dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ RUN poetry export --output=requirements.txt --without-hashes
FROM python:3.12-slim

ARG wheel=ecoindex_cli-2.24.1-py3-none-any.whl
ENV DOCKER_CONTAINER=true
ENV DOCKER_CONTAINER=True

WORKDIR /code

Expand Down
31 changes: 30 additions & 1 deletion projects/ecoindex_cli/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions projects/ecoindex_cli/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ include = ["LICENSE"]
packages = [
{ include = "ecoindex/cli", from = "../../bases" },
{ include = "ecoindex/compute", from = "../../components" },
{ include = "ecoindex/config", from = "../../components" },
{ include = "ecoindex/data", from = "../../components" },
{ include = "ecoindex/exceptions", from = "../../components" },
{ include = "ecoindex/models", from = "../../components" },
Expand All @@ -28,6 +29,7 @@ pandas = "^2.1.2"
playwright = "^1.39.0"
playwright-stealth = "^1.0.6"
pydantic = "^2.4.2"
pydantic-settings = "^2.0.3"
python = ">=3.10,<3.13"
pyyaml = "^6.0.1"
rich = "^13.6.0"
Expand Down

0 comments on commit 2f236a9

Please sign in to comment.