diff --git a/Teil1/app/Dockerfile b/Teil1/app/Dockerfile new file mode 100644 index 0000000..29765d6 --- /dev/null +++ b/Teil1/app/Dockerfile @@ -0,0 +1,14 @@ +# Example of Dockerfile + +FROM python:3.8.5-alpine3.12 + +WORKDIR /app + +EXPOSE 5000 +ENV FLASK_APP=app.py + +COPY . /app +RUN pip install -r requirements.txt + +ENTRYPOINT [ "flask"] +CMD [ "run", "--host=0.0.0.0" ] diff --git a/Teil1/app/app.py b/Teil1/app/app.py new file mode 100644 index 0000000..4b9d90f --- /dev/null +++ b/Teil1/app/app.py @@ -0,0 +1,100 @@ +from flask import Flask, request, jsonify, render_template +import mysql.connector + +app = Flask(__name__) + +def employee_data(): + # Connect to MySQL database + conn = mysql.connector.connect( + host='db', # Use the IP address or hostname of the MySQL container + port='3306', # MySQL port + user='root', # MySQL username + password='root_password', # MySQL password + database='employees' # Name of the database + ) + + # Create a cursor object to execute SQL commands + cursor = conn.cursor() + + cursor.execute('SELECT Employee_Name, Title FROM employee_data') + results = cursor.fetchall() + cursor.close() + conn.close() + return results + +# Endpoint to add a new entry to the database +@app.route('/add', methods=['POST']) +def add_entry(): + # Connect to MySQL database + conn = mysql.connector.connect( + host='db', # Use the IP address or hostname of the MySQL container + port='3306', # MySQL port + user='root', # MySQL username + password='root_password', # MySQL password + database='employees' # Name of the database + ) + + # Create a cursor object to execute SQL commands + cursor = conn.cursor() + + # Get data from request + data = request.get_json() + employee_name = data.get('employee_name') + title = data.get('title') + + # SQL query to insert data into the employee_data table + sql_query = "INSERT INTO employee_data (Employee_Name, Title) VALUES (%s, %s)" + + try: + # Execute the SQL query + cursor.execute(sql_query, (employee_name, title)) + # Commit the transaction + conn.commit() + cursor.close() + conn.close() + return jsonify({'message': 'Entry added successfully'}), 200 + except Exception as e: + # Rollback in case of any error + conn.rollback() + cursor.close() + conn.close() + return jsonify({'error': str(e)}), 500 + +# Endpoint to list all entries in the database +@app.route('/entries', methods=['GET']) +def get_entries(): + # Connect to MySQL database + conn = mysql.connector.connect( + host='db', # Use the IP address or hostname of the MySQL container + port='3306', # MySQL port + user='root', # MySQL username + password='root_password', # MySQL password + database='employees' # Name of the database + ) + + # Create a cursor object to execute SQL commands + cursor = conn.cursor() + # SQL query to select all entries from the employee_data table + sql_query = "SELECT * FROM employee_data" + + # Execute the SQL query + cursor.execute(sql_query) + # Fetch all entries + entries = cursor.fetchall() + + # Convert entries to JSON format + entries_json = [{'Employee_Name': entry[0], 'Title': entry[1]} for entry in entries] + cursor.close() + conn.close() + return jsonify(entries_json), 200 + +@app.route('/') +def index(): + #return jsonify({'Employee Data': employee_data()}) + entries = employee_data() + return render_template('index.html', entries=entries) + #return "Hello world" + +if __name__ == '__main__': + #app.run(debug = True, host='0.0.0.0') + app.run() diff --git a/Teil1/app/requirements.txt b/Teil1/app/requirements.txt new file mode 100644 index 0000000..37806b7 --- /dev/null +++ b/Teil1/app/requirements.txt @@ -0,0 +1,2 @@ +Flask +mysql-connector diff --git a/Teil1/app/templates/index.html b/Teil1/app/templates/index.html new file mode 100644 index 0000000..2bbf22f --- /dev/null +++ b/Teil1/app/templates/index.html @@ -0,0 +1,37 @@ + + + + + + MySQL Database Entries + + + +

SQLite Database Entries

