aboutsummaryrefslogtreecommitdiffstats
path: root/libmproxy/web/app.py
blob: e2765a6d80b1448a68e9ea27733f041b5dc4c63f (plain)
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
import os.path
import tornado.web
import tornado.websocket
import logging
import json


class IndexHandler(tornado.web.RequestHandler):
    def get(self):
        self.render("index.html")


class ClientConnection(tornado.websocket.WebSocketHandler):
    connections = set()

    def open(self):
        ClientConnection.connections.add(self)

    def on_close(self):
        ClientConnection.connections.remove(self)

    @classmethod
    def broadcast(cls, type, data):
        for conn in cls.connections:
            try:
                conn.write_message(
                    json.dumps(
                        {
                            "type": type,
                            "data": data
                        }
                    )
                )
            except:
                logging.error("Error sending message", exc_info=True)


class Application(tornado.web.Application):
    def __init__(self, debug):
        handlers = [
            (r"/", IndexHandler),
            (r"/updates", ClientConnection),
        ]
        settings = dict(
            template_path=os.path.join(os.path.dirname(__file__), "templates"),
            static_path=os.path.join(os.path.dirname(__file__), "static"),
            xsrf_cookies=True,
            cookie_secret="__TODO:_GENERATE_YOUR_OWN_RANDOM_VALUE_HERE__",
            debug=debug,
        )
        tornado.web.Application.__init__(self, handlers, **settings)