-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublisher.py
executable file
·71 lines (56 loc) · 2.03 KB
/
publisher.py
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
65
66
67
68
69
70
71
#!/usr/bin/env python3
# pub.py
from flask import Flask, render_template, url_for
# The publisher and hub are combined in the same process because it's easier.
# There's no need to do so, though.
from flask_websub.publisher import publisher, init_publisher
from flask_websub.hub import Hub, SQLite3HubStorage
from celery import Celery
# app & celery
app = Flask(__name__)
app.config['SERVER_NAME'] = 'pub.websub.local'
celery = Celery('publisher', broker='redis://localhost:6379')
# initialise publisher
init_publisher(app)
# initialise hub
#
# PUBLISH_SUPPORTED is not recommended in production, as it just accepts any
# link without validation, but it's but nice for testing.
app.config['PUBLISH_SUPPORTED'] = True
# we could also have passed in just PUBLISH_SUPPORTED, but this is probably a
# nice pattern for your app:
hub = Hub(SQLite3HubStorage('publisher.sqlite3'), celery, **app.config)
app.register_blueprint(hub.build_blueprint(url_prefix='/hub'))
def validate_topic_existence(callback_url, topic_url, *args):
with app.app_context():
if topic_url.startswith('http://pub.websub.local/'):
return # pass validation
if topic_url != url_for('md', _external=True):
return "Topic not allowed"
hub.register_validator(validate_topic_existence)
hub.schedule_cleanup() # cleanup expired subscriptions once a day, by default
mdversion = 1
@app.before_first_request
def cleanup():
# or just cleanup manually at some point
hub.cleanup_expired_subscriptions.delay()
@app.route('/')
@publisher()
def root():
msg = "Publisher home"
return render_template('publisher.html', message=msg)
@app.route('/md')
@publisher()
def md():
md = "[pub]metadata-{}[pub]".format(mdversion)
return md
@app.route('/update_now')
@publisher()
def update_now():
global mdversion
mdversion += 1
hub.send_change_notification.delay(url_for('md', _external=True))
msg = "Notification send!"
return render_template('publisher.html', message=msg)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80)