From 8ee85912cce3cfe6d75e8a1b6617614a49f9bf20 Mon Sep 17 00:00:00 2001 From: JohannesBOT Date: Tue, 5 Nov 2024 11:08:28 +0100 Subject: [PATCH] docker --- .gitignore | 3 +- Dockerfile | 6 +--- docker-compose.yml | 46 ++++++++++++++++++++++++ main.py | 4 +-- nginx/nginx.conf | 35 +++++++++++++++++++ requerments.txt => requirements.txt | 0 src/server/Dockerfile | 10 ++++++ src/server/main.py | 52 ++++++++++++++++++++++++++++ src/server/requirements.txt | 1 + src/{ => webScraper}/Spritpreise.py | 2 +- www/css/navigation.css | 0 www/css/style.css | 0 www/images/favicon.ico | Bin 0 -> 3996 bytes www/index.html | 30 ++++++++++++++++ www/js/main.js | 25 +++++++++++++ 15 files changed, 205 insertions(+), 9 deletions(-) create mode 100644 docker-compose.yml create mode 100644 nginx/nginx.conf rename requerments.txt => requirements.txt (100%) create mode 100644 src/server/Dockerfile create mode 100644 src/server/main.py create mode 100644 src/server/requirements.txt rename src/{ => webScraper}/Spritpreise.py (98%) create mode 100644 www/css/navigation.css create mode 100644 www/css/style.css create mode 100644 www/images/favicon.ico create mode 100644 www/index.html create mode 100644 www/js/main.js diff --git a/.gitignore b/.gitignore index ef6457b..b2f1736 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ test.* *.db db.* -*.log \ No newline at end of file +*.log +docker-compose.yml \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 6fcf8ac..c39fdd3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,10 @@ FROM python:3.10 -# Setze das Arbeitsverzeichnis im Container WORKDIR /usr/src/app -# Kopiere requirements.txt und installiere die Abhängigkeiten COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt -# Kopiere den restlichen App-Code COPY . . -# Standardkommando (kann in docker-compose überschrieben werden) -CMD ["python", "server.py"] +CMD ["python", "main.py"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..ea19eb8 --- /dev/null +++ b/docker-compose.yml @@ -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 \ No newline at end of file diff --git a/main.py b/main.py index 01046bf..c833cd7 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,7 @@ -from src.Spritpreise import Spritpreise +from src.webScraper.Spritpreise import Spritpreise from src.db import * if __name__ == "__main__": sprit = Spritpreise(location="Linnich", radius=30) sprit.setDbConnection(dbHost, dbPort, dbUser, dbPassword, dbType) - sprit.getAllPricesSchedule() \ No newline at end of file + #sprit.getAllPricesSchedule() \ No newline at end of file diff --git a/nginx/nginx.conf b/nginx/nginx.conf new file mode 100644 index 0000000..b9ea438 --- /dev/null +++ b/nginx/nginx.conf @@ -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; + } + } +} diff --git a/requerments.txt b/requirements.txt similarity index 100% rename from requerments.txt rename to requirements.txt diff --git a/src/server/Dockerfile b/src/server/Dockerfile new file mode 100644 index 0000000..c39fdd3 --- /dev/null +++ b/src/server/Dockerfile @@ -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"] diff --git a/src/server/main.py b/src/server/main.py new file mode 100644 index 0000000..833ea73 --- /dev/null +++ b/src/server/main.py @@ -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() \ No newline at end of file diff --git a/src/server/requirements.txt b/src/server/requirements.txt new file mode 100644 index 0000000..658130b --- /dev/null +++ b/src/server/requirements.txt @@ -0,0 +1 @@ +psycopg2 diff --git a/src/Spritpreise.py b/src/webScraper/Spritpreise.py similarity index 98% rename from src/Spritpreise.py rename to src/webScraper/Spritpreise.py index 1f5cfb6..11caf7b 100644 --- a/src/Spritpreise.py +++ b/src/webScraper/Spritpreise.py @@ -66,7 +66,7 @@ class Spritpreise: now = "" try: 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") except Exception as e: print(f"Error occurred: {e}") diff --git a/www/css/navigation.css b/www/css/navigation.css new file mode 100644 index 0000000..e69de29 diff --git a/www/css/style.css b/www/css/style.css new file mode 100644 index 0000000..e69de29 diff --git a/www/images/favicon.ico b/www/images/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..17c1bc0467bd30cb0e5850581c1c9432271591ee GIT binary patch literal 3996 zcmaJ@c|25Y*nVaj%nXe|V<|M4P$UXj!Wg_NS)#PqvL@NGZ!wm!WXV>PU5d9NSt8p^ zlL%qnv{(|FyfcN!7TLZtdjI+U_`Y*~=l9&teP8!=Kld`{3;;0b=3Fp<2l4y>2t#s` zg_-eA^lmgH?mTha(2`^2JV<2I&Ia_DknF`hSi_QnO$GWAok`~| zNlv?{VQ2lz9JxTrN{3}!u0!Xb&CxysuzSvVbJ@yPqQ*^v7hdVp)#wMbF1VgQ_(tW5 zkm19XvN?kZhk%OiQm>sr-I8 z7q2TAO9@raHB9(>mN;=NyhSE)%c$13xjM_tGQ2J|N`X&iCh13Cl$^v%iC`(1Q}y+Q z0jUVT=1DLSK=YAo*!Z!R=U)|N_K^pN$9Zn=uu{lw*g-n1=~d(VP-Q4mo$NhY%Lt}l zJrSw=A#R0*s>`kTQ9u7ZU{Q(bwKmkc>~oR*r5RIzdzN07u;uKS=-+W^??iU`WBqSk z-z_(enh(t`mNZAH26)IHSs!P*@tGwno?GM1LFHN5-rVf-T;rpYin4M?_?vCCeJu>f z)`wTeyvD;8gQIRBbQ3iRn$yu&rMuo+Sw=3%MBBwF?X)wEF&JF1{8E!6&@kv9Vf`@< zL8>{8Kw)$o-MXG@sku-K@~@)!>QA4^+!IF6DruQ{#XUIHa;&PS5)nYvc=j`D7I0N<#L(pt}a1&G!d=GZyolCsl)&6{LJ@PDc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c6|EDfeI)}nPlV$KB$9XPn9R%) z@P;vxQ%jmnt}54JMq@HUutJ9h6SXrIj_MWcb(%DCJT|IIy&UPiCKV(paFJa#baT01 z?s>5^do?KhAFtZv6Di9Eln_&m81KB%hAl4w?o5htMns zbf8^)-$|XUA0r{_NiH!r{CGzjex5gz)~7h2FlD?)g?%!PTbvFE!cQS~^A&EDbnl41 z$EI^Ws~LyYvp6O%Fc2e#K$I(@yOskGPhc;ksHJP_lBWdLy@z})Z<3hCFvBuNcF{b)b zZo`dB{d$AK;!b6DD_WzqjoL6t9=vd&K$~HMV&o|z5BJe@vW1{eyvrz2u0r5VRF^@#M(W7gUS8Ibe + + + + + + + + + Spritpreise + + + + +
+
+ +
+
+ +
+
+ +
+
+ + + + + \ No newline at end of file diff --git a/www/js/main.js b/www/js/main.js new file mode 100644 index 0000000..47f6345 --- /dev/null +++ b/www/js/main.js @@ -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} + } +}); \ No newline at end of file