round 3
This commit is contained in:
38
main.py
38
main.py
@@ -1,9 +1,12 @@
|
||||
from flask import Flask
|
||||
from flask import Flask, request
|
||||
from flask_cors import CORS
|
||||
|
||||
from api.child_api import child_api
|
||||
from api.image_api import image_api
|
||||
from api.reward_api import reward_api
|
||||
from api.task_api import task_api
|
||||
from api.image_api import image_api
|
||||
from events.broadcaster import Broadcaster
|
||||
from events.sse import sse_response_for_user, send_to_user
|
||||
|
||||
app = Flask(__name__)
|
||||
#CORS(app, resources={r"/api/*": {"origins": ["http://localhost:3000", "http://localhost:5173"]}})
|
||||
@@ -13,7 +16,36 @@ app.register_blueprint(task_api)
|
||||
app.register_blueprint(image_api)
|
||||
CORS(app)
|
||||
|
||||
@app.route("/events")
|
||||
def events():
|
||||
# Authenticate user or read a token
|
||||
user_id = request.args.get("user_id")
|
||||
|
||||
if not user_id:
|
||||
return {"error": "Missing user_id"}, 400
|
||||
|
||||
|
||||
return sse_response_for_user(user_id)
|
||||
|
||||
|
||||
@app.route("/notify/<user_id>")
|
||||
def notify_user(user_id):
|
||||
# Example trigger
|
||||
send_to_user(user_id, {
|
||||
"type": "notification",
|
||||
"message": f"Hello {user_id}, this is a private message!"
|
||||
})
|
||||
|
||||
return {"status": "sent"}
|
||||
|
||||
def start_background_threads():
|
||||
broadcaster = Broadcaster()
|
||||
broadcaster.daemon = True
|
||||
broadcaster.start()
|
||||
|
||||
# Initialize background workers on server start
|
||||
start_background_threads()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=False, host='0.0.0.0', port=5000)
|
||||
app.run(debug=False, host='0.0.0.0', port=5000, threaded=True)
|
||||
Reference in New Issue
Block a user