1
0
mirror of https://github.com/krateng/maloja.git synced 2023-08-10 21:12:55 +03:00

Moved endpoints back out of functions

This commit is contained in:
krateng 2021-12-09 21:12:10 +01:00
parent d23da91101
commit a52c494e4b
2 changed files with 132 additions and 149 deletions

View File

@ -5,7 +5,7 @@ author = {
"email":"maloja@dev.krateng.ch", "email":"maloja@dev.krateng.ch",
"github": "krateng" "github": "krateng"
} }
version = 2,12,17 version = 2,12,18
versionstr = ".".join(str(n) for n in version) versionstr = ".".join(str(n) for n in version)
links = { links = {
"pypi":"malojaserver", "pypi":"malojaserver",

View File

@ -90,19 +90,6 @@ def clean_html(inp):
###### ######
### ERRORS ### ERRORS
##### #####
@ -154,38 +141,42 @@ aliases = {
} }
def register_endpoints_api(): ### API
auth.authapi.mount(server=webserver)
from .apis import init_apis auth.authapi.mount(server=webserver)
init_apis(webserver)
# redirects for backwards compatibility from .apis import init_apis
@webserver.get("/api/s/<pth:path>") init_apis(webserver)
@webserver.post("/api/s/<pth:path>")
def deprecated_api_s(pth): # redirects for backwards compatibility
@webserver.get("/api/s/<pth:path>")
@webserver.post("/api/s/<pth:path>")
def deprecated_api_s(pth):
redirect("/apis/" + pth + "?" + request.query_string,308) redirect("/apis/" + pth + "?" + request.query_string,308)
@webserver.get("/api/<pth:path>") @webserver.get("/api/<pth:path>")
@webserver.post("/api/<pth:path>") @webserver.post("/api/<pth:path>")
def deprecated_api(pth): def deprecated_api(pth):
redirect("/apis/mlj_1/" + pth + "?" + request.query_string,308) redirect("/apis/mlj_1/" + pth + "?" + request.query_string,308)
def register_endpoints_web_static():
@webserver.route("/image")
def dynamic_image():
### STATIC
@webserver.route("/image")
def dynamic_image():
keys = FormsDict.decode(request.query) keys = FormsDict.decode(request.query)
relevant, _, _, _, _ = uri_to_internal(keys) relevant, _, _, _, _ = uri_to_internal(keys)
result = resolveImage(**relevant) result = resolveImage(**relevant)
if result == "": return "" if result == "": return ""
redirect(result,307) redirect(result,307)
@webserver.route("/images/<pth:re:.*\\.jpeg>") @webserver.route("/images/<pth:re:.*\\.jpeg>")
@webserver.route("/images/<pth:re:.*\\.jpg>") @webserver.route("/images/<pth:re:.*\\.jpg>")
@webserver.route("/images/<pth:re:.*\\.png>") @webserver.route("/images/<pth:re:.*\\.png>")
@webserver.route("/images/<pth:re:.*\\.gif>") @webserver.route("/images/<pth:re:.*\\.gif>")
def static_image(pth): def static_image(pth):
if globalconf.USE_THUMBOR: if globalconf.USE_THUMBOR:
return static_file(pth,root=data_dir['images']()) return static_file(pth,root=data_dir['images']())
@ -215,27 +206,27 @@ def register_endpoints_web_static():
return response return response
@webserver.route("/style.css") @webserver.route("/style.css")
def get_css(): def get_css():
response.content_type = 'text/css' response.content_type = 'text/css'
global css global css
if settings.get_settings("DEV_MODE"): css = generate_css() if settings.get_settings("DEV_MODE"): css = generate_css()
return css return css
@webserver.route("/login") @webserver.route("/login")
def login(): def login():
return auth.get_login_page() return auth.get_login_page()
@webserver.route("/<name>.<ext>") @webserver.route("/<name>.<ext>")
def static(name,ext): def static(name,ext):
assert ext in ["txt","ico","jpeg","jpg","png","less","js"] assert ext in ["txt","ico","jpeg","jpg","png","less","js"]
response = static_file(ext + "/" + name + "." + ext,root=STATICFOLDER) response = static_file(ext + "/" + name + "." + ext,root=STATICFOLDER)
response.set_header("Cache-Control", "public, max-age=3600") response.set_header("Cache-Control", "public, max-age=3600")
return response return response
@webserver.route("/media/<name>.<ext>") @webserver.route("/media/<name>.<ext>")
def static(name,ext): def static(name,ext):
assert ext in ["ico","jpeg","jpg","png"] assert ext in ["ico","jpeg","jpg","png"]
response = static_file(ext + "/" + name + "." + ext,root=STATICFOLDER) response = static_file(ext + "/" + name + "." + ext,root=STATICFOLDER)
response.set_header("Cache-Control", "public, max-age=3600") response.set_header("Cache-Control", "public, max-age=3600")
@ -243,13 +234,9 @@ def register_endpoints_web_static():
### DYNAMIC
def static_html(name):
def register_endpoints_web_dynamic():
def static_html(name):
if name in aliases: redirect(aliases[name]) if name in aliases: redirect(aliases[name])
linkheaders = ["</style.css>; rel=preload; as=style"] linkheaders = ["</style.css>; rel=preload; as=style"]
keys = remove_identical(FormsDict.decode(request.query)) keys = remove_identical(FormsDict.decode(request.query))
@ -278,28 +265,28 @@ def register_endpoints_web_dynamic():
log("Generated page {name} in {time:.5f}s".format(name=name,time=clock.stop()),module="debug_performance") log("Generated page {name} in {time:.5f}s".format(name=name,time=clock.stop()),module="debug_performance")
return clean_html(res) return clean_html(res)
@webserver.route("/<name:re:admin.*>") @webserver.route("/<name:re:admin.*>")
@auth.authenticated @auth.authenticated
def static_html_private(name): def static_html_private(name):
return static_html(name) return static_html(name)
@webserver.route("/<name>") @webserver.route("/<name>")
def static_html_public(name): def static_html_public(name):
return static_html(name) return static_html(name)
@webserver.route("") @webserver.route("")
@webserver.route("/") @webserver.route("/")
def mainpage(): def mainpage():
return static_html("start") return static_html("start")
# Shortlinks # Shortlinks
@webserver.get("/artist/<artist>") @webserver.get("/artist/<artist>")
def redirect_artist(artist): def redirect_artist(artist):
redirect("/artist?artist=" + artist) redirect("/artist?artist=" + artist)
@webserver.get("/track/<artists:path>/<title>") @webserver.get("/track/<artists:path>/<title>")
def redirect_track(artists,title): def redirect_track(artists,title):
redirect("/track?title=" + title + "&" + "&".join("artist=" + artist for artist in artists.split("/"))) redirect("/track?title=" + title + "&" + "&".join("artist=" + artist for artist in artists.split("/")))
@ -333,10 +320,6 @@ signal.signal(signal.SIGTERM, graceful_exit)
def run_server(): def run_server():
register_endpoints_api()
register_endpoints_web_static()
register_endpoints_web_dynamic()
Thread(target=database.start_db).start() Thread(target=database.start_db).start()
## start database ## start database