2014-02-06 18:36:32 +00:00
|
|
|
import logging
|
2014-02-07 01:58:26 +00:00
|
|
|
import json
|
2014-02-06 18:36:32 +00:00
|
|
|
|
2014-03-26 19:52:24 +00:00
|
|
|
from flask import request, Blueprint, abort, Response
|
|
|
|
from flask.ext.login import current_user
|
|
|
|
from data import userevent
|
|
|
|
from auth.auth import require_session_login
|
2014-02-06 18:36:32 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
realtime = Blueprint('realtime', __name__)
|
|
|
|
|
|
|
|
|
2014-02-07 01:58:26 +00:00
|
|
|
@realtime.route("/user/")
|
2014-03-26 19:52:24 +00:00
|
|
|
@require_session_login
|
2014-02-06 18:36:32 +00:00
|
|
|
def index():
|
2014-03-26 19:52:24 +00:00
|
|
|
debug_template = """
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>Server sent events</h1>
|
|
|
|
<div id="event"></div>
|
|
|
|
<script type="text/javascript">
|
|
|
|
|
|
|
|
var eventOutputContainer = document.getElementById("event");
|
|
|
|
var evtSrc = new EventSource("/realtime/user/subscribe?events=docker-cli");
|
|
|
|
|
|
|
|
evtSrc.onmessage = function(e) {
|
|
|
|
console.log(e.data);
|
|
|
|
eventOutputContainer.innerHTML = e.data;
|
|
|
|
};
|
|
|
|
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|
2014-02-06 18:36:32 +00:00
|
|
|
"""
|
2014-03-26 19:52:24 +00:00
|
|
|
return(debug_template)
|
2014-02-06 18:36:32 +00:00
|
|
|
|
|
|
|
|
2014-02-07 01:58:26 +00:00
|
|
|
@realtime.route("/user/test")
|
2014-03-26 19:52:24 +00:00
|
|
|
@require_session_login
|
2014-02-07 01:58:26 +00:00
|
|
|
def user_test():
|
|
|
|
evt = userevent.UserEvent('logs.quay.io', current_user.db_user().username)
|
|
|
|
evt.publish_event_data('test', {'foo': 2})
|
|
|
|
return 'OK'
|
|
|
|
|
|
|
|
@realtime.route("/user/subscribe")
|
2014-03-26 19:52:24 +00:00
|
|
|
@require_session_login
|
2014-02-07 01:58:26 +00:00
|
|
|
def user_subscribe():
|
|
|
|
def wrapper(listener):
|
|
|
|
for event_id, data in listener.event_stream():
|
|
|
|
message = {'event': event_id, 'data': data}
|
|
|
|
json_string = json.dumps(message)
|
|
|
|
yield 'data: %s\n\n' % json_string
|
|
|
|
|
|
|
|
events = request.args.get('events', '').split(',')
|
|
|
|
if not events:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
listener = userevent.UserEventListener('logs.quay.io', current_user.db_user().username, events)
|
|
|
|
return Response(wrapper(listener), mimetype="text/event-stream")
|