dockerfiles added
This commit is contained in:
53
apps/api/Dockerfile
Normal file
53
apps/api/Dockerfile
Normal file
@@ -0,0 +1,53 @@
|
||||
# API Service Dockerfile
|
||||
# FastAPI + Redis + MQTT Gateway
|
||||
|
||||
FROM python:3.14-alpine
|
||||
|
||||
# Prevent Python from writing .pyc files and enable unbuffered output
|
||||
ENV PYTHONDONTWRITEBYTECODE=1 \
|
||||
PYTHONUNBUFFERED=1 \
|
||||
MQTT_BROKER=172.16.2.16 \
|
||||
MQTT_PORT=1883 \
|
||||
REDIS_HOST=localhost \
|
||||
REDIS_PORT=6379 \
|
||||
REDIS_DB=0 \
|
||||
REDIS_CHANNEL=ui:updates
|
||||
|
||||
# Create non-root user
|
||||
RUN addgroup -g 10001 -S app && \
|
||||
adduser -u 10001 -S app -G app
|
||||
|
||||
# Set working directory
|
||||
WORKDIR /app
|
||||
|
||||
# Install system dependencies
|
||||
RUN apk add --no-cache \
|
||||
curl \
|
||||
gcc \
|
||||
musl-dev \
|
||||
linux-headers
|
||||
|
||||
# Install Python dependencies
|
||||
COPY apps/api/requirements.txt /app/requirements.txt
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
|
||||
# Copy application code
|
||||
COPY apps/__init__.py /app/apps/__init__.py
|
||||
COPY apps/api/ /app/apps/api/
|
||||
COPY packages/ /app/packages/
|
||||
|
||||
# Change ownership to app user
|
||||
RUN chown -R app:app /app
|
||||
|
||||
# Switch to non-root user
|
||||
USER app
|
||||
|
||||
# Health check
|
||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
|
||||
CMD curl -f http://localhost:8001/health || exit 1
|
||||
|
||||
# Expose port
|
||||
EXPOSE 8001
|
||||
|
||||
# Run application
|
||||
CMD ["python", "-m", "uvicorn", "apps.api.main:app", "--host", "0.0.0.0", "--port", "8001"]
|
||||
@@ -104,10 +104,12 @@ def load_devices() -> list[dict[str, Any]]:
|
||||
def get_mqtt_settings() -> tuple[str, int]:
|
||||
"""Get MQTT broker settings from environment.
|
||||
|
||||
Supports both MQTT_BROKER and MQTT_HOST for compatibility.
|
||||
|
||||
Returns:
|
||||
tuple: (host, port)
|
||||
"""
|
||||
host = os.environ.get("MQTT_HOST", "172.16.2.16")
|
||||
host = os.environ.get("MQTT_BROKER") or os.environ.get("MQTT_HOST", "172.16.2.16")
|
||||
port = int(os.environ.get("MQTT_PORT", "1883"))
|
||||
return host, port
|
||||
|
||||
@@ -115,9 +117,24 @@ def get_mqtt_settings() -> tuple[str, int]:
|
||||
def get_redis_settings() -> tuple[str, str]:
|
||||
"""Get Redis settings from configuration.
|
||||
|
||||
Prioritizes environment variables over config file:
|
||||
- REDIS_HOST, REDIS_PORT, REDIS_DB → redis://host:port/db
|
||||
- REDIS_CHANNEL → pub/sub channel name
|
||||
|
||||
Returns:
|
||||
tuple: (url, channel)
|
||||
"""
|
||||
# Check environment variables first
|
||||
redis_host = os.getenv("REDIS_HOST")
|
||||
redis_port = os.getenv("REDIS_PORT", "6379")
|
||||
redis_db = os.getenv("REDIS_DB", "0")
|
||||
redis_channel = os.getenv("REDIS_CHANNEL", "ui:updates")
|
||||
|
||||
if redis_host:
|
||||
url = f"redis://{redis_host}:{redis_port}/{redis_db}"
|
||||
return url, redis_channel
|
||||
|
||||
# Fallback to config file
|
||||
config_path = Path(__file__).parent.parent.parent / "config" / "devices.yaml"
|
||||
|
||||
if config_path.exists():
|
||||
|
||||
6
apps/api/requirements.txt
Normal file
6
apps/api/requirements.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
fastapi==0.104.1
|
||||
uvicorn[standard]==0.24.0
|
||||
pydantic>=2
|
||||
redis==5.0.1
|
||||
aiomqtt==2.0.1
|
||||
pyyaml==6.0.1
|
||||
Reference in New Issue
Block a user