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:
parent
d23da91101
commit
a52c494e4b
@ -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",
|
||||||
|
279
maloja/server.py
279
maloja/server.py
@ -90,19 +90,6 @@ def clean_html(inp):
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
######
|
######
|
||||||
### ERRORS
|
### ERRORS
|
||||||
#####
|
#####
|
||||||
@ -154,153 +141,153 @@ 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):
|
|
||||||
redirect("/apis/" + pth + "?" + request.query_string,308)
|
|
||||||
|
|
||||||
@webserver.get("/api/<pth:path>")
|
# redirects for backwards compatibility
|
||||||
@webserver.post("/api/<pth:path>")
|
@webserver.get("/api/s/<pth:path>")
|
||||||
def deprecated_api(pth):
|
@webserver.post("/api/s/<pth:path>")
|
||||||
redirect("/apis/mlj_1/" + pth + "?" + request.query_string,308)
|
def deprecated_api_s(pth):
|
||||||
|
redirect("/apis/" + pth + "?" + request.query_string,308)
|
||||||
|
|
||||||
def register_endpoints_web_static():
|
@webserver.get("/api/<pth:path>")
|
||||||
|
@webserver.post("/api/<pth:path>")
|
||||||
@webserver.route("/image")
|
def deprecated_api(pth):
|
||||||
def dynamic_image():
|
redirect("/apis/mlj_1/" + pth + "?" + request.query_string,308)
|
||||||
keys = FormsDict.decode(request.query)
|
|
||||||
relevant, _, _, _, _ = uri_to_internal(keys)
|
|
||||||
result = resolveImage(**relevant)
|
|
||||||
if result == "": return ""
|
|
||||||
redirect(result,307)
|
|
||||||
|
|
||||||
@webserver.route("/images/<pth:re:.*\\.jpeg>")
|
|
||||||
@webserver.route("/images/<pth:re:.*\\.jpg>")
|
|
||||||
@webserver.route("/images/<pth:re:.*\\.png>")
|
|
||||||
@webserver.route("/images/<pth:re:.*\\.gif>")
|
|
||||||
def static_image(pth):
|
|
||||||
if globalconf.USE_THUMBOR:
|
|
||||||
return static_file(pth,root=data_dir['images']())
|
|
||||||
|
|
||||||
type = pth.split(".")[-1]
|
|
||||||
small_pth = pth + "-small"
|
|
||||||
if os.path.exists(data_dir['images'](small_pth)):
|
|
||||||
response = static_file(small_pth,root=data_dir['images']())
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
from wand.image import Image
|
|
||||||
img = Image(filename=data_dir['images'](pth))
|
|
||||||
x,y = img.size[0], img.size[1]
|
|
||||||
smaller = min(x,y)
|
|
||||||
if smaller > 300:
|
|
||||||
ratio = 300/smaller
|
|
||||||
img.resize(int(ratio*x),int(ratio*y))
|
|
||||||
img.save(filename=data_dir['images'](small_pth))
|
|
||||||
response = static_file(small_pth,root=data_dir['images']())
|
|
||||||
else:
|
|
||||||
response = static_file(pth,root=data_dir['images']())
|
|
||||||
except:
|
|
||||||
response = static_file(pth,root=data_dir['images']())
|
|
||||||
|
|
||||||
#response = static_file("images/" + pth,root="")
|
|
||||||
response.set_header("Cache-Control", "public, max-age=86400")
|
|
||||||
response.set_header("Content-Type", "image/" + type)
|
|
||||||
return response
|
|
||||||
|
|
||||||
|
|
||||||
@webserver.route("/style.css")
|
|
||||||
def get_css():
|
|
||||||
response.content_type = 'text/css'
|
|
||||||
global css
|
|
||||||
if settings.get_settings("DEV_MODE"): css = generate_css()
|
|
||||||
return css
|
|
||||||
|
|
||||||
|
|
||||||
@webserver.route("/login")
|
|
||||||
def login():
|
|
||||||
return auth.get_login_page()
|
|
||||||
|
|
||||||
@webserver.route("/<name>.<ext>")
|
|
||||||
def static(name,ext):
|
|
||||||
assert ext in ["txt","ico","jpeg","jpg","png","less","js"]
|
|
||||||
response = static_file(ext + "/" + name + "." + ext,root=STATICFOLDER)
|
|
||||||
response.set_header("Cache-Control", "public, max-age=3600")
|
|
||||||
return response
|
|
||||||
|
|
||||||
@webserver.route("/media/<name>.<ext>")
|
|
||||||
def static(name,ext):
|
|
||||||
assert ext in ["ico","jpeg","jpg","png"]
|
|
||||||
response = static_file(ext + "/" + name + "." + ext,root=STATICFOLDER)
|
|
||||||
response.set_header("Cache-Control", "public, max-age=3600")
|
|
||||||
return response
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### STATIC
|
||||||
|
|
||||||
|
@webserver.route("/image")
|
||||||
|
def dynamic_image():
|
||||||
|
keys = FormsDict.decode(request.query)
|
||||||
|
relevant, _, _, _, _ = uri_to_internal(keys)
|
||||||
|
result = resolveImage(**relevant)
|
||||||
|
if result == "": return ""
|
||||||
|
redirect(result,307)
|
||||||
|
|
||||||
|
@webserver.route("/images/<pth:re:.*\\.jpeg>")
|
||||||
|
@webserver.route("/images/<pth:re:.*\\.jpg>")
|
||||||
|
@webserver.route("/images/<pth:re:.*\\.png>")
|
||||||
|
@webserver.route("/images/<pth:re:.*\\.gif>")
|
||||||
|
def static_image(pth):
|
||||||
|
if globalconf.USE_THUMBOR:
|
||||||
|
return static_file(pth,root=data_dir['images']())
|
||||||
|
|
||||||
def register_endpoints_web_dynamic():
|
type = pth.split(".")[-1]
|
||||||
|
small_pth = pth + "-small"
|
||||||
def static_html(name):
|
if os.path.exists(data_dir['images'](small_pth)):
|
||||||
if name in aliases: redirect(aliases[name])
|
response = static_file(small_pth,root=data_dir['images']())
|
||||||
linkheaders = ["</style.css>; rel=preload; as=style"]
|
else:
|
||||||
keys = remove_identical(FormsDict.decode(request.query))
|
|
||||||
|
|
||||||
adminmode = request.cookies.get("adminmode") == "true" and auth.check(request)
|
|
||||||
|
|
||||||
clock = Clock()
|
|
||||||
clock.start()
|
|
||||||
|
|
||||||
LOCAL_CONTEXT = {
|
|
||||||
"adminmode":adminmode,
|
|
||||||
"apikey":request.cookies.get("apikey") if adminmode else None,
|
|
||||||
"_urikeys":keys, #temporary!
|
|
||||||
}
|
|
||||||
lc = LOCAL_CONTEXT
|
|
||||||
lc["filterkeys"], lc["limitkeys"], lc["delimitkeys"], lc["amountkeys"], lc["specialkeys"] = uri_to_internal(keys)
|
|
||||||
|
|
||||||
template = jinja_environment.get_template(name + '.jinja')
|
|
||||||
try:
|
try:
|
||||||
res = template.render(**LOCAL_CONTEXT)
|
from wand.image import Image
|
||||||
except (ValueError, IndexError) as e:
|
img = Image(filename=data_dir['images'](pth))
|
||||||
abort(404,"This Artist or Track does not exist")
|
x,y = img.size[0], img.size[1]
|
||||||
|
smaller = min(x,y)
|
||||||
|
if smaller > 300:
|
||||||
|
ratio = 300/smaller
|
||||||
|
img.resize(int(ratio*x),int(ratio*y))
|
||||||
|
img.save(filename=data_dir['images'](small_pth))
|
||||||
|
response = static_file(small_pth,root=data_dir['images']())
|
||||||
|
else:
|
||||||
|
response = static_file(pth,root=data_dir['images']())
|
||||||
|
except:
|
||||||
|
response = static_file(pth,root=data_dir['images']())
|
||||||
|
|
||||||
if settings.get_settings("DEV_MODE"): jinja_environment.cache.clear()
|
#response = static_file("images/" + pth,root="")
|
||||||
|
response.set_header("Cache-Control", "public, max-age=86400")
|
||||||
log("Generated page {name} in {time:.5f}s".format(name=name,time=clock.stop()),module="debug_performance")
|
response.set_header("Content-Type", "image/" + type)
|
||||||
return clean_html(res)
|
return response
|
||||||
|
|
||||||
@webserver.route("/<name:re:admin.*>")
|
|
||||||
@auth.authenticated
|
|
||||||
def static_html_private(name):
|
|
||||||
return static_html(name)
|
|
||||||
|
|
||||||
@webserver.route("/<name>")
|
|
||||||
def static_html_public(name):
|
|
||||||
return static_html(name)
|
|
||||||
|
|
||||||
@webserver.route("")
|
|
||||||
@webserver.route("/")
|
|
||||||
def mainpage():
|
|
||||||
return static_html("start")
|
|
||||||
|
|
||||||
|
|
||||||
# Shortlinks
|
@webserver.route("/style.css")
|
||||||
|
def get_css():
|
||||||
|
response.content_type = 'text/css'
|
||||||
|
global css
|
||||||
|
if settings.get_settings("DEV_MODE"): css = generate_css()
|
||||||
|
return css
|
||||||
|
|
||||||
@webserver.get("/artist/<artist>")
|
|
||||||
def redirect_artist(artist):
|
@webserver.route("/login")
|
||||||
redirect("/artist?artist=" + artist)
|
def login():
|
||||||
@webserver.get("/track/<artists:path>/<title>")
|
return auth.get_login_page()
|
||||||
def redirect_track(artists,title):
|
|
||||||
redirect("/track?title=" + title + "&" + "&".join("artist=" + artist for artist in artists.split("/")))
|
@webserver.route("/<name>.<ext>")
|
||||||
|
def static(name,ext):
|
||||||
|
assert ext in ["txt","ico","jpeg","jpg","png","less","js"]
|
||||||
|
response = static_file(ext + "/" + name + "." + ext,root=STATICFOLDER)
|
||||||
|
response.set_header("Cache-Control", "public, max-age=3600")
|
||||||
|
return response
|
||||||
|
|
||||||
|
@webserver.route("/media/<name>.<ext>")
|
||||||
|
def static(name,ext):
|
||||||
|
assert ext in ["ico","jpeg","jpg","png"]
|
||||||
|
response = static_file(ext + "/" + name + "." + ext,root=STATICFOLDER)
|
||||||
|
response.set_header("Cache-Control", "public, max-age=3600")
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### DYNAMIC
|
||||||
|
|
||||||
|
def static_html(name):
|
||||||
|
if name in aliases: redirect(aliases[name])
|
||||||
|
linkheaders = ["</style.css>; rel=preload; as=style"]
|
||||||
|
keys = remove_identical(FormsDict.decode(request.query))
|
||||||
|
|
||||||
|
adminmode = request.cookies.get("adminmode") == "true" and auth.check(request)
|
||||||
|
|
||||||
|
clock = Clock()
|
||||||
|
clock.start()
|
||||||
|
|
||||||
|
LOCAL_CONTEXT = {
|
||||||
|
"adminmode":adminmode,
|
||||||
|
"apikey":request.cookies.get("apikey") if adminmode else None,
|
||||||
|
"_urikeys":keys, #temporary!
|
||||||
|
}
|
||||||
|
lc = LOCAL_CONTEXT
|
||||||
|
lc["filterkeys"], lc["limitkeys"], lc["delimitkeys"], lc["amountkeys"], lc["specialkeys"] = uri_to_internal(keys)
|
||||||
|
|
||||||
|
template = jinja_environment.get_template(name + '.jinja')
|
||||||
|
try:
|
||||||
|
res = template.render(**LOCAL_CONTEXT)
|
||||||
|
except (ValueError, IndexError) as e:
|
||||||
|
abort(404,"This Artist or Track does not exist")
|
||||||
|
|
||||||
|
if settings.get_settings("DEV_MODE"): jinja_environment.cache.clear()
|
||||||
|
|
||||||
|
log("Generated page {name} in {time:.5f}s".format(name=name,time=clock.stop()),module="debug_performance")
|
||||||
|
return clean_html(res)
|
||||||
|
|
||||||
|
@webserver.route("/<name:re:admin.*>")
|
||||||
|
@auth.authenticated
|
||||||
|
def static_html_private(name):
|
||||||
|
return static_html(name)
|
||||||
|
|
||||||
|
@webserver.route("/<name>")
|
||||||
|
def static_html_public(name):
|
||||||
|
return static_html(name)
|
||||||
|
|
||||||
|
@webserver.route("")
|
||||||
|
@webserver.route("/")
|
||||||
|
def mainpage():
|
||||||
|
return static_html("start")
|
||||||
|
|
||||||
|
|
||||||
|
# Shortlinks
|
||||||
|
|
||||||
|
@webserver.get("/artist/<artist>")
|
||||||
|
def redirect_artist(artist):
|
||||||
|
redirect("/artist?artist=" + artist)
|
||||||
|
@webserver.get("/track/<artists:path>/<title>")
|
||||||
|
def redirect_track(artists,title):
|
||||||
|
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
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user