-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathapp.py
More file actions
64 lines (47 loc) · 1.66 KB
/
app.py
File metadata and controls
64 lines (47 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import logging
import os
from flask import Flask, jsonify, render_template, request
from flask_socketio import SocketIO
APP_PORT = os.getenv("APP_PORT", "3001")
PUBSUB_NAME = os.getenv("PUBSUB_NAME", "orders")
TOPIC_NAME = os.getenv("TOPIC_NAME", "notifications")
app = Flask(__name__)
socketio = SocketIO(app)
@socketio.on('connect')
def socket_connect():
print('connected', flush=True)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/dapr/subscribe', methods=['GET'])
def subscribe():
"""Returns the list of topics the app wants to subscribe to.
Ref: https://docs.dapr.io/reference/api/pubsub_api/#provide-a-route-for-dapr-to-discover-topic-subscriptions"""
subs = [
{
'pubsubname': PUBSUB_NAME,
'topic': TOPIC_NAME,
'route': TOPIC_NAME,
},
]
return jsonify(subs)
@app.route('/' + TOPIC_NAME, methods=['POST', 'PUT'])
def topic_notifications():
"""Handles notification events from the Dapr pubsub component.
Ref: https://docs.dapr.io/reference/api/pubsub_api/#provide-routes-for-dapr-to-deliver-topic-events"""
logging.info(f"Received notification: {request.json}")
event = request.json
socketio.emit('message', event)
return '', 200
@app.route("/healthz", methods=["GET"])
def hello():
return f"Hello from {__name__}", 200
def main():
logging.info("Starting Flask app...")
socketio.run(app, port=APP_PORT, allow_unsafe_werkzeug=True)
if __name__ == "__main__":
logging.basicConfig(
format='%(asctime)s.%(msecs)03d %(levelname)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
level=logging.INFO)
main()