Compare commits

..

No commits in common. "7118bb2c5bfcd6a9e8c72aae5efdf9f96beab65e" and "9a330f3743aa7f9e1a51a000edbec11444cf3adc" have entirely different histories.

5 changed files with 26 additions and 74 deletions

View File

@ -1,19 +1,10 @@
FROM python:3.13-slim-bookworm
# Set the PYTHONPATH environment variable
ENV PYTHONPATH=/usr/local/lib
# Set the working directory
COPY . /app
WORKDIR /app
# Copy only the requirements file first
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
# Install dependencies
RUN pip3 install --no-cache-dir -r requirements.txt
# Copy the rest of the application code
COPY . .
# Set the command to run your application
CMD ["/bin/sh", "entrypoint.sh"]

View File

@ -1,8 +0,0 @@
services:
backend:
build:
context: .
dockerfile: Dockerfile
ports:
- "2027:5000"
container_name: "${CONTAINER_NAME:-nyi-backend}"

13
backend/run.sh Executable file
View File

@ -0,0 +1,13 @@
#!/bin/bash
# Generate a random name for the container using uuidgen
container_name=$(uuidgen)
# Build the Docker image with your project code and unit tests
docker build -t nyi-backend -f Dockerfile .
# Run the Docker container with the unit tests and attach the output
docker run --tty --rm --name "$container_name" -p 2027:5000 nyi-backend
# Exit the script with the exit code of the container
exit $?

View File

@ -6,39 +6,9 @@
db_client.py is the module for managing teh Dungeon's database services.
'''
from enum import Enum
import sys
import mysql.connector
import os
STARTUP_TABLE_CREATION_QUERIES = {
"users": """CREATE TABLE IF NOT EXISTS users (
Name varchar(255),
Attendance varchar(255),
HasCar bool
);""",
"car": """CREATE TABLE IF NOT EXISTS car (
Name varchar(255),
Car varchar(255),
FreeCarSpaces tinyint(1)
);""",
"food": """CREATE TABLE IF NOT EXISTS food (
Name varchar(255),
Suggestion varchar(2048)
);""",
"passengers": """CREATE TABLE IF NOT EXISTS passengers (
Name varchar(255),
Car varchar(255)
);""",
}
class Severity(Enum):
INFO = "INFO"
WARNING = "WARNING"
ERROR = "ERROR"
class DBClient:
def __init__(self):
self.db_server = os.environ.get('DB_SERVER')
@ -48,13 +18,13 @@ class DBClient:
self.database = os.environ.get('DB_NAME')
if not self.db_server:
self.error("Environment variable 'DB_SERVER' is not set.")
raise ValueError("Environment variable 'DB_SERVER' is not set.")
if not self.db_port:
self.error("Environment variable 'DB_PORT' is not set.")
raise ValueError("Environment variable 'DB_PORT' is not set.")
if not self.password:
self.error("Environment variable 'ROOT_PWD' is not set.")
raise ValueError("Environment variable 'ROOT_PWD' is not set.")
if not self.database:
self.error("Environment variable 'DB_NAME' is not set.")
raise ValueError("Environment variable 'DB_NAME' is not set.")
self.connection = mysql.connector.connect(
host=self.db_server,
@ -77,13 +47,13 @@ class DBClient:
def initialize_database(self):
self.create_database(self.database)
self.switch_database(self.database)
self.query(STARTUP_TABLE_CREATION_QUERIES['users'])
self.query(STARTUP_TABLE_CREATION_QUERIES['car'])
self.query(STARTUP_TABLE_CREATION_QUERIES['food'])
self.query(STARTUP_TABLE_CREATION_QUERIES['passengers'])
self.query("""CREATE TABLE IF NOT EXISTS users (
Name varchar(255),
Attendance bool,
HasCar bool
);""")
def query(self, query_str, quiet=False):
self.info(f'Executing query: {query_str}')
self.cursor.execute(query_str)
if quiet:
return []
@ -93,17 +63,3 @@ class DBClient:
self.cursor.close()
self.connection.close()
def info(self, message):
self.message(severity=Severity.INFO, message=message)
def warning(self, message):
self.message(severity=Severity.WARNING, message=message)
def error(self, message):
self.message(severity=Severity.ERROR, message=message)
sys.exit(1)
def message(self, severity, message):
print(f'DBClient [{severity.value}]: {message}')

View File

@ -16,7 +16,7 @@ database = DBClient()
@app.route('/login', methods=['POST'])
def login():
if request.is_json:
return jsonify({"hello": "user"}), 200
return request.json, 200
else:
return jsonify({'error': 'Request must contain JSON data'}), 400