+ + + + + + + {% for entry in entries %} + + + + + + {% endfor %} +
NamePosition
{{ entry[0] }}{{ entry[1] }}
+ + + diff --git a/Teil1/db/Dockerfile b/Teil1/db/Dockerfile new file mode 100644 index 0000000..c7db0f1 --- /dev/null +++ b/Teil1/db/Dockerfile @@ -0,0 +1,12 @@ +# Use the official MySQL image from Docker Hub +FROM mysql:latest + +# Set environment variables +ENV MYSQL_ROOT_PASSWORD=root_password +ENV MYSQL_DATABASE=employees + +# Copy the SQL initialization script into the container +COPY init.sql /docker-entrypoint-initdb.d/ + +# Expose port 3306 to allow external connections to the database +EXPOSE 3306 diff --git a/Teil1/db/init.sql b/Teil1/db/init.sql new file mode 100755 index 0000000..a6422a0 --- /dev/null +++ b/Teil1/db/init.sql @@ -0,0 +1,15 @@ +CREATE DATABASE employees; +USE employees; + + +CREATE TABLE employee_data ( + Employee_Name VARCHAR(50), + Title VARCHAR(50) +); + + +INSERT INTO employee_data + (Employee_Name, Title) +VALUES + ('Amit Khanna', 'Manager'), + ('Anjali Gupta', 'Engineer'); diff --git a/Teil2/app/Dockerfile b/Teil2/app/Dockerfile new file mode 100644 index 0000000..29765d6 --- /dev/null +++ b/Teil2/app/Dockerfile @@ -0,0 +1,14 @@ +# Example of Dockerfile + +FROM python:3.8.5-alpine3.12 + +WORKDIR /app + +EXPOSE 5000 +ENV FLASK_APP=app.py + +COPY . /app +RUN pip install -r requirements.txt + +ENTRYPOINT [ "flask"] +CMD [ "run", "--host=0.0.0.0" ] diff --git a/Teil2/app/app.py b/Teil2/app/app.py new file mode 100644 index 0000000..4b9d90f --- /dev/null +++ b/Teil2/app/app.py @@ -0,0 +1,100 @@ +from flask import Flask, request, jsonify, render_template +import mysql.connector + +app = Flask(__name__) + +def employee_data(): + # Connect to MySQL database + conn = mysql.connector.connect( + host='db', # Use the IP address or hostname of the MySQL container + port='3306', # MySQL port + user='root', # MySQL username + password='root_password', # MySQL password + database='employees' # Name of the database + ) + + # Create a cursor object to execute SQL commands + cursor = conn.cursor() + + cursor.execute('SELECT Employee_Name, Title FROM employee_data') + results = cursor.fetchall() + cursor.close() + conn.close() + return results + +# Endpoint to add a new entry to the database +@app.route('/add', methods=['POST']) +def add_entry(): + # Connect to MySQL database + conn = mysql.connector.connect( + host='db', # Use the IP address or hostname of the MySQL container + port='3306', # MySQL port + user='root', # MySQL username + password='root_password', # MySQL password + database='employees' # Name of the database + ) + + # Create a cursor object to execute SQL commands + cursor = conn.cursor() + + # Get data from request + data = request.get_json() + employee_name = data.get('employee_name') + title = data.get('title') + + # SQL query to insert data into the employee_data table + sql_query = "INSERT INTO employee_data (Employee_Name, Title) VALUES (%s, %s)" + + try: + # Execute the SQL query + cursor.execute(sql_query, (employee_name, title)) + # Commit the transaction + conn.commit() + cursor.close() + conn.close() + return jsonify({'message': 'Entry added successfully'}), 200 + except Exception as e: + # Rollback in case of any error + conn.rollback() + cursor.close() + conn.close() + return jsonify({'error': str(e)}), 500 + +# Endpoint to list all entries in the database +@app.route('/entries', methods=['GET']) +def get_entries(): + # Connect to MySQL database + conn = mysql.connector.connect( + host='db', # Use the IP address or hostname of the MySQL container + port='3306', # MySQL port + user='root', # MySQL username + password='root_password', # MySQL password + database='employees' # Name of the database + ) + + # Create a cursor object to execute SQL commands + cursor = conn.cursor() + # SQL query to select all entries from the employee_data table + sql_query = "SELECT * FROM employee_data" + + # Execute the SQL query + cursor.execute(sql_query) + # Fetch all entries + entries = cursor.fetchall() + + # Convert entries to JSON format + entries_json = [{'Employee_Name': entry[0], 'Title': entry[1]} for entry in entries] + cursor.close() + conn.close() + return jsonify(entries_json), 200 + +@app.route('/') +def index(): + #return jsonify({'Employee Data': employee_data()}) + entries = employee_data() + return render_template('index.html', entries=entries) + #return "Hello world" + +if __name__ == '__main__': + #app.run(debug = True, host='0.0.0.0') + app.run() diff --git a/Teil2/app/requirements.txt b/Teil2/app/requirements.txt new file mode 100644 index 0000000..37806b7 --- /dev/null +++ b/Teil2/app/requirements.txt @@ -0,0 +1,2 @@ +Flask +mysql-connector diff --git a/Teil2/app/templates/index.html b/Teil2/app/templates/index.html new file mode 100644 index 0000000..2bbf22f --- /dev/null +++ b/Teil2/app/templates/index.html @@ -0,0 +1,37 @@ + + + + + + MySQL Database Entries + + + +

