-
Notifications
You must be signed in to change notification settings - Fork 0
/
wsgi.py
113 lines (98 loc) · 3.12 KB
/
wsgi.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import time
import re
from cgi import escape
from threading import Thread
import queue
from clicklist_manager import (update_cart, empty_cart)
import logging
from logging.handlers import QueueHandler
import datetime
## uwsgi --enable-threads --http :8084 --wsgi-file wsgi.py
logging.basicConfig(level=logging.INFO)
que = queue.Queue(-1)
log_root = logging.getLogger(None)
log_root.addHandler(QueueHandler(que))
running = None
class ClickListThread(Thread):
def __init__(self, cmd):
global running
Thread.__init__(self)
running = "Running"
self.cmd = cmd
def run(self):
global running
logging.info("Running ClickList update")
self.cmd()
running = "Stopped"
def index(environ, start_response):
"""This function will be mounted on "/" and display a link
to the hello world page."""
print("Sending Index")
start_response('200 OK', [('Content-Type', 'text/html')])
str = '''
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<script>
var source = new EventSource('/events');
source.onmessage = function(e) {
document.body.innerHTML += e.data + '<br>';
};
</script>
</body>
</html>'''
return [str.encode('utf-8')]
def update(environ, start_response):
t = ClickListThread(update_cart)
t.daemon = True
t.start()
return index(environ, start_response)
def empty(environ, start_response):
t = ClickListThread(empty_cart)
t.daemon = True
t.start()
return index(environ, start_response)
def events(environ, start_response):
global running
if not running:
start_response('204 No Content', [('Content-Type', 'text/event-stream')]) #('Cache-Control: no-cache')
return [b'']
start_response('200 OK', [('Content-Type', 'text/event-stream')]) #('Cache-Control: no-cache')
#logging.info("Start Event")
try:
while True:
line = que.get(False)
t = datetime.datetime.fromtimestamp(line.created)
yield 'data:{} {}: {}\n\n'.format(line.levelname, t.strftime("%Y-%m-%d %H:%M:%S.%f"), line.message).encode('utf-8')
except:
pass
if running == "Stopped":
running = None
def not_found(environ, start_response):
"""Called if no URL matches."""
start_response('404 NOT FOUND', [('Content-Type', 'text/plain')])
return [b'Not Found']
# map urls to functions
urls = [
(r'update/?$', update),
(r'empty/?$', empty),
(r'events/?$', events),
]
def application(environ, start_response):
"""
The main WSGI application. Dispatch the current request to
the functions from above and store the regular expression
captures in the WSGI environment as `myapp.url_args` so that
the functions from above can access the url placeholders.
If nothing matches call the `not_found` function.
"""
path = environ.get('PATH_INFO', '').lstrip('/')
for regex, callback in urls:
match = re.search(regex, path)
if match is not None:
environ['myapp.url_args'] = match.groups()
return callback(environ, start_response)
return not_found(environ, start_response)