Skip to content

Commit

Permalink
dockerized the server
Browse files Browse the repository at this point in the history
  • Loading branch information
leomanga committed Nov 9, 2024
1 parent 1ed86e2 commit 614c349
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 19 deletions.
13 changes: 13 additions & 0 deletions core/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM python:3.12.3

WORKDIR /app

COPY . /app

# INSTALL DEPENDENCIES
RUN pip install -r requirements.txt

#EXPOSE 5000

# RUN THE SERVER
CMD python -m server.start
5 changes: 5 additions & 0 deletions core/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
flask
python-dotenv
pydantic
pymongo
icecream
Empty file added core/server/__init__.py
Empty file.
9 changes: 5 additions & 4 deletions database.py → core/server/database.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from pymongo import MongoClient
from abc import ABC, abstractmethod
from icecream import ic

from telemetries import TelemetryData
from env import (
from server.telemetries import TelemetryData
from server.env import (
MONGODB_PATH, MONGODB_CLIENT, MONGODB_COLLECTION
)

Expand All @@ -27,8 +28,8 @@ def store_telemetry(self, telemetry: TelemetryData):
try:
self._collection.insert_one(telemetry.model_dump())

except Exception:
raise Exception(f"Error occurred while saving telemetry data")
except Exception as e:
raise Exception(f"Error occurred while saving telemetry data") from e

def close(self):
self._client.close()
5 changes: 1 addition & 4 deletions env.py → core/server/env.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import os
from dotenv import load_dotenv

load_dotenv()

SERVER_PORT = os.getenv("SERVER_PORT", 5000)

MONGODB_PATH = os.getenv("MONGODB_PATH", 'mongodb://127.0.0.1:27017/')
MONGODB_PATH = os.getenv("MONGODB_PATH", '"mongodb://mongodb_container:27017"/')
MONGODB_CLIENT = os.getenv("MONGODB_CLIENT", 'telemetry_db')
MONGODB_COLLECTION = os.getenv("MONGODB_COLLECTION", 'telemetries')

Expand Down
14 changes: 7 additions & 7 deletions telemetry_endpoint.py → core/server/start.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from flask import Flask, request, jsonify
from icecream import ic

from telemetries import parse_telemetry, TelemetryData
from database import Database, MongoDB
from env import SERVER_PORT
from server.telemetries import parse_telemetry, TelemetryData
from server.database import Database, MongoDB
from server.env import SERVER_PORT

app = Flask(__name__)
database: Database = MongoDB()
Expand All @@ -17,19 +18,18 @@ def receive_telemetries():

# This point may be reached if a new telemetry field is added in a future version,
# as older versions won't send this field and will always have missing data.

return jsonify({"error": "Incomplete telemetry data received. Some required fields are missing."}), 400

telemetry = parse_telemetry(data)

try:
database.store_telemetry(telemetry)
return jsonify({"message": "Success"}), 200

except Exception as e:
return jsonify({"error": e}), 400
ic(e)
return jsonify({"error": "Error occurred while saving telemetry data"}), 400

if __name__ == '__main__':
app.run(port = SERVER_PORT, debug = True)
app.run(host="0.0.0.0", port = SERVER_PORT, debug = True)
database.close()

File renamed without changes.
27 changes: 27 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
services:
mongo:
image: mongo:latest
container_name: mongodb_container
environment:
- MONGO_INITDB_DATABASE=${MONGODB_CLIENT}
ports:
- "27017:27017"
volumes:
- mongo_data:/data/db

app:
build: ./core
container_name: telemetry_server
environment:
- MONGODB_PATH=${MONGODB_PATH}
- MONGODB_CLIENT=${MONGODB_CLIENT}
- MONGODB_COLLECTION=${MONGODB_COLLECTION}
ports:
- "5000:5000"
depends_on:
- mongo
volumes:
- ./core:/app

volumes:
mongo_data:
4 changes: 0 additions & 4 deletions requirements.txt

This file was deleted.

0 comments on commit 614c349

Please sign in to comment.