-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_http.py
154 lines (130 loc) · 7.02 KB
/
server_http.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"""
PySiCa, a simple Python Cache system
v0.2 December 2018
"""
import os
import falcon
import ujson
import logging.config
from logging.handlers import RotatingFileHandler
from shutil import copyfile
from pysica import PySiCa
class Application(object):
def __init__(self):
# ------------------------------------------------------------------------------------------
# SERVER DEFINITION
# ------------------------------------------------------------------------------------------
self.settings = self.read_settings_file()
self.buffer_size = 0
# Enable the logging to file for production
self.logger= self.configure_logging()
# Create the instance for the cache
self.cache_instance = PySiCa(
logger=self.logger,
timeout=self.settings.get("TIMEOUT"),
compress=self.settings.get("COMPRESS"),
max_elems=self.settings.get("MAX_ELEMS"),
clean_interval=self.settings.get("CLEAN_INTERVAL")
)
def on_get(self, req, resp, element_id=None):
try:
data_type = req.params.get('data_type')
user_id = req.params.get('user_id')
reset_timeout = req.params.get('reset_timeout', False)
timeout = req.params.get('timeout')
result = self.cache_instance.get_elem(element_id, data_type=data_type, user_id=user_id, reset_timeout=reset_timeout, timeout=timeout)
resp.status = falcon.HTTP_200
if len(result) > 0:
resp.body = ujson.dumps({'success': True, 'result': result}, ensure_ascii=False)
else:
resp.body = ujson.dumps({'success': False}, ensure_ascii=False)
except Exception as e:
resp.status = falcon.HTTP_200
resp.body = ujson.dumps({'success': False, 'message': "Failed while getting element. Error message: " + str(e)})
def on_post(self, req, resp):
try:
element_id = req.media.get("element_id")
data = req.media.get("data")
data_type = req.media.get("data_type")
user_id = req.media.get("user_id")
timeout = req.media.get("timeout") # in minutes
compress = req.media.get("compress")
success = self.cache_instance.add(element_id, data, data_type, timeout=timeout, compress=compress, user_id=user_id)
resp.status = falcon.HTTP_200
resp.body = ujson.dumps({'success': success, 'element_id': element_id})
except Exception as e:
resp.status = falcon.HTTP_200
resp.body = ujson.dumps({'success': False, 'message': "Failed while storing new element. Error message: " + str(e)})
def on_delete(self, req, resp, element_id):
try:
user_id = req.params.get("user_id", None)
return_elem = req.params.get("return", False)
result = self.cache_instance.remove(element_id, user_id=user_id)
if return_elem:
resp.body = ujson.dumps({'success': len(result) > 0, 'result': result}, ensure_ascii=False)
else:
resp.body = ujson.dumps({'success': len(result) > 0}, ensure_ascii=False)
except Exception as e:
resp.status = falcon.HTTP_200
resp.body = ujson.dumps({'success': False, 'message': "Failed while removing element. Error message: " + str(e)})
def on_put(self, req, resp, element_id):
try:
timeout = req.params.get("timeout", None)
user_id = req.params.get("user_id", None)
result = self.cache_instance.reset_timeout(element_id, timeout=timeout, user_id=user_id)
if result:
resp.body = ujson.dumps({'success': True}, ensure_ascii=False)
else:
resp.body = ujson.dumps({'success': False, 'message': "Element " + element_id + " is not in cache."}, ensure_ascii=False)
except Exception as e:
resp.status = falcon.HTTP_200
resp.body = ujson.dumps({'success': False, 'message': "Failed while removing element. Error message: " + str(e)})
def read_settings_file(self):
conf_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "conf/server.cfg")
logging_conf_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "conf/logging.cfg")
# Copy the default settings
if not os.path.isfile(conf_path):
copyfile(os.path.join(os.path.dirname(os.path.realpath(__file__)), "default/server.default.cfg"), conf_path)
copyfile(os.path.join(os.path.dirname(os.path.realpath(__file__)), "default/logging.default.cfg"), logging_conf_path)
settings = {}
if os.path.isfile(conf_path):
config = ujson.load(open(conf_path))
SERVER_SETTINGS = config.get("SERVER_SETTINGS", {})
settings["SERVER_HOST_NAME"] = SERVER_SETTINGS.get('SERVER_HOST_NAME', "0.0.0.0")
settings["SERVER_SUBDOMAIN"] = SERVER_SETTINGS.get('SERVER_SUBDOMAIN', "")
settings["SERVER_PORT_NUMBER"] = SERVER_SETTINGS.get('SERVER_PORT_NUMBER', 8081)
settings["DEBUG"] = SERVER_SETTINGS.get('DEBUG', False)
settings["TMP_DIRECTORY"] = SERVER_SETTINGS.get('TMP_DIRECTORY', "/tmp")
settings["LOG_FILE"] = SERVER_SETTINGS.get('LOG_FILE', "/tmp/queue.log")
CACHE_SETTINGS = config.get("CACHE_SETTINGS", {})
settings["TIMEOUT"] = CACHE_SETTINGS.get('TIMEOUT', 10)
settings["COMPRESS"] = CACHE_SETTINGS.get('COMPRESS', True)
settings["MAX_ELEMS"] = CACHE_SETTINGS.get('MAX_ELEMS', 50)
settings["CLEAN_INTERVAL"] = CACHE_SETTINGS.get('CLEAN_INTERVAL', 30)
# PREPARE LOGGING
logging.config.fileConfig(logging_conf_path)
return settings
def configure_logging(self):
if not self.settings.get("DEBUG", True):
try:
logger = logging.getLogger()
logger.handlers=[]
#
handler = RotatingFileHandler(self.settings.get("LOG_FILE"), maxBytes=31457280, backupCount=5)
handler.propagate = False
handler.setLevel(logging.INFO)
handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(filename)s : %(funcName)s - %(message)s'))
logger.root.addHandler(handler)
return logger
except Exception as e:
raise Exception("Unable to open log file " + self.settings.get("LOG_FILE", "LOG FILE NOT SPECIFIED") + ". Error message: " + str(e))
else:
return logging.root
api = application = falcon.API()
application.req_options.auto_parse_form_urlencoded = True
application_functions = Application()
application.add_route('/api/get/{element_id}', application_functions)
application.add_route('/api/get', application_functions)
application.add_route('/api/remove/{element_id}', application_functions)
application.add_route('/api/reset/{element_id}', application_functions)
application.add_route('/api/add', application_functions)