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