summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--api/.dockerignore1
-rw-r--r--api/Dockerfile20
-rw-r--r--api/main.py11
-rw-r--r--api/models.py2
-rw-r--r--api/requirements.txt3
-rw-r--r--api/routes/locations.py26
-rw-r--r--compose.yaml19
7 files changed, 75 insertions, 7 deletions
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: