~unity-2d-team/unity-2d/Shell-MultiMonitor

7 by William Grant
Add trivial WSGI app and test.
1
# Copyright (c) 2011 Canonical Ltd
2
#
3
# This program is free software: you can redistribute it and/or modify
4
# it under the terms of the GNU Affero General Public License as published by
5
# the Free Software Foundation, either version 3 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU Affero General Public License for more details.
12
#
13
# You should have received a copy of the GNU Affero General Public
14
# License along with this program. If not, see
15
# <http://www.gnu.org/licenses/>.
16
17
__metaclass__ = type
18
11 by William Grant
Support batching.
19
import json
20
import uuid
21
7 by William Grant
Add trivial WSGI app and test.
22
import web
23
8 by William Grant
A little bit of server.
24
from grackle.server.model import CassandraConnection
25
26
conn = None
27
7 by William Grant
Add trivial WSGI app and test.
28
29
class root:
30
31
    def GET(self):
32
        web.header('Content-Type', 'text/plain')
33
        return (
34
            'Service endpoint for Grackle.\n\n'
35
            'When you want mail that was archived in Grackle, you can get it '
36
            'from Grackle')
37
38
39
class archive:
40
41
    def GET(self, name):
42
        web.header('Content-Type', 'text/plain')
8 by William Grant
A little bit of server.
43
        args = web.input()
44
        if not getattr(args, 'op', None):
45
            return self.index(name)
46
47
        if args.op == 'get_messages':
10 by William Grant
Let the HTTP client decide count and order (to an extent).
48
            return self.get_messages(name, args)
8 by William Grant
A little bit of server.
49
50
    def POST(self, name):
51
        data = web.data()
52
        conn.add_message(name, data)
53
        web.ctx.status = '201 Created'
54
55
    def index(self, name):
7 by William Grant
Add trivial WSGI app and test.
56
        return ('Service for archive %s.' % name)
57
10 by William Grant
Let the HTTP client decide count and order (to an extent).
58
    def get_messages(self, name, args):
59
        try:
60
            count = int(args.count)
61
        except (AttributeError, ValueError):
62
            count = 10
63
        order = getattr(args, 'order', '-date')
11 by William Grant
Support batching.
64
        start = getattr(args, 'start', '')
65
        if start:
66
            start = uuid.UUID(start)
67
        messages, next_memo = conn.get_messages(name, order, count, start)
68
        return json.dumps({
69
            'messages': messages,
70
            'next_memo': next_memo,
71
            })
8 by William Grant
A little bit of server.
72
7 by William Grant
Add trivial WSGI app and test.
73
74
urls = (
75
    '/', 'root',
76
    '/archive/(.+)', 'archive',
77
    )
78
79
app = web.application(urls, globals(), autoreload=False)
80
81
if __name__ == "__main__":
9 by William Grant
Extract some message metadata. Log betterer.
82
    import logging
83
    logging.basicConfig(
84
        format='%(asctime)s:%(levelname)s:%(name)s:%(message)s',
85
        level=logging.DEBUG)
86
    conn = CassandraConnection('grackle', ['localhost:9160'])
7 by William Grant
Add trivial WSGI app and test.
87
    app.run()