-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkvstore.py
63 lines (54 loc) · 1.99 KB
/
kvstore.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
import pickle
from flask import Flask, request
from gevent.pywsgi import WSGIServer
app = Flask(__name__)
port = 5000
@app.route('/', methods=['GET'])
def kvhome():
return "<h1>KV Store Web Server</h1>"
#To dump serialize and dump the dictionary data to a file using pickle to have persistance in the data.
def dump_data_for_cache(store):
with open("store.pickle","wb") as handle:
pickle.dump(store,handle,protocol=pickle.HIGHEST_PROTOCOL)
#Helper to add KV pair to our store.
@app.route('/add', methods=['POST'])
def add_value():
if 'key' and 'value' in request.args:
key = str(request.args['key'])
value = str(request.args['value'])
#Check for the false case of empty key or value.
if not key or not value:
return "Error: Incomplete Input."
else:
if key in store:
store[key] = value
dump_data_for_cache(store)
return "Existing Key! Successfully changed the Value to - %s for Key - %s" % (store[key],key)
else:
store[key] = value
dump_data_for_cache(store)
return "Successfully added the Value - %s for Key - %s" % (store[key],key)
else:
return "Error: Improper input."
#Helper to get the value of provided key from our store.
@app.route('/get', methods=['GET'])
def get_value():
if 'key' in request.args:
key = str(request.args['key'])
if not key:
return "Error: Empty KEY provided."
else:
if key in store:
return store[key]
else:
return "Error: Provided key doesn't exists in the store."
else:
return "Error: No KEY provided."
@app.route('/get_all', methods=['GET'])
def get_all():
return store
#Load the dumped data everytime the API starts.
datatoload = open("store.pickle","rb")
store = pickle.load(datatoload)
http_server = WSGIServer(('127.0.0.1',port),app)
http_server.serve_forever()