From 3c2ba0bb797a76110ea30671cabe87ee3ae9c8c2 Mon Sep 17 00:00:00 2001 From: Yannik Hampe Date: Wed, 29 Dec 2021 12:37:24 +0100 Subject: [PATCH] use FastAPI as webserver --- openwb-install.sh | 17 +---------- packages/main.py | 2 ++ packages/web/__init__.py | 1 + packages/web/_server.py | 30 +++++++++++++++++++ requirements.txt | 2 ++ runs/atreboot.sh | 10 ------- web/themes/standard/{theme.php => theme.html} | 0 7 files changed, 36 insertions(+), 26 deletions(-) create mode 100644 packages/web/__init__.py create mode 100644 packages/web/_server.py rename web/themes/standard/{theme.php => theme.html} (100%) diff --git a/openwb-install.sh b/openwb-install.sh index 8653946130..0c38271c95 100755 --- a/openwb-install.sh +++ b/openwb-install.sh @@ -5,7 +5,7 @@ echo "installing openWB 2 into \"${OPENWBBASEDIR}\"" echo "install required packages..." apt-get update -apt-get -q -y install vim bc apache2 php php-gd php-curl php-xml php-json libapache2-mod-php jq git mosquitto mosquitto-clients socat python3-pip sshpass +apt-get -q -y install vim bc jq git mosquitto mosquitto-clients socat python3-pip sshpass echo "done" echo "check for initial git clone..." @@ -71,21 +71,6 @@ echo "mosquitto done" # echo "EXTRA_OPTS=\"-L 0\"" >> /etc/default/cron # fi -# apache -echo -n "replacing apache default page..." -sudo cp ${OPENWBBASEDIR}/index.html /var/www/html/index.html -echo "done" -echo -n "fix upload limit..." -if [ -d "/etc/php/7.3/" ]; then - sudo /bin/su -c "echo 'upload_max_filesize = 300M' > /etc/php/7.3/apache2/conf.d/20-uploadlimit.ini" - sudo /bin/su -c "echo 'post_max_size = 300M' >> /etc/php/7.3/apache2/conf.d/20-uploadlimit.ini" - echo "done (OS Buster)" -elif [ -d "/etc/php/7.4/" ]; then - sudo /bin/su -c "echo 'upload_max_filesize = 300M' > /etc/php/7.4/apache2/conf.d/20-uploadlimit.ini" - sudo /bin/su -c "echo 'post_max_size = 300M' >> /etc/php/7.4/apache2/conf.d/20-uploadlimit.ini" - echo "done (OS Bullseye)" -fi - echo "installing python requirements..." sudo pip install -r ${OPENWBBASEDIR}/requirements.txt diff --git a/packages/main.py b/packages/main.py index 8b7dae19e2..60507833ae 100755 --- a/packages/main.py +++ b/packages/main.py @@ -7,6 +7,7 @@ import traceback from threading import Thread +import web from modules import loadvars from modules import configuration from helpermodules import update_config @@ -156,6 +157,7 @@ def repeated_handler_call(): try: + Thread(target=web.run_webserver).start() # Regelung erst starten, wenn atreboot.sh fertig ist. MainLogger().debug("Warten auf das Ende des Boot-Prozesses") while os.path.isfile(os.path.dirname(os.path.abspath(__file__)) + "/../ramdisk/bootdone") is False: diff --git a/packages/web/__init__.py b/packages/web/__init__.py new file mode 100644 index 0000000000..9ce57a41f3 --- /dev/null +++ b/packages/web/__init__.py @@ -0,0 +1 @@ +from web._server import run_webserver diff --git a/packages/web/_server.py b/packages/web/_server.py new file mode 100644 index 0000000000..0fe48cb93c --- /dev/null +++ b/packages/web/_server.py @@ -0,0 +1,30 @@ +import time +from pathlib import Path + +import fastapi.responses +import uvicorn +from fastapi import FastAPI +from fastapi.staticfiles import StaticFiles +from starlette import status +from starlette.responses import FileResponse + + +def run_webserver(): + openwb_root = Path(__file__).parents[2] + + app = FastAPI() + + @app.get("/") + async def root(): + return fastapi.responses.RedirectResponse("/web/", status.HTTP_303_SEE_OTHER) + + @app.get("/web/") + async def web_root(): + return FileResponse(openwb_root.joinpath("web", "themes", "standard", "theme.html")) + + @app.get("/sample") + async def sample(): + return f"Hello world! Time is {time.time()}" + + app.mount("/web", StaticFiles(directory=openwb_root / "web", html=True), name="static") + uvicorn.run(app, host="0.0.0.0", port=80) diff --git a/requirements.txt b/requirements.txt index 7c128a6eb3..8fdb76a3d9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,5 @@ paho_mqtt==1.6.1 pymodbus==2.5.2 pytest==6.2.5 requests_mock==1.9.3 +fastapi==0.70.1 +uvicorn==0.16.0 diff --git a/runs/atreboot.sh b/runs/atreboot.sh index b47bb97e9d..473e11a562 100755 --- a/runs/atreboot.sh +++ b/runs/atreboot.sh @@ -45,16 +45,6 @@ echo "LAN/WLAN..." # alpha image restricted to LAN only sudo ifconfig eth0:0 192.168.193.5 netmask 255.255.255.0 up -# check for apache configuration -echo "apache..." -if grep -Fxq "AllowOverride" /etc/apache2/sites-available/000-default.conf -then - echo "...ok" -else - sudo cp ${OPENWBBASEDIR}/data/config/000-default.conf /etc/apache2/sites-available/ - echo "...changed" -fi - # check for needed packages echo "apt packages..." # nothing here yet, all in install.sh diff --git a/web/themes/standard/theme.php b/web/themes/standard/theme.html similarity index 100% rename from web/themes/standard/theme.php rename to web/themes/standard/theme.html