Change main.py to app.py. Maybe Gunicorn will like us now.
This commit is contained in:
parent
a9a4709a07
commit
8d41e75e93
33
app.py
Normal file
33
app.py
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
from flask import Flask, render_template, jsonify
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
|
||||||
|
app = Flask(__name__)
|
||||||
|
current_state = "red"
|
||||||
|
lock = threading.Lock()
|
||||||
|
|
||||||
|
def reset_to_red():
|
||||||
|
global current_state
|
||||||
|
time.sleep(10)
|
||||||
|
with lock:
|
||||||
|
current_state = "red"
|
||||||
|
|
||||||
|
@app.route("/")
|
||||||
|
def index():
|
||||||
|
return render_template("site_template.j2", state=current_state)
|
||||||
|
|
||||||
|
@app.route("/status")
|
||||||
|
def status():
|
||||||
|
return jsonify({"state": current_state})
|
||||||
|
|
||||||
|
@app.route("/toggle", methods=["POST"])
|
||||||
|
def toggle():
|
||||||
|
global current_state
|
||||||
|
with lock:
|
||||||
|
if current_state != "green":
|
||||||
|
current_state = "green"
|
||||||
|
threading.Thread(target=reset_to_red, daemon=True).start()
|
||||||
|
return jsonify({"state": current_state})
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
app.run(host="0.0.0.0", port=8000)
|
Loading…
x
Reference in New Issue
Block a user