Had to refactor server to support minimal plugin
This commit is contained in:
parent
46d6448e3b
commit
54395b0ad3
|
@ -26,20 +26,6 @@ URL = "//plugins.lexonomy.cjvt.si"
|
||||||
REPLACE_STRING = "$LOCATION$"
|
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):
|
def check_cache(full_path):
|
||||||
file_time = int(os.path.getmtime(full_path))
|
file_time = int(os.path.getmtime(full_path))
|
||||||
old_file_time = 0
|
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))
|
return ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N))
|
||||||
|
|
||||||
|
|
||||||
@app.route("/<plugin>/<path:path>")
|
def return_file(path, plugin=None):
|
||||||
def plugin_file(plugin, path):
|
full_path = path if plugin is None else"{}/{}".format(plugin, path)
|
||||||
full_path = "{}/{}".format(plugin, path)
|
|
||||||
|
|
||||||
if not os.path.isfile(full_path):
|
if not os.path.isfile(full_path):
|
||||||
return "File not found", 404
|
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, make search-and-replace and save to redis (+etag)
|
||||||
else:
|
else:
|
||||||
replace_with = "{}/{}".format(URL, plugin)
|
|
||||||
status_code = 200
|
status_code = 200
|
||||||
etag = generate_etag()
|
etag = generate_etag()
|
||||||
|
|
||||||
with open(full_path, 'rb') as fp:
|
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)
|
||||||
|
|
||||||
|
if plugin is not None:
|
||||||
|
replace_with = "{}/{}".format(URL, plugin)
|
||||||
try:
|
try:
|
||||||
content = content_bytes.decode('UTF-8')
|
content = result.decode('UTF-8')
|
||||||
result = content.replace(REPLACE_STRING, replace_with)
|
content_replaced = content.replace(REPLACE_STRING, replace_with)
|
||||||
result = result.encode('UTF-8')
|
result = content_replaced.encode('UTF-8')
|
||||||
except UnicodeDecodeError:
|
except UnicodeDecodeError:
|
||||||
result = content_bytes
|
pass
|
||||||
|
|
||||||
redis.set(full_path + ":content", result)
|
redis.set(full_path + ":content", result)
|
||||||
redis.set(full_path + ":etag", etag)
|
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 browser does not have a cached etag -> 200
|
||||||
if 'If-None-Match' not in request.headers:
|
if 'If-None-Match' not in request.headers:
|
||||||
status_code = 200
|
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
|
# then check if it is the latest etag, else -> 200
|
||||||
elif request.headers['If-None-Match'] != etag:
|
elif request.headers['If-None-Match'] != etag:
|
||||||
status_code = 200
|
status_code = 200
|
||||||
|
|
||||||
return Response(result, mimetype=mt, headers=headers, status=status_code)
|
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__':
|
if __name__ == '__main__':
|
||||||
app.run(host="0.0.0.0")
|
app.run(host="0.0.0.0")
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user