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

« back to all changes in this revision

Viewing changes to grackle/server/wsgi.py

  • Committer: William Grant
  • Date: 2012-01-22 06:41:06 UTC
  • Revision ID: william.grant@canonical.com-20120122064106-6at8ool919624m0z
Let the HTTP client decide count and order (to an extent).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
19
import web
 
20
 
 
21
from grackle.server.model import CassandraConnection
 
22
 
 
23
conn = None
 
24
 
 
25
 
 
26
class root:
 
27
 
 
28
    def GET(self):
 
29
        web.header('Content-Type', 'text/plain')
 
30
        return (
 
31
            'Service endpoint for Grackle.\n\n'
 
32
            'When you want mail that was archived in Grackle, you can get it '
 
33
            'from Grackle')
 
34
 
 
35
 
 
36
class archive:
 
37
 
 
38
    def GET(self, name):
 
39
        web.header('Content-Type', 'text/plain')
 
40
        args = web.input()
 
41
        if not getattr(args, 'op', None):
 
42
            return self.index(name)
 
43
 
 
44
        if args.op == 'get_messages':
 
45
            return self.get_messages(name, args)
 
46
 
 
47
    def POST(self, name):
 
48
        data = web.data()
 
49
        conn.add_message(name, data)
 
50
        web.ctx.status = '201 Created'
 
51
 
 
52
    def index(self, name):
 
53
        return ('Service for archive %s.' % name)
 
54
 
 
55
    def get_messages(self, name, args):
 
56
        try:
 
57
            count = int(args.count)
 
58
        except (AttributeError, ValueError):
 
59
            count = 10
 
60
        order = getattr(args, 'order', '-date')
 
61
        return repr(conn.get_messages(name, count=count, order=order))
 
62
 
 
63
 
 
64
urls = (
 
65
    '/', 'root',
 
66
    '/archive/(.+)', 'archive',
 
67
    )
 
68
 
 
69
app = web.application(urls, globals(), autoreload=False)
 
70
 
 
71
if __name__ == "__main__":
 
72
    import logging
 
73
    logging.basicConfig(
 
74
        format='%(asctime)s:%(levelname)s:%(name)s:%(message)s',
 
75
        level=logging.DEBUG)
 
76
    conn = CassandraConnection('grackle', ['localhost:9160'])
 
77
    app.run()