Skip to content

Commit

Permalink
feat: use pydantic settings to load environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
djs0109 committed Mar 4, 2024
1 parent 0db565a commit 11418cc
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 32 deletions.
7 changes: 5 additions & 2 deletions backend/api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ ENV PYTHONUNBUFFERED 1
WORKDIR /app

# Copy requirements.txt
COPY requirements.txt ./
COPY ./backend/api/requirements.txt ./

# Copy settings.py
COPY settings.py ./

# Install dependencies
RUN pip install -r requirements.txt

# Copy source code
COPY . ./
COPY ./backend/api/. ./

# Expose application on port 8000
EXPOSE 8000
Expand Down
14 changes: 7 additions & 7 deletions backend/api/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
import os
from typing import List, Optional
from uuid import uuid4
import asyncpg
Expand All @@ -9,6 +8,7 @@
from pydantic import BaseModel, Field, Extra
from redis import asyncio as aioredis
import aiohttp
from settings import settings

app = FastAPI()
# enable CORS for the frontend
Expand All @@ -20,13 +20,13 @@
allow_headers=["*"],
)

host = os.environ.get("POSTGRES_HOST", "localhost")
user = os.environ.get("POSTGRES_USER", "admin")
password = os.environ.get("POSTGRES_PASSWORD", "postgres")
database = os.environ.get("POSTGRES_DB", "iot_devices")
host = settings.POSTGRES_HOST
user = settings.POSTGRES_USER
password = settings.POSTGRES_PASSWORD
database = settings.POSTGRES_DB
DATABASE_URL = f"postgresql://{user}:{password}@{host}/{database}"
ORION_URL = os.environ.get("ORION_URL", "http://localhost:1026")
REDIS_URL = os.environ.get("REDIS_URL", "redis://localhost:6379")
ORION_URL = settings.ORION_URL
REDIS_URL = settings.REDIS_URL


# Pydantic model
Expand Down
7 changes: 5 additions & 2 deletions backend/gateway/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ ENV PYTHONUNBUFFERED 1
WORKDIR /app

# Copy requirements.txt
COPY requirements.txt ./
COPY ./backend/gateway/requirements.txt ./

# Copy settings.py
COPY settings.py ./

# Install dependencies
RUN pip install -r requirements.txt

# Copy source code
COPY . ./
COPY ./backend/gateway/. ./

# Run the gateway
CMD ["python", "gateway.py"]
30 changes: 15 additions & 15 deletions backend/gateway/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import asyncio
import json
import os
import ssl
from typing import List, Tuple

Expand All @@ -17,26 +16,27 @@
from jsonpath_ng import parse
from redis import asyncio as aioredis
from uuid import uuid4
from settings import settings

# Load configuration from JSON file
MQTT_HOST = os.environ.get("MQTT_HOST", "localhost")
MQTT_PORT = int(os.environ.get("MQTT_PORT", 1883))
MQTT_USER = os.environ.get("MQTT_USER", None)
MQTT_PASSWORD = os.environ.get("MQTT_PASSWORD", None)
MQTT_TLS = os.environ.get("MQTT_TLS", False)
MQTT_HOST = settings.MQTT_HOST
MQTT_PORT = settings.MQTT_PORT
MQTT_USER = settings.MQTT_USER
MQTT_PASSWORD = settings.MQTT_PASSWORD
MQTT_TLS = settings.MQTT_TLS
if MQTT_TLS:
tls_context = ssl.create_default_context()
else:
tls_context = None
REDIS_URL = os.environ.get("REDIS_URL", "redis://localhost:6379")
orion = os.environ.get("ORION_URL", "http://localhost:1026")
service = os.environ.get("FIWARE_SERVICE", "gateway")
service_path = os.environ.get("FIWARE_SERVICEPATH", "/gateway")
REDIS_URL = settings.REDIS_URL
orion_url = settings.ORION_URL
service = settings.FIWARE_SERVICE
service_path = settings.FIWARE_SERVICEPATH

host = os.environ.get("POSTGRES_HOST", "localhost")
user = os.environ.get("POSTGRES_USER", "karelia")
password = os.environ.get("POSTGRES_PASSWORD", "postgres")
database = os.environ.get("POSTGRES_DB", "iot_devices")
host = settings.POSTGRES_HOST
user = settings.POSTGRES_USER
password = settings.POSTGRES_PASSWORD
database = settings.POSTGRES_DB

DATABASE_URL = f"postgresql://{user}:{password}@{host}/{database}"

Expand Down Expand Up @@ -175,7 +175,7 @@ async def process_mqtt_message(
# Send the payload to the Orion Context Broker
try:
await session.patch(
url=f"{orion}/v2/entities/{datapoint['entity_id']}/attrs?type={datapoint['entity_type']}&options=keyValues",
url=f"{orion_url}/v2/entities/{datapoint['entity_id']}/attrs?type={datapoint['entity_type']}&options=keyValues",
json=payload,
headers={
"fiware-service": service,
Expand Down
12 changes: 6 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ services:
env_file:
- .env
ports:
- 5173:5173
- "5173:5173"
networks:
- default
# volumes:
# - ./frontend:/dist # for hot reloading

api:
build:
context: ./backend/api
dockerfile: Dockerfile
context: ./
dockerfile: ./backend/api/Dockerfile
container_name: mqtt_gateway_api
ports:
- 8000:8000
- "8000:8000"
networks:
- default
# volumes:
Expand All @@ -38,8 +38,8 @@ services:

gateway:
build:
context: ./backend/gateway
dockerfile: Dockerfile
context: ./
dockerfile: ./backend/gateway/Dockerfile
networks:
- default
# volumes:
Expand Down
24 changes: 24 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from pydantic import BaseSettings


class Settings(BaseSettings):
ORION_URL: str = "http://orion:1026"
MQTT_HOST: str = "test.mosquitto.org"
MQTT_PORT: int = 1883
MQTT_USER: str = None
MQTT_PASSWORD: str = None
MQTT_TLS: bool = False
POSTGRES_HOST: str = "localhost"
POSTGRES_USER: str = "admin"
POSTGRES_PASSWORD: str = "postgres"
POSTGRES_DB: str = "iot_devices"
REDIS_URL: str = "redis://localhost:6379"
FIWARE_SERVICE: str = "gateway_test"
FIWARE_SERVICEPATH: str = "/"

class Config:
env_file = ".env"


# Create an instance of the settings
settings = Settings()

0 comments on commit 11418cc

Please sign in to comment.