SQLite Database Entries

+ + + + + + + {% for entry in entries %} + + + + + + {% endfor %} +
NamePosition
{{ entry[0] }}{{ entry[1] }}
+ + + diff --git a/Teil2/compose.yml b/Teil2/compose.yml new file mode 100644 index 0000000..08f7ea7 --- /dev/null +++ b/Teil2/compose.yml @@ -0,0 +1,21 @@ +services: + app: + build: ./app + links: + - db + ports: + - "5000:5000" + depends_on: + - db + + db: + image: mysql:5.7 + ports: + - "3306:3306" + + environment: + MYSQL_ROOT_PASSWORD: root_password + volumes: + - ./db/init.sql:/docker-entrypoint-initdb.d/init.sql + - ./data:/var/lib/mysql + diff --git a/Teil2/db/init.sql b/Teil2/db/init.sql new file mode 100755 index 0000000..a6422a0 --- /dev/null +++ b/Teil2/db/init.sql @@ -0,0 +1,15 @@ +CREATE DATABASE employees; +USE employees; + + +CREATE TABLE employee_data ( + Employee_Name VARCHAR(50), + Title VARCHAR(50) +); + + +INSERT INTO employee_data + (Employee_Name, Title) +VALUES + ('Amit Khanna', 'Manager'), + ('Anjali Gupta', 'Engineer'); diff --git a/Teil2/main.c b/Teil2/main.c new file mode 100644 index 0000000..214cbc9 --- /dev/null +++ b/Teil2/main.c @@ -0,0 +1,71 @@ +#include +#include +#include +#include +#include +#include + +#define PORT 5000 +#define BUFFER_SIZE 1024 + +int main(int argc, char *argv[]) { + char *endpoint = ""; + char *data = ""; // Data to be sent in the POST request + if (argc > 1) { + endpoint = argv[1]; + } + if (argc > 2) { + data = argv[2]; + } + + char request[BUFFER_SIZE]; + snprintf(request, BUFFER_SIZE, "POST /%s HTTP/1.1\r\nHost: localhost:5000\r\nContent-Type: application/json\r\nContent-Length: %ld\r\n\r\n%s", endpoint, strlen(data), data); + + int sockfd; + struct sockaddr_in server_addr; + char buffer[BUFFER_SIZE]; + + // Create socket + sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (sockfd < 0) { + perror("Error in socket creation"); + exit(EXIT_FAILURE); + } + + // Set up server address + server_addr.sin_family = AF_INET; + server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); + server_addr.sin_port = htons(PORT); + + // Connect to server + if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { + perror("Error in connection"); + exit(EXIT_FAILURE); + } + + // Send HTTP request + if (send(sockfd, request, strlen(request), 0) != strlen(request)) { + perror("Error in sending request"); + exit(EXIT_FAILURE); + } + + // Receive data from server + ssize_t total_bytes_received = 0; + ssize_t bytes_received; + while ((bytes_received = recv(sockfd, buffer, BUFFER_SIZE, 0)) > 0) { + // Display received data + fwrite(buffer, 1, bytes_received, stdout); + total_bytes_received += bytes_received; + } + + if (bytes_received < 0) { + perror("Error in receiving data"); + exit(EXIT_FAILURE); + } + + // Close socket + close(sockfd); + + return 0; +} +