This commit is contained in:
2024-11-05 11:08:28 +01:00
parent 32d7f8a8f5
commit 8ee85912cc
15 changed files with 205 additions and 9 deletions

1
.gitignore vendored
View File

@@ -3,3 +3,4 @@ test.*
*.db *.db
db.* db.*
*.log *.log
docker-compose.yml

View File

@@ -1,14 +1,10 @@
FROM python:3.10 FROM python:3.10
# Setze das Arbeitsverzeichnis im Container
WORKDIR /usr/src/app WORKDIR /usr/src/app
# Kopiere requirements.txt und installiere die Abhängigkeiten
COPY requirements.txt . COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt RUN pip install --no-cache-dir -r requirements.txt
# Kopiere den restlichen App-Code
COPY . . COPY . .
# Standardkommando (kann in docker-compose überschrieben werden) CMD ["python", "main.py"]
CMD ["python", "server.py"]

46
docker-compose.yml Normal file
View File

@@ -0,0 +1,46 @@
services:
python_app:
build:
context: .
dockerfile: Dockerfile
container_name: spritpreise_app
deploy:
resources:
limits:
memory: 500M
volumes:
- ./:/usr/src/app
networks:
- app-network
python_server:
build:
context: ./src/server
dockerfile: Dockerfile
container_name: spritpreise_python_server
deploy:
resources:
limits:
memory: 500M
volumes:
- ./src/server:/usr/src/app
networks:
- app-network
webserver:
image: nginx:latest
container_name: spritpreise_nginx_webserver
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./:/opt
ports:
- "6342:6342"
networks:
- app-network
networks:
app-network:
driver: bridge

View File

@@ -1,7 +1,7 @@
from src.Spritpreise import Spritpreise from src.webScraper.Spritpreise import Spritpreise
from src.db import * from src.db import *
if __name__ == "__main__": if __name__ == "__main__":
sprit = Spritpreise(location="Linnich", radius=30) sprit = Spritpreise(location="Linnich", radius=30)
sprit.setDbConnection(dbHost, dbPort, dbUser, dbPassword, dbType) sprit.setDbConnection(dbHost, dbPort, dbUser, dbPassword, dbType)
sprit.getAllPricesSchedule() #sprit.getAllPricesSchedule()

35
nginx/nginx.conf Normal file
View File

@@ -0,0 +1,35 @@
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 6342;
server_name localhost;
root /opt/www;
index index.html;
location ~* \.(html|htm|css|js|png|jpg|jpeg|gif|ico|svg)$ {
expires 1d;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
try_files $uri $uri/ =404;
}
location / {
try_files $uri $uri/ /index.html; # Fallback zu index.html für SPA (Single-Page Applications)
}
location ~ /\.ht {
deny all;
}
}
}

10
src/server/Dockerfile Normal file
View File

@@ -0,0 +1,10 @@
FROM python:3.10
WORKDIR /usr/src/app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "main.py"]

52
src/server/main.py Normal file
View File

@@ -0,0 +1,52 @@
import datetime
from http.server import HTTPServer, BaseHTTPRequestHandler
import json
from threading import Thread
from src.db import *
import psycopg2
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def __writeLog(self, text: str, printOnConsole=True):
now = datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S")
try:
with open(f"spritpreise.log", "a") as logs:
logs.write(f"[{now}] {text}\n")
except Exception as e:
print(f"Error occurred: {e}")
if printOnConsole:
print(f"[{now}] {text}\n")
def sendResponse(self, responseCode=200, responseText="OK"):
self.send_response(responseCode)
self.send_header('Content-type', 'application/json')
self.end_headers()
response = {"message": responseText}
self.wfile.write(json.dumps(response).encode())
def do_POST(self):
try:
content_length = int(self.headers['Content-Length'])
data = json.loads(self.rfile.read(content_length))
self.__writeLog(f"Received data: {data}")
Thread(target=self.executeCode, args=(data,)).start()
self.sendResponse()
except Exception as e:
self.__writeLog(f"Error occurred: {e}")
self.sendResponse(500, str(e))
def executeCode(self, data):
self.sendResponse()
if __name__ == "__main__":
port = 9045
server_address = ('', port)
httpd = HTTPServer(server_address, SimpleHTTPRequestHandler)
print(f"Server running on port {port}")
httpd.serve_forever()

View File

@@ -0,0 +1 @@
psycopg2

View File

@@ -66,7 +66,7 @@ class Spritpreise:
now = "" now = ""
try: try:
now = datetime.now().strftime("%Y/%m/%d %H:%M:%S") now = datetime.now().strftime("%Y/%m/%d %H:%M:%S")
with open(f"{self.currentFolder}\\spritpreise.log", "a") as logs: with open(os.path.join(self.currentFolder, "spritpreise.log"), "a") as logs:
logs.write(f"[{now}] {text}\n") logs.write(f"[{now}] {text}\n")
except Exception as e: except Exception as e:
print(f"Error occurred: {e}") print(f"Error occurred: {e}")

0
www/css/navigation.css Normal file
View File

0
www/css/style.css Normal file
View File

BIN
www/images/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

30
www/index.html Normal file
View File

@@ -0,0 +1,30 @@
<!doctype html>
<html lang="de" >
<head>
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<link href='https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css' rel='stylesheet' integrity='sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9' crossorigin='anonymous'>
<link rel='stylesheet' href='css/navigation.css'>
<link rel="stylesheet" href="css/style.css">
<link rel='icon' href='images/favicon.ico' type='image/x-icon'>
<title>Spritpreise</title>
</head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js"></script>
<body>
<div class="wrapper">
<header>
</header>
<div class="pageMain">
<canvas id="myChart" style="width:100%;max-width:700px"></canvas>
</div>
<footer>
</footer>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
<script src="js/main.js"></script>
</body>
</html>

25
www/js/main.js Normal file
View File

@@ -0,0 +1,25 @@
// https://www.w3schools.com/ai/ai_chartjs.asp
const xValues = [100,200,300,400,500,600,700,800,900,1000];
new Chart("myChart", {
type: "line",
data: {
labels: xValues,
datasets: [{
data: [860,1140,1060,1060,1070,1110,1330,2210,7830,2478],
borderColor: "red",
fill: false
},{
data: [1600,1700,1700,1900,2000,2700,4000,5000,6000,7000],
borderColor: "green",
fill: false
},{
data: [300,700,2000,5000,6000,4000,2000,1000,200,100],
borderColor: "blue",
fill: false
}]
},
options: {
legend: {display: false}
}
});