From 9e5e5da02b4a9704543e0efd7eb568b31a77a369 Mon Sep 17 00:00:00 2001 From: Matheus Date: Sun, 2 Nov 2025 21:23:02 -0300 Subject: list locations --- api/.dockerignore | 1 + api/Dockerfile | 20 ++++++++++++++++++++ api/main.py | 11 +++++++++++ api/models.py | 2 +- api/requirements.txt | 3 +++ api/routes/locations.py | 26 ++++++++++++++++++++++++++ compose.yaml | 19 +++++++++++++------ 7 files changed, 75 insertions(+), 7 deletions(-) create mode 100644 api/.dockerignore diff --git a/api/.dockerignore b/api/.dockerignore new file mode 100644 index 0000000..5ceb386 --- /dev/null +++ b/api/.dockerignore @@ -0,0 +1 @@ +venv diff --git a/api/Dockerfile b/api/Dockerfile index e69de29..5119feb 100644 --- a/api/Dockerfile +++ b/api/Dockerfile @@ -0,0 +1,20 @@ +FROM python:3.14-slim +ENV PYTHONUNBUFFERED=1 + +WORKDIR /api + +RUN useradd \ + --shell /sbin/nologin \ + --uid 10001 \ + --no-create-home \ + api-usr + +COPY requirements.txt . + +RUN python -m pip install -r requirements.txt + +USER api-usr + +COPY . . + +CMD ["fastapi", "run", "main.py", "--port", "80"] diff --git a/api/main.py b/api/main.py index e69de29..b4c85c5 100644 --- a/api/main.py +++ b/api/main.py @@ -0,0 +1,11 @@ +from fastapi import FastAPI +from .routes import locations + +app = FastAPI() +app.include_router(locations.router) + + +@app.get('/') +async def root(): + return {'msg': 'You will never be happy.'} + diff --git a/api/models.py b/api/models.py index 98549bc..c4e16de 100644 --- a/api/models.py +++ b/api/models.py @@ -1,7 +1,7 @@ from sqlalchemy import Column, Integer, String, Float, DateTime, ForeignKey, Boolean from sqlalchemy.orm import relationship from sqlalchemy.sql import func -from database import Base +from .database import Base # ---------------------------- # Location diff --git a/api/requirements.txt b/api/requirements.txt index e69de29..b82030c 100644 --- a/api/requirements.txt +++ b/api/requirements.txt @@ -0,0 +1,3 @@ +fastapi[standard] +psycopg2-binary +sqlalchemy diff --git a/api/routes/locations.py b/api/routes/locations.py index e69de29..c3b30aa 100644 --- a/api/routes/locations.py +++ b/api/routes/locations.py @@ -0,0 +1,26 @@ +from fastapi import APIRouter +from sqlalchemy import select +from ..database import SessionLocal +from ..models import Location + +router = APIRouter( + prefix='/locations', + tags=['locations'] +) + +@router.get('/') +async def get_locations(): + dc = {'locations': []} + session = SessionLocal() + stmt = select(Location.street, Location.avenue, Location.zip_code) + locs = session.execute(stmt).all() # TODO: Page results + for loc in locs: + l = { + 'street': loc.street, + 'avenue': loc.avenue, + 'zip_code': loc.zip_code + } + dc['locations'].append(l) + dc['size'] = len(dc['locations']) + return dc + diff --git a/compose.yaml b/compose.yaml index 9af60c6..8892fda 100644 --- a/compose.yaml +++ b/compose.yaml @@ -28,6 +28,7 @@ services: context: ./mqtt_client/ networks: - mosquitto + - database depends_on: - mosquitto - postgres @@ -35,9 +36,7 @@ services: image: postgres:13 container_name: postgres networks: - - mosquitto - ports: - - "5432:5432" + - database environment: POSTGRES_PASSWORD: public POSTGRES_USER: root @@ -45,8 +44,6 @@ services: volumes: #- ${PWD}/database/init_scripts:/docker-entrypoint-initdb.d - db_data:/var/lib/postgresql/data - - pgadmin: image: dpage/pgadmin4 container_name: pgadmin @@ -56,9 +53,19 @@ services: ports: - "5050:80" networks: - - mosquitto + - database + depends_on: + - postgres + api: + build: + context: ./api/ + networks: + - database + ports: + - "127.0.0.1:8000:80" depends_on: - postgres + networks: mosquitto: database: -- cgit v1.2.3