From b9348c906dc9d7d95be503005baf76457c2bfce6 Mon Sep 17 00:00:00 2001 From: SpudGunMan Date: Wed, 5 Feb 2025 18:09:08 -0800 Subject: [PATCH] enhance better path handling setting for IP Address per https://github.com/SpudGunMan/meshing-around/issues/126 Co-Authored-By: mikecarper <135079168+mikecarper@users.noreply.github.com> --- etc/mesh_bot_w3.tmp | 23 +++++++++++++++++++++++ install.sh | 5 +++++ modules/web.py | 24 +++++++++++++++++++----- 3 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 etc/mesh_bot_w3.tmp diff --git a/etc/mesh_bot_w3.tmp b/etc/mesh_bot_w3.tmp new file mode 100644 index 0000000..3a42f10 --- /dev/null +++ b/etc/mesh_bot_w3.tmp @@ -0,0 +1,23 @@ +# /etc/systemd/system/mesh_bot_w3.service +# sudo systemctl daemon-reload +# sudo systemctl enable mesh_bot_w3.service +# sudo systemctl start mesh_bot_w3.service + +[Unit] +Description=MeshingAround-W3Server +After=network.target + +[Service] +Type=simple +User=pi +Group=pi +WorkingDirectory=/dir/ +ExecStart=python3 modules/web.py +ExecStop=pkill -f mesh_bot_w3.py + +# Disable Python's buffering of STDOUT and STDERR, so that output from the +# service shows up immediately in systemd's logs +Environment=PYTHONUNBUFFERED=1 + +Restart=on-failure +Type=notify #try simple if any problems diff --git a/install.sh b/install.sh index 45af5a9..918f050 100755 --- a/install.sh +++ b/install.sh @@ -80,6 +80,7 @@ sudo usermod -a -G bluetooth $USER cp etc/pong_bot.tmp etc/pong_bot.service cp etc/mesh_bot.tmp etc/mesh_bot.service cp etc/mesh_bot_reporting.tmp etc/mesh_bot_reporting.service +cp etc/mesh_bot_w3.tmp etc/mesh_bot_w3.service # generate config file, check if it exists if [[ -f config.ini ]]; then @@ -176,6 +177,7 @@ replace="s|/dir/|$program_path/|g" sed -i $replace etc/pong_bot.service sed -i $replace etc/mesh_bot.service sed -i $replace etc/mesh_bot_reporting.service +sed -i $replace etc/mesh_bot_w3.service # set the correct user in the service file? #ask if we should add a user for the bot @@ -207,10 +209,12 @@ replace="s|User=pi|User=$whoami|g" sed -i $replace etc/pong_bot.service sed -i $replace etc/mesh_bot.service sed -i $replace etc/mesh_bot_reporting.service +sed -i $replace etc/mesh_bot_w3.service replace="s|Group=pi|Group=$whoami|g" sed -i $replace etc/pong_bot.service sed -i $replace etc/mesh_bot.service sed -i $replace etc/mesh_bot_reporting.service +sed -i $replace etc/mesh_bot_w3.service printf "\n service files updated\n" if [[ $(echo "${bot}" | grep -i "^p") ]]; then @@ -325,6 +329,7 @@ exit 0 # sudo systemctl disable mesh_bot_reporting # sudo rm /etc/systemd/system/mesh_bot.service # sudo rm /etc/systemd/system/mesh_bot_reporting.service +# sudo rm /etc/systemd/system/mesh_bot_w3.service # sudo rm /etc/systemd/system/pong_bot.service # sudo systemctl daemon-reload # sudo systemctl reset-failed diff --git a/modules/web.py b/modules/web.py index a340622..f05ab65 100644 --- a/modules/web.py +++ b/modules/web.py @@ -7,17 +7,26 @@ import os import http.server +# Set the desired IP address +server_ip = '127.0.0.1' + # Set the port for the server PORT = 8420 -# set webRoot index.html location -webRoot = "etc/www" +# Generate with: openssl req -new -x509 -keyout server.pem -out server.pem -days 365 -nodes +SSL = False # Set to True to enable logging sdtout webServerLogs = False -# Generate with: openssl req -new -x509 -keyout server.pem -out server.pem -days 365 -nodes -SSL = False +# Determine the directory where this script is located. +script_dir = os.path.dirname(os.path.realpath(__file__)) + +# Go up one level from the modules directory to the project root. +project_root = os.path.abspath(os.path.join(script_dir, "..")) + +# Build the absolute path to the webRoot folder; to where index.html is located. +webRoot = os.path.join(project_root, "etc", "www") if SSL: import ssl @@ -43,7 +52,12 @@ if SSL: exit(1) httpd.socket = ctx.wrap_socket(httpd.socket, server_side=True) -print(f"Serving reports at http://localhost:{PORT} Press ^C to quit.\n\n") +# Create the HTTP server instance with the desired IP address +httpd = http.server.HTTPServer((server_ip, PORT), QuietHandler) + +# Print out the URL using the IP address stored in server_ip +print(f"Serving reports at http://{server_ip}:{PORT} Press ^C to quit.\n\n") + if not webServerLogs: print("Server Logs are disabled") # Serve forever, that is until the user interrupts the process