Skip to content

Commit 775fd0a

Browse files
committed
use FastAPI as webserver
1 parent 9b21a7a commit 775fd0a

File tree

7 files changed

+36
-26
lines changed

7 files changed

+36
-26
lines changed

openwb-install.sh

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ echo "installing openWB 2 into \"${OPENWBBASEDIR}\""
55

66
echo "install required packages..."
77
apt-get update
8-
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
8+
apt-get -q -y install vim bc jq git mosquitto mosquitto-clients socat python3-pip sshpass
99
echo "done"
1010

1111
echo "check for initial git clone..."
@@ -71,21 +71,6 @@ echo "mosquitto done"
7171
# echo "EXTRA_OPTS=\"-L 0\"" >> /etc/default/cron
7272
# fi
7373

74-
# apache
75-
echo -n "replacing apache default page..."
76-
sudo cp ${OPENWBBASEDIR}/index.html /var/www/html/index.html
77-
echo "done"
78-
echo -n "fix upload limit..."
79-
if [ -d "/etc/php/7.3/" ]; then
80-
sudo /bin/su -c "echo 'upload_max_filesize = 300M' > /etc/php/7.3/apache2/conf.d/20-uploadlimit.ini"
81-
sudo /bin/su -c "echo 'post_max_size = 300M' >> /etc/php/7.3/apache2/conf.d/20-uploadlimit.ini"
82-
echo "done (OS Buster)"
83-
elif [ -d "/etc/php/7.4/" ]; then
84-
sudo /bin/su -c "echo 'upload_max_filesize = 300M' > /etc/php/7.4/apache2/conf.d/20-uploadlimit.ini"
85-
sudo /bin/su -c "echo 'post_max_size = 300M' >> /etc/php/7.4/apache2/conf.d/20-uploadlimit.ini"
86-
echo "done (OS Bullseye)"
87-
fi
88-
8974
echo "installing python requirements..."
9075
sudo pip install -r ${OPENWBBASEDIR}/requirements.txt
9176

packages/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import traceback
88
from threading import Thread
99

10+
import web
1011
from modules import loadvars
1112
from modules import configuration
1213
from helpermodules import update_config
@@ -155,6 +156,7 @@ def repeated_handler_call():
155156

156157

157158
try:
159+
Thread(target=web.run_webserver).start()
158160
# Regelung erst starten, wenn atreboot.sh fertig ist.
159161
MainLogger().debug("Warten auf das Ende des Boot-Prozesses")
160162
while os.path.isfile(os.path.dirname(os.path.abspath(__file__)) + "/../ramdisk/bootdone") is False:

packages/web/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from web._server import run_webserver

packages/web/_server.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import time
2+
from pathlib import Path
3+
4+
import fastapi.responses
5+
import uvicorn
6+
from fastapi import FastAPI
7+
from fastapi.staticfiles import StaticFiles
8+
from starlette import status
9+
from starlette.responses import FileResponse
10+
11+
12+
def run_webserver():
13+
openwb_root = Path(__file__).parents[2]
14+
15+
app = FastAPI()
16+
17+
@app.get("/")
18+
async def root():
19+
return fastapi.responses.RedirectResponse("/web/", status.HTTP_303_SEE_OTHER)
20+
21+
@app.get("/web/")
22+
async def web_root():
23+
return FileResponse(openwb_root.joinpath("web", "themes", "standard", "theme.html"))
24+
25+
@app.get("/sample")
26+
async def sample():
27+
return f"Hello world! Time is {time.time()}"
28+
29+
app.mount("/web", StaticFiles(directory=openwb_root / "web", html=True), name="static")
30+
uvicorn.run(app, host="0.0.0.0", port=80)

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ paho_mqtt==1.6.1
33
pymodbus==2.5.2
44
pytest==6.2.5
55
requests_mock==1.9.3
6+
fastapi==0.70.1
7+
uvicorn==0.16.0

runs/atreboot.sh

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,6 @@ echo "LAN/WLAN..."
4545
# alpha image restricted to LAN only
4646
sudo ifconfig eth0:0 192.168.193.5 netmask 255.255.255.0 up
4747

48-
# check for apache configuration
49-
echo "apache..."
50-
if grep -Fxq "AllowOverride" /etc/apache2/sites-available/000-default.conf
51-
then
52-
echo "...ok"
53-
else
54-
sudo cp ${OPENWBBASEDIR}/data/config/000-default.conf /etc/apache2/sites-available/
55-
echo "...changed"
56-
fi
57-
5848
# check for needed packages
5949
echo "apt packages..."
6050
# nothing here yet, all in install.sh

0 commit comments

Comments
 (0)