@@ -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" ] |
@@ -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() |
@@ -0,0 +1,2 @@ | |||
Flask | |||
mysql-connector |
@@ -0,0 +1,37 @@ | |||
<!DOCTYPE html> | |||
<html lang="en"> | |||
<head> | |||
<meta charset="UTF-8"> | |||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |||
<title>MySQL Database Entries</title> | |||
<style> | |||
table { | |||
border-collapse: collapse; | |||
width: 100%; | |||
} | |||
th, td { | |||
border: 1px solid #dddddd; | |||
text-align: left; | |||
padding: 8px; | |||
} | |||
</style> | |||
</head> | |||
<body> | |||
<h1>SQLite Database Entries</h1> | |||
<table> | |||
<tr> | |||
<th>Name</th> | |||
<th>Position</th> | |||
<!-- Add more table headers if needed --> | |||
</tr> | |||
{% for entry in entries %} | |||
<tr> | |||
<td>{{ entry[0] }}</td> | |||
<td>{{ entry[1] }}</td> | |||
<!-- Access more columns if needed --> | |||
</tr> | |||
{% endfor %} | |||
</table> | |||
</body> | |||
</html> | |||
@@ -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 |
@@ -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'); |
@@ -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" ] |
@@ -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() |
@@ -0,0 +1,2 @@ | |||
Flask | |||
mysql-connector |
@@ -0,0 +1,37 @@ | |||
<!DOCTYPE html> | |||
<html lang="en"> | |||
<head> | |||
<meta charset="UTF-8"> | |||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |||
<title>MySQL Database Entries</title> | |||
<style> | |||
table { | |||
border-collapse: collapse; | |||
width: 100%; | |||
} | |||
th, td { | |||
border: 1px solid #dddddd; | |||
text-align: left; | |||
padding: 8px; | |||
} | |||
</style> | |||
</head> | |||
<body> | |||
<h1>SQLite Database Entries</h1> | |||
<table> | |||
<tr> | |||
<th>Name</th> | |||
<th>Position</th> | |||
<!-- Add more table headers if needed --> | |||
</tr> | |||
{% for entry in entries %} | |||
<tr> | |||
<td>{{ entry[0] }}</td> | |||
<td>{{ entry[1] }}</td> | |||
<!-- Access more columns if needed --> | |||
</tr> | |||
{% endfor %} | |||
</table> | |||
</body> | |||
</html> | |||
@@ -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 | |||
@@ -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'); |
@@ -0,0 +1,71 @@ | |||
#include <stdio.h> | |||
#include <stdlib.h> | |||
#include <string.h> | |||
#include <unistd.h> | |||
#include <arpa/inet.h> | |||
#include <sys/socket.h> | |||
#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; | |||
} | |||