-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·137 lines (102 loc) · 3.63 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/python
# -*- coding: utf-8 -*-
from pymongo import MongoClient
import redis
import json
import web
client = MongoClient(host='mongodb', port=27017)
db = client.mydb
coll = db.mycoll
cache = redis.Redis(host='rediska', port=6379)
cache.ping()
# For each end point there is a method that handles
# it in the HandleRequest class
urls = (r'/storage/?(?P<key>.+)?', 'HandleRequest')
# This class is used to handle requests
class HandleRequest:
# Initialization code
def __init__(self):
# Set response content type (json) and
# encoding (utf8) for all requests
web.header('Content-Type', 'text/json; charset=utf-8')
# --------------------------------------------
# Handle HTTP GET
def GET(self, key=None):
if key is None:
raise web.badrequest()
else:
answ = cache.get(key)
response = {}
if answ == None:
db_doc = coll.find_one({'key': key})
if db_doc == None:
response['Status'] = 'Not found'
raise web.notfound(json.dumps(response))
else:
response['message'] = db_doc['msg']
response['Status'] = 'OK'
cache.set(key, response['message']) # update chache
else:
if isinstance(answ, bytes):
response['message'] = answ.decode('utf-8')
else:
response['message'] = answ
response['Status'] = 'OK'
return json.dumps(response)
# --------------------------------------------
# Handle HTTP POST
def POST(self, key=None):
if key is None:
raise web.badrequest()
else:
response = {}
obj_data = json.loads(web.data())
db_doc = coll.find_one({'key': key})
if db_doc == None:
response['Status'] = 'Created'
coll.insert_one({'key': key, 'msg': obj_data['message']})
else:
response['Status'] = 'OK'
db_doc['msg'] = obj_data['message']
if cache.exists(key):
cache.set(key, obj_data['message'])
return json.dumps(response)
# --------------------------------------------
# Handle HTTP PUT
def PUT(self, key=None):
if key is None:
raise web.badrequest()
else:
response = {}
obj_data = json.loads(web.data())
db_doc = coll.find_one({'key': key})
if db_doc != None:
db_doc['msg'] = obj_data['message']
if cache.exists(key):
cache.set(key, obj_data['message'])
response['Status'] = 'OK'
else:
response['Status'] = 'Not found'
raise web.notfound(json.dumps(response))
return json.dumps(response)
# --------------------------------------------
# Handle HTTP DELETE
def DELETE(self, key=None):
if key is None:
raise web.badrequest()
else:
response = {}
db_doc = coll.find_one({'key': key})
if db_doc != None:
coll.remove(db_doc)
cache.delete(key)
response['Status'] = 'OK'
else:
response['Status'] = 'Not found'
raise web.notfound(json.dumps(response))
return json.dumps(response)
# --------------------------------------------
# Start web server
if __name__ == '__main__':
app = web.application(urls, globals())
app.run()