Skip to content

Commit

Permalink
use FastAPI as webserver
Browse files Browse the repository at this point in the history
  • Loading branch information
yankee42 committed Dec 29, 2021
1 parent 9b21a7a commit 775fd0a
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 26 deletions.
17 changes: 1 addition & 16 deletions openwb-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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..."
Expand Down Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions packages/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -155,6 +156,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:
Expand Down
1 change: 1 addition & 0 deletions packages/web/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from web._server import run_webserver
30 changes: 30 additions & 0 deletions packages/web/_server.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 0 additions & 10 deletions runs/atreboot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
File renamed without changes.

0 comments on commit 775fd0a

Please sign in to comment.