~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
21 by William Grant
Merge grackle.server into grackle. Alter Makefile to run all the tests.
24
from grackle.model import CassandraConnection
8 by William Grant
A little bit of server.
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')
28 by William Grant
Fix wsgi to cope with memo changes.
64
        memo = getattr(args, 'memo', '')
65
        prev, messages, next = conn.get_messages(name, order, count, memo)
11 by William Grant
Support batching.
66
        return json.dumps({
28 by William Grant
Fix wsgi to cope with memo changes.
67
            'prev_memo': prev,
11 by William Grant
Support batching.
68
            'messages': messages,
28 by William Grant
Fix wsgi to cope with memo changes.
69
            'next_memo': next,
11 by William Grant
Support batching.
70
            })
8 by William Grant
A little bit of server.
71
7 by William Grant
Add trivial WSGI app and test.
72
73
urls = (
74
    '/', 'root',
75
    '/archive/(.+)', 'archive',
76
    )
77
78
app = web.application(urls, globals(), autoreload=False)
79
80
if __name__ == "__main__":
9 by William Grant
Extract some message metadata. Log betterer.
81
    import logging
82
    logging.basicConfig(
83
        format='%(asctime)s:%(levelname)s:%(name)s:%(message)s',
84
        level=logging.DEBUG)
85
    conn = CassandraConnection('grackle', ['localhost:9160'])
7 by William Grant
Add trivial WSGI app and test.
86
    app.run()