From 4432049d9ae5a54a99ebb543c457ea8236d14e4e Mon Sep 17 00:00:00 2001 From: Kevin Midboe Date: Sun, 20 Apr 2025 01:14:36 +0200 Subject: [PATCH] also let server serve www directory for frontend --- .gitignore | 3 ++- server/main.py | 35 ++++++++++++++++++++--------------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 4bcff75..afb390a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .DS_Store -frontend/nodes_modules +frontend/.env +frontend/node_modules/ frontend/dist server/lights.db diff --git a/server/main.py b/server/main.py index d4bbf69..09c3bc1 100644 --- a/server/main.py +++ b/server/main.py @@ -3,15 +3,19 @@ import argparse import RPi.GPIO as GPIO from light import Tower import database -from flask import Flask, jsonify, request +from flask import Flask, jsonify, request, send_from_directory +import os # Pins configuration (order: green, orange, red) GPIO_PINS = [{'name': 'green', 'pin': 22}, {'name': 'orange', 'pin': 17}, {'name': 'red', 'pin': 27}] -# Create a Flask app -app = Flask(__name__) +# Path to static files directory +STATIC_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'www') + +# Create a Flask app and set static_folder +app = Flask(__name__, static_folder=STATIC_DIR, static_url_path='') # Initialize the database database.ensure_database() @@ -32,39 +36,40 @@ def add_cors_headers(response): def set_state(): data = request.data.decode('utf-8') state = [int(n.strip()) for n in data.split(',') if n.strip().isdigit()] - """Endpoint to get the current state of all lights.""" tower.green.on() if state[0] else tower.green.off() tower.orange.on() if state[1] else tower.orange.off() tower.red.on() if state[2] else tower.red.off() - return jsonify(state) @app.route('/api/state', methods=['GET']) def get_state(): - """Endpoint to get the current state of all lights.""" state = database.get_all_states() return jsonify(state) @app.route('/api/toggle/', methods=['POST']) def toggle_light(color): - """Endpoint to toggle the state of a specified light.""" if color not in ['green', 'orange', 'red']: return jsonify({'error': 'Invalid color'}), 404 - - # Get the light object based on the color light = getattr(tower, color) - - # Toggle light state (on/off) new_state = GPIO.LOW if light.state == GPIO.HIGH else GPIO.HIGH - light._set(new_state) # Update the light state in the system - + light._set(new_state) return jsonify({color: new_state}) +# Serve static files from www/ +@app.route('/') +def serve_index(): + return send_from_directory(STATIC_DIR, 'index.html') + + +@app.route('/') +def serve_static(path): + return send_from_directory(STATIC_DIR, path) + + def run(tower): - """Run the tower with manual light control.""" try: time.sleep(0.5) while True: @@ -72,7 +77,6 @@ def run(tower): tower.green.on() tower.red.off() tower.orange.off() - time.sleep(10) except KeyboardInterrupt: print("\nInterrupted by user. Cleaning up...") @@ -98,3 +102,4 @@ def main(): if __name__ == '__main__': main() +