Had to refactor server to support minimal plugin

This commit is contained in:
Ozbolt Menegatti 2019-09-23 15:32:13 +00:00
parent 46d6448e3b
commit 54395b0ad3

View File

@ -26,20 +26,6 @@ URL = "//plugins.lexonomy.cjvt.si"
REPLACE_STRING = "$LOCATION$"
@app.route("/")
def root():
result = "<html><body><h2>Description</h2><pre>"
result += sys.modules[__name__].__doc__
result += "</pre><h2>Url</h2><p>"
result += URL
result += "</p></body></html>"
return result
@app.route("/<plugin>")
def plugin_root(plugin):
status = "" if os.path.isdir(plugin) else " NOT"
return "Plugin {} was{} found".format(plugin, status)
def check_cache(full_path):
file_time = int(os.path.getmtime(full_path))
old_file_time = 0
@ -60,9 +46,8 @@ def generate_etag(N=12):
return ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N))
@app.route("/<plugin>/<path:path>")
def plugin_file(plugin, path):
full_path = "{}/{}".format(plugin, path)
def return_file(path, plugin=None):
full_path = path if plugin is None else"{}/{}".format(plugin, path)
if not os.path.isfile(full_path):
return "File not found", 404
@ -77,19 +62,23 @@ def plugin_file(plugin, path):
# else, make search-and-replace and save to redis (+etag)
else:
replace_with = "{}/{}".format(URL, plugin)
status_code = 200
etag = generate_etag()
with open(full_path, 'rb') as fp:
content_bytes = fp.read()
result = fp.read()
with open(".err", "a") as fpw:
print(result, file=fpw)
print(len(result), file=fpw)
try:
content = content_bytes.decode('UTF-8')
result = content.replace(REPLACE_STRING, replace_with)
result = result.encode('UTF-8')
except UnicodeDecodeError:
result = content_bytes
if plugin is not None:
replace_with = "{}/{}".format(URL, plugin)
try:
content = result.decode('UTF-8')
content_replaced = content.replace(REPLACE_STRING, replace_with)
result = content_replaced.encode('UTF-8')
except UnicodeDecodeError:
pass
redis.set(full_path + ":content", result)
redis.set(full_path + ":etag", etag)
@ -103,12 +92,42 @@ def plugin_file(plugin, path):
# if browser does not have a cached etag -> 200
if 'If-None-Match' not in request.headers:
status_code = 200
# then check if it etag is None -> 200
elif request.headers['If-None-Match'] == "None":
status_code = 200
# then check if it is the latest etag, else -> 200
elif request.headers['If-None-Match'] != etag:
status_code = 200
return Response(result, mimetype=mt, headers=headers, status=status_code)
@app.route("/")
def root():
result = "<html><body><h2>Description</h2><pre>"
result += sys.modules[__name__].__doc__
result += "</pre><h2>Url</h2><p>"
result += URL
result += "</p></body></html>"
return result
@app.route("/plugin-loader.js")
def plugin_loader():
return return_file("./plugin-loader.js")
@app.route("/<plugin>")
def plugin_root(plugin):
status = "" if os.path.isdir(plugin) else " NOT"
return "Plugin {} was{} found".format(plugin, status)
@app.route("/<plugin>/<path:path>")
def plugin_file(plugin, path):
return return_file(path, plugin=plugin)
if __name__ == '__main__':
app.run(host="0.0.0.0")