2018-12-05 16:30:50 +03:00
|
|
|
#!/usr/bin/env python
|
2019-11-24 23:47:03 +03:00
|
|
|
import os
|
2020-12-25 07:24:59 +03:00
|
|
|
from .globalconf import data_dir
|
2019-11-24 23:47:03 +03:00
|
|
|
|
2018-12-05 16:30:50 +03:00
|
|
|
|
2019-02-20 23:10:58 +03:00
|
|
|
# server stuff
|
2020-08-17 18:14:38 +03:00
|
|
|
from bottle import Bottle, route, get, post, error, run, template, static_file, request, response, FormsDict, redirect, template, HTTPResponse, BaseRequest, abort
|
2019-02-20 23:10:58 +03:00
|
|
|
import waitress
|
2020-08-31 00:49:14 +03:00
|
|
|
|
2019-05-08 18:42:56 +03:00
|
|
|
# monkey patching
|
2019-11-24 23:47:03 +03:00
|
|
|
from . import monkey
|
2019-02-20 23:10:58 +03:00
|
|
|
# rest of the project
|
2019-11-24 23:47:03 +03:00
|
|
|
from . import database
|
|
|
|
from . import malojatime
|
|
|
|
from . import utilities
|
2020-08-31 00:49:14 +03:00
|
|
|
from . import malojauri
|
2019-11-24 23:47:03 +03:00
|
|
|
from .utilities import resolveImage
|
2020-09-02 21:38:38 +03:00
|
|
|
from .malojauri import uri_to_internal, remove_identical, compose_querystring
|
2019-12-12 23:24:13 +03:00
|
|
|
from . import globalconf
|
2020-08-31 00:49:14 +03:00
|
|
|
from .jinjaenv.context import jinja_environment
|
2020-09-03 03:57:51 +03:00
|
|
|
from jinja2.exceptions import TemplateNotFound
|
2019-03-28 17:06:14 +03:00
|
|
|
# doreah toolkit
|
|
|
|
from doreah import settings
|
2019-03-29 21:44:42 +03:00
|
|
|
from doreah.logging import log
|
2019-11-21 08:31:23 +03:00
|
|
|
from doreah.timing import Clock
|
2020-07-29 22:19:29 +03:00
|
|
|
from doreah import auth
|
2019-02-20 23:10:58 +03:00
|
|
|
# technical
|
2019-11-24 23:47:03 +03:00
|
|
|
#from importlib.machinery import SourceFileLoader
|
|
|
|
import importlib
|
2018-11-24 18:29:24 +03:00
|
|
|
import _thread
|
2018-11-27 18:21:33 +03:00
|
|
|
import sys
|
2018-11-28 15:02:43 +03:00
|
|
|
import signal
|
2018-12-16 19:52:13 +03:00
|
|
|
import os
|
2019-02-16 23:21:29 +03:00
|
|
|
import setproctitle
|
2019-11-24 23:47:03 +03:00
|
|
|
import pkg_resources
|
2020-05-13 23:57:55 +03:00
|
|
|
import math
|
2021-08-30 20:03:06 +03:00
|
|
|
from css_html_js_minify import html_minify, css_minify
|
2019-02-20 23:10:58 +03:00
|
|
|
# url handling
|
2019-05-21 18:29:07 +03:00
|
|
|
import urllib
|
2018-11-24 18:29:24 +03:00
|
|
|
|
|
|
|
|
2019-02-17 14:49:06 +03:00
|
|
|
|
2019-11-24 23:47:03 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
2019-03-31 13:18:49 +03:00
|
|
|
#settings.config(files=["settings/default.ini","settings/settings.ini"])
|
2019-03-28 18:18:31 +03:00
|
|
|
#settings.update("settings/default.ini","settings/settings.ini")
|
2019-05-12 19:39:46 +03:00
|
|
|
MAIN_PORT = settings.get_settings("WEB_PORT")
|
2019-06-30 20:32:16 +03:00
|
|
|
HOST = settings.get_settings("HOST")
|
2020-08-06 19:57:23 +03:00
|
|
|
THREADS = 24
|
2019-11-22 01:14:53 +03:00
|
|
|
BaseRequest.MEMFILE_MAX = 15 * 1024 * 1024
|
2019-03-28 17:06:14 +03:00
|
|
|
|
2020-08-30 19:56:28 +03:00
|
|
|
STATICFOLDER = pkg_resources.resource_filename(__name__,"web/static")
|
2018-11-24 18:29:24 +03:00
|
|
|
|
2018-12-19 17:28:10 +03:00
|
|
|
webserver = Bottle()
|
2020-07-29 22:19:29 +03:00
|
|
|
auth.authapi.mount(server=webserver)
|
2018-11-24 18:29:24 +03:00
|
|
|
|
2020-09-02 16:22:30 +03:00
|
|
|
from .apis import init_apis
|
|
|
|
init_apis(webserver)
|
|
|
|
|
2020-09-02 21:38:38 +03:00
|
|
|
# redirects for backwards compatibility
|
|
|
|
@webserver.get("/api/s/<pth:path>")
|
|
|
|
@webserver.post("/api/s/<pth:path>")
|
|
|
|
def deprecated_api_s(pth):
|
2020-09-06 19:19:48 +03:00
|
|
|
redirect("/apis/" + pth + "?" + request.query_string,308)
|
2020-09-02 21:38:38 +03:00
|
|
|
|
|
|
|
@webserver.get("/api/<pth:path>")
|
|
|
|
@webserver.post("/api/<pth:path>")
|
|
|
|
def deprecated_api(pth):
|
2020-09-06 19:19:48 +03:00
|
|
|
redirect("/apis/mlj_1/" + pth + "?" + request.query_string,308)
|
2020-09-02 21:38:38 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-12-04 21:14:33 +03:00
|
|
|
def generate_css():
|
2021-08-30 20:03:06 +03:00
|
|
|
css = ""
|
|
|
|
for f in os.listdir(os.path.join(STATICFOLDER,"css")):
|
|
|
|
with open(os.path.join(STATICFOLDER,"css",f),"r") as fd:
|
|
|
|
css += fd.read()
|
|
|
|
|
|
|
|
css = css_minify(css)
|
2019-12-04 21:14:33 +03:00
|
|
|
return css
|
|
|
|
|
|
|
|
css = generate_css()
|
2019-10-11 06:17:15 +03:00
|
|
|
|
2020-09-23 17:26:54 +03:00
|
|
|
|
|
|
|
def clean_html(inp):
|
2020-09-25 17:03:51 +03:00
|
|
|
if settings.get_settings("DEV_MODE"): return inp
|
|
|
|
else: return html_minify(inp)
|
2020-09-23 17:26:54 +03:00
|
|
|
|
2019-10-11 06:17:15 +03:00
|
|
|
|
|
|
|
|
2018-12-19 17:28:10 +03:00
|
|
|
@webserver.route("")
|
|
|
|
@webserver.route("/")
|
2018-11-24 18:29:24 +03:00
|
|
|
def mainpage():
|
2021-10-19 15:58:24 +03:00
|
|
|
return static_html("start")
|
2018-11-25 16:49:53 +03:00
|
|
|
|
2019-04-03 19:03:55 +03:00
|
|
|
@webserver.error(400)
|
|
|
|
@webserver.error(403)
|
|
|
|
@webserver.error(404)
|
|
|
|
@webserver.error(405)
|
|
|
|
@webserver.error(408)
|
|
|
|
@webserver.error(500)
|
|
|
|
@webserver.error(505)
|
|
|
|
def customerror(error):
|
2020-08-31 21:12:44 +03:00
|
|
|
errorcode = error.status_code
|
|
|
|
errordesc = error.status
|
2020-09-03 03:57:51 +03:00
|
|
|
traceback = error.traceback
|
|
|
|
traceback = traceback.strip() if traceback is not None else "No Traceback"
|
2020-08-31 21:12:44 +03:00
|
|
|
adminmode = request.cookies.get("adminmode") == "true" and auth.check(request)
|
2019-04-03 19:03:55 +03:00
|
|
|
|
2020-08-31 01:08:55 +03:00
|
|
|
template = jinja_environment.get_template('error.jinja')
|
2021-10-19 15:58:24 +03:00
|
|
|
return template.render(
|
|
|
|
errorcode=errorcode,
|
|
|
|
errordesc=errordesc,
|
|
|
|
traceback=traceback,
|
|
|
|
adminmode=adminmode,
|
|
|
|
)
|
2019-04-03 19:03:55 +03:00
|
|
|
|
2018-11-25 16:49:53 +03:00
|
|
|
|
2019-05-12 19:39:46 +03:00
|
|
|
|
2018-11-28 15:02:43 +03:00
|
|
|
def graceful_exit(sig=None,frame=None):
|
2019-03-28 16:18:12 +03:00
|
|
|
#urllib.request.urlopen("http://[::1]:" + str(DATABASE_PORT) + "/sync")
|
2019-07-09 21:27:36 +03:00
|
|
|
log("Received signal to shutdown")
|
|
|
|
try:
|
|
|
|
database.sync()
|
|
|
|
except Exception as e:
|
|
|
|
log("Error while shutting down!",e)
|
2019-01-10 01:29:01 +03:00
|
|
|
log("Server shutting down...")
|
2019-02-02 22:51:04 +03:00
|
|
|
os._exit(42)
|
2018-11-27 18:08:14 +03:00
|
|
|
|
|
|
|
|
2019-02-20 23:10:58 +03:00
|
|
|
@webserver.route("/image")
|
|
|
|
def dynamic_image():
|
|
|
|
keys = FormsDict.decode(request.query)
|
2020-08-30 19:53:02 +03:00
|
|
|
relevant, _, _, _, _ = uri_to_internal(keys)
|
2019-02-20 23:10:58 +03:00
|
|
|
result = resolveImage(**relevant)
|
2019-03-06 20:04:12 +03:00
|
|
|
if result == "": return ""
|
2019-04-03 19:03:55 +03:00
|
|
|
redirect(result,307)
|
2019-02-20 23:10:58 +03:00
|
|
|
|
2018-12-28 20:06:09 +03:00
|
|
|
@webserver.route("/images/<pth:re:.*\\.jpeg>")
|
|
|
|
@webserver.route("/images/<pth:re:.*\\.jpg>")
|
|
|
|
@webserver.route("/images/<pth:re:.*\\.png>")
|
2019-03-28 21:25:45 +03:00
|
|
|
@webserver.route("/images/<pth:re:.*\\.gif>")
|
2018-12-17 17:10:10 +03:00
|
|
|
def static_image(pth):
|
2019-12-12 23:24:13 +03:00
|
|
|
if globalconf.USE_THUMBOR:
|
2020-12-25 07:24:59 +03:00
|
|
|
return static_file(pth,root=data_dir['images']())
|
2019-12-12 23:24:13 +03:00
|
|
|
|
|
|
|
type = pth.split(".")[-1]
|
2019-05-13 19:35:41 +03:00
|
|
|
small_pth = pth + "-small"
|
2020-12-25 06:52:05 +03:00
|
|
|
if os.path.exists(data_dir['images'](small_pth)):
|
2020-12-25 07:24:59 +03:00
|
|
|
response = static_file(small_pth,root=data_dir['images']())
|
2019-02-17 14:49:06 +03:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
from wand.image import Image
|
2020-12-25 06:52:05 +03:00
|
|
|
img = Image(filename=data_dir['images'](pth))
|
2019-02-17 14:49:06 +03:00
|
|
|
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))
|
2020-12-25 06:52:05 +03:00
|
|
|
img.save(filename=data_dir['images'](small_pth))
|
2020-12-25 07:24:59 +03:00
|
|
|
response = static_file(small_pth,root=data_dir['images']())
|
2019-02-17 14:49:06 +03:00
|
|
|
else:
|
2020-12-25 07:24:59 +03:00
|
|
|
response = static_file(pth,root=data_dir['images']())
|
2019-02-17 14:49:06 +03:00
|
|
|
except:
|
2020-12-25 07:24:59 +03:00
|
|
|
response = static_file(pth,root=data_dir['images']())
|
2019-03-14 13:07:20 +03:00
|
|
|
|
2019-02-17 14:49:06 +03:00
|
|
|
#response = static_file("images/" + pth,root="")
|
2019-05-14 22:58:18 +03:00
|
|
|
response.set_header("Cache-Control", "public, max-age=86400")
|
2019-12-12 23:24:13 +03:00
|
|
|
response.set_header("Content-Type", "image/" + type)
|
2019-02-17 14:49:06 +03:00
|
|
|
return response
|
2018-12-17 17:10:10 +03:00
|
|
|
|
2019-12-04 21:14:33 +03:00
|
|
|
|
2019-12-04 22:41:53 +03:00
|
|
|
@webserver.route("/style.css")
|
2019-12-04 21:14:33 +03:00
|
|
|
def get_css():
|
|
|
|
response.content_type = 'text/css'
|
2020-09-01 01:38:30 +03:00
|
|
|
global css
|
2020-09-01 01:35:14 +03:00
|
|
|
if settings.get_settings("DEV_MODE"): css = generate_css()
|
|
|
|
return css
|
2019-12-04 21:14:33 +03:00
|
|
|
|
2019-12-04 22:41:53 +03:00
|
|
|
|
2020-07-29 16:52:01 +03:00
|
|
|
@webserver.route("/login")
|
|
|
|
def login():
|
2020-07-29 22:19:29 +03:00
|
|
|
return auth.get_login_page()
|
2020-07-29 16:52:01 +03:00
|
|
|
|
2019-12-04 22:41:53 +03:00
|
|
|
@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)
|
2019-05-14 22:58:18 +03:00
|
|
|
response.set_header("Cache-Control", "public, max-age=3600")
|
2019-02-17 14:49:06 +03:00
|
|
|
return response
|
2019-03-14 13:07:20 +03:00
|
|
|
|
2019-11-24 23:47:03 +03:00
|
|
|
|
2020-08-18 05:55:36 +03:00
|
|
|
aliases = {
|
|
|
|
"admin": "admin_overview",
|
2020-08-21 19:06:16 +03:00
|
|
|
"manual": "admin_manual",
|
|
|
|
"setup": "admin_setup",
|
|
|
|
"issues": "admin_issues"
|
2020-08-18 05:55:36 +03:00
|
|
|
}
|
2020-05-13 23:57:55 +03:00
|
|
|
|
|
|
|
|
2020-05-17 02:58:24 +03:00
|
|
|
|
|
|
|
|
2020-08-17 19:16:09 +03:00
|
|
|
@webserver.route("/<name:re:admin.*>")
|
2020-07-29 22:19:29 +03:00
|
|
|
@auth.authenticated
|
2020-07-29 16:52:01 +03:00
|
|
|
def static_html_private(name):
|
|
|
|
return static_html(name)
|
|
|
|
|
2018-12-19 17:28:10 +03:00
|
|
|
@webserver.route("/<name>")
|
2020-07-29 16:52:01 +03:00
|
|
|
def static_html_public(name):
|
|
|
|
return static_html(name)
|
|
|
|
|
2018-11-29 18:53:25 +03:00
|
|
|
def static_html(name):
|
2020-08-18 05:55:36 +03:00
|
|
|
if name in aliases: redirect(aliases[name])
|
2019-12-04 22:41:53 +03:00
|
|
|
linkheaders = ["</style.css>; rel=preload; as=style"]
|
2019-04-08 14:04:31 +03:00
|
|
|
keys = remove_identical(FormsDict.decode(request.query))
|
2019-03-14 13:07:20 +03:00
|
|
|
|
2020-07-29 22:19:29 +03:00
|
|
|
adminmode = request.cookies.get("adminmode") == "true" and auth.check(request)
|
2019-11-20 07:19:02 +03:00
|
|
|
|
2019-11-21 08:31:23 +03:00
|
|
|
clock = Clock()
|
|
|
|
clock.start()
|
|
|
|
|
2020-08-30 19:56:28 +03:00
|
|
|
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)
|
2019-05-21 18:29:07 +03:00
|
|
|
|
2020-08-31 00:49:14 +03:00
|
|
|
template = jinja_environment.get_template(name + '.jinja')
|
2020-09-04 17:36:35 +03:00
|
|
|
try:
|
|
|
|
res = template.render(**LOCAL_CONTEXT)
|
|
|
|
except ValueError as e:
|
|
|
|
abort(404,"Entity does not exist")
|
2020-08-31 00:49:14 +03:00
|
|
|
|
2020-09-01 01:35:14 +03:00
|
|
|
if settings.get_settings("DEV_MODE"): jinja_environment.cache.clear()
|
|
|
|
|
2020-11-11 19:45:40 +03:00
|
|
|
log("Generated page {name} in {time:.5f}s".format(name=name,time=clock.stop()),module="debug_performance")
|
2020-09-23 17:26:54 +03:00
|
|
|
return clean_html(res)
|
2018-11-24 18:29:24 +03:00
|
|
|
|
2019-06-24 13:48:45 +03:00
|
|
|
|
|
|
|
# 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("/")))
|
|
|
|
|
2018-12-05 16:30:50 +03:00
|
|
|
#set graceful shutdown
|
2018-11-28 15:02:43 +03:00
|
|
|
signal.signal(signal.SIGINT, graceful_exit)
|
2018-12-08 02:01:44 +03:00
|
|
|
signal.signal(signal.SIGTERM, graceful_exit)
|
2018-11-24 18:29:24 +03:00
|
|
|
|
2019-02-16 23:21:29 +03:00
|
|
|
#rename process, this is now required for the daemon manager to work
|
|
|
|
setproctitle.setproctitle("Maloja")
|
2019-03-14 13:07:20 +03:00
|
|
|
|
2019-05-13 11:45:30 +03:00
|
|
|
## start database
|
2019-05-12 19:39:46 +03:00
|
|
|
database.start_db()
|
2018-11-24 18:29:24 +03:00
|
|
|
|
2019-03-14 13:07:20 +03:00
|
|
|
log("Starting up Maloja server...")
|
2021-05-24 14:49:58 +03:00
|
|
|
|
|
|
|
try:
|
|
|
|
#run(webserver, host=HOST, port=MAIN_PORT, server='waitress')
|
|
|
|
waitress.serve(webserver, host=HOST, port=MAIN_PORT, threads=THREADS)
|
|
|
|
except OSError:
|
|
|
|
log("Error. Is another Maloja process already running?")
|
|
|
|
raise
|