~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-12 10:52:19 UTC
  • Revision ID: william.grant@canonical.com-20120112105219-c05dndn8q1p0ct31
Add trivial WSGI app and test.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
__metaclass__ = type
18
18
 
19
 
import json
20
 
import uuid
21
 
 
22
19
import web
23
20
 
24
 
from grackle.model import CassandraConnection
25
 
 
26
 
conn = None
27
 
 
28
21
 
29
22
class root:
30
23
 
40
33
 
41
34
    def GET(self, name):
42
35
        web.header('Content-Type', 'text/plain')
43
 
        args = web.input()
44
 
        if not getattr(args, 'op', None):
45
 
            return self.index(name)
46
 
 
47
 
        if args.op == 'get_messages':
48
 
            return self.get_messages(name, args)
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):
56
36
        return ('Service for archive %s.' % name)
57
37
 
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')
64
 
        memo = getattr(args, 'memo', '')
65
 
        prev, messages, next = conn.get_messages(name, order, count, memo)
66
 
        return json.dumps({
67
 
            'prev_memo': prev,
68
 
            'messages': messages,
69
 
            'next_memo': next,
70
 
            })
71
 
 
72
38
 
73
39
urls = (
74
40
    '/', 'root',
78
44
app = web.application(urls, globals(), autoreload=False)
79
45
 
80
46
if __name__ == "__main__":
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'])
86
47
    app.run()