From 8ccb7d21ddea652583bc59bf77e85a7f90a11b05 Mon Sep 17 00:00:00 2001 From: Helva Date: Sat, 14 Feb 2026 11:21:26 +0100 Subject: [PATCH] added redirect service --- README.md | 13 ++++++++----- face/etc/nginx/sites-available/face | 8 ++++++++ face/etc/systemd/system/redirect.service | 17 +++++++++++++++++ face/opt/face/face_server.py | 10 ++++++++++ face/opt/face/redirect_tethering_ip.py | 14 ++++++++++++++ 5 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 face/etc/systemd/system/redirect.service create mode 100755 face/opt/face/redirect_tethering_ip.py diff --git a/README.md b/README.md index 6c5511e..30a583b 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ We build a robot with raspberry and arduino. python3 -m venv /opt/face/venv /opt/face/venv/bin/pip install --upgrade pip - /opt/face/venv/bin/pip install fastapi uvicorn + /opt/face/venv/bin/pip install fastapi uvicorn netifaces ### Exchange Nginx Web-Folder sudo rm -r /var/www/html @@ -73,18 +73,21 @@ http://pi-ip/ curl -X POST http:///api/state -H "Content-Type: application/json" \ -d '{"emotion":"happy","blink":true,"intensity":0.85}' -### systemd Service +### systemd Services sudo ln -s /opt/helva-robot/face/etc/systemd/system/face.service /etc/systemd/system/ + sudo ln -s /opt/helva-robot/face/etc/systemd/system/redirect_to_tethering.service /etc/systemd/system/ - sudo chown -R www-data:www-data /opt/face - sudo chmod -R 755 /opt/face + sudo chown -R www-data:www-data /opt/face/*.py + sudo chmod -R 755 /opt/face/*.py -Start: +### Start: sudo systemctl daemon-reload sudo systemctl enable --now face.service sudo systemctl status face.service --no-pager + + ### Face Control UI http://localhost/control/ diff --git a/face/etc/nginx/sites-available/face b/face/etc/nginx/sites-available/face index d3b742c..d1b0048 100644 --- a/face/etc/nginx/sites-available/face +++ b/face/etc/nginx/sites-available/face @@ -2,6 +2,14 @@ server { listen 80; server_name _; + # redirect service to tethering IP for 'webview kiosk' -> mobile phone to face + location /redirect { + proxy_pass http://127.0.0.1:8000/redirect; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + } + # captive portal to face include /opt/helva-robot/drive/etc/nginx/snippets/captive-portal.conf; diff --git a/face/etc/systemd/system/redirect.service b/face/etc/systemd/system/redirect.service new file mode 100644 index 0000000..0e76c5e --- /dev/null +++ b/face/etc/systemd/system/redirect.service @@ -0,0 +1,17 @@ +[Unit] +Description=Redirect Tethering IP Service +After=network.target + +[Service] +WorkingDirectory=/opt/face +ExecStart=/opt/face/venv/bin/python -m uvicorn redirect_tethering_ip:app --host 127.0.0.1 --port 8000 --app-dir /opt/face +Restart=always +RestartSec=1 + +# Tipp: eigener User ist sauber, aber www-data geht auch. +User=www-data +Group=www-data + +[Install] +WantedBy=multi-user.target + diff --git a/face/opt/face/face_server.py b/face/opt/face/face_server.py index e03247c..31c49b0 100755 --- a/face/opt/face/face_server.py +++ b/face/opt/face/face_server.py @@ -66,6 +66,10 @@ async def broadcast() -> None: async def get_state(): return JSONResponse(STATE) +@app.get("/api/state") +async def get_state(): + return JSONResponse(STATE) + @app.post("/state") async def set_state(payload: Dict[str, Any]): @@ -73,6 +77,12 @@ async def set_state(payload: Dict[str, Any]): await broadcast() return JSONResponse({"ok": True, "state": STATE}) +@app.post("/api/state") +async def set_state(payload: Dict[str, Any]): + merge_state(payload) + await broadcast() + return JSONResponse({"ok": True, "state": STATE}) + @app.get("/events") async def events(request: Request): diff --git a/face/opt/face/redirect_tethering_ip.py b/face/opt/face/redirect_tethering_ip.py new file mode 100755 index 0000000..853f2c5 --- /dev/null +++ b/face/opt/face/redirect_tethering_ip.py @@ -0,0 +1,14 @@ +from fastapi import FastAPI +from fastapi.responses import RedirectResponse +import netifaces + +app = FastAPI() + +@app.get("/redirect") +def redirect_to_pi(): + # gather tethering IP + interface = 'usb0' + ip_address = netifaces.ifaddresses(interface)[netifaces.AF_INET][0]['addr'] + + # relocate to current tethering-IP + return RedirectResponse(url=f'http://{ip_address}:80')