This commit is contained in:
Pascal 2025-04-23 16:14:24 +02:00
parent 7c4b8214ca
commit c5c10416c3
7 changed files with 183 additions and 0 deletions

17
Dockerfile Normal file
View File

@ -0,0 +1,17 @@
FROM alpine/sqlite
WORKDIR /app
RUN sqlite3 chatroom.db -cmd 'create table messages (id INTEGER PRIMARY KEY NOT NULL, message TEXT NOT NULL, timestamp TEXT NOT NULL)'
FROM python:3.10-slim
WORKDIR /app
COPY --from=0 /app/chatroom.db ./
COPY static/ static/
COPY index.html webserver_mit_db.py ./
EXPOSE 8080
# Starte den Python-Server
CMD ["python", "webserver_mit_db.py"]

BIN
chatroom.db Normal file

Binary file not shown.

37
index.html Normal file
View File

@ -0,0 +1,37 @@
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8" />
<title>twat</title>
<meta name="viewport" content="width=device-width,initial-scale=1" />
<link rel="icon" href="static/logo.png">
<link rel="stylesheet" type="text/css" href="static/styles.css">
<script src="static/htmx.js"></script>
</head>
<body>
<div class="out-container">
<div class="header-container">
<img src="static/logo.png" height="100px">
<h1>Twat</h1>
</div>
<form hx-include="find textarea" hx-post="/post" hx-target="#posts" hx-swap="afterbegin">
<textarea id="post-box" name="data"></textarea>
<button type="submit"> send </button>
</form>
<div id="posts" hx-get="/post" hx-trigger="load" hx-swap="innerHTML">
<div class="post-container">
<p>text in box</p>
<sup>sent: 12:13</sup>
</div>
<div class="post-container">
<p>text in box2</p>
<sup>sent: 12:18</sup>
</div>
<div class="post-container">
<p>content</p>
<sup>sent: sent Date</sup>
</div>
</div>
</div>
</body>
</html>

1
static/htmx.js Normal file

File diff suppressed because one or more lines are too long

BIN
static/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

29
static/styles.css Normal file
View File

@ -0,0 +1,29 @@
h1 {
text-align: center;
}
.out-container {
max-width: 500px;
margin: auto;
}
.header-container {
display: flex;
justify-content: center;
}
textarea {
width: 100%;
}
button {
width: 100%;
}
.post-container {
border: solid 2px;
border-radius: 5px;
padding: 1em;
margin-top: 1em;
}

99
webserver_mit_db.py Normal file
View File

@ -0,0 +1,99 @@
import os
import sqlite3
from http.server import SimpleHTTPRequestHandler, HTTPServer
from urllib.parse import parse_qs
from datetime import datetime
def get_db_connection():
conn = sqlite3.connect('chatroom.db')
conn.row_factory = sqlite3.Row
return conn
class MyServer(SimpleHTTPRequestHandler):
def do_GET(self):
if self.path == "/":
self.handle_index()
elif self.path.startswith('/static/'):
self.handle_static_files()
elif self.path == "/post":
self.do_GET_posts()
else:
self.send_response(404)
self.end_headers()
def handle_index(self):
with open('index.html', 'r') as file:
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(file.read().encode())
def do_GET_posts(self):
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute("SELECT message, timestamp FROM messages ORDER BY id DESC LIMIT 10")
rows = cursor.fetchall()
conn.close()
posts_html = "".join(
f"""
<div class="post-container">
<p>{row[0]}</p>
<sup>sent: {row[1]}</sup>
</div>
""" for row in rows
)
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(posts_html.encode())
def do_POST(self):
if self.path == "/post":
self.handle_post_request()
def handle_post_request(self):
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
form = parse_qs(post_data.decode())
message = form.get('data', [''])[0]
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute("INSERT INTO messages (message, timestamp) VALUES (?, ?)", (message, timestamp))
conn.commit()
conn.close()
self.send_response(200)
self.send_header("Content-Type", "text/html")
self.end_headers()
self.wfile.write(f"""
<div class="post-container">
<p>{message}</p>
<sup>sent: {timestamp}</sup>
</div>
""".encode())
def handle_static_files(self):
static_dir = os.path.join(os.getcwd(), 'static')
file_path = os.path.join(static_dir, self.path[8:])
if os.path.exists(file_path):
content_type = self.guess_type(file_path)
with open(file_path, 'rb') as file:
content = file.read()
self.send_response(200)
self.send_header('Content-Type', content_type)
self.end_headers()
self.wfile.write(content)
else:
self.send_response(404)
self.end_headers()
if __name__ == "__main__":
server_address = ('', 8080)
httpd = HTTPServer(server_address, MyServer)
print("Server gestartet auf http://localhost:8080")
httpd.serve_forever()