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

249 lines
7.3 KiB
Python
Raw Normal View History

2018-12-05 16:30:50 +03:00
#!/usr/bin/env python
# server stuff
from bottle import Bottle, route, get, post, error, run, template, static_file, request, response, FormsDict, redirect, template, HTTPResponse
import waitress
2019-05-08 18:42:56 +03:00
# monkey patching
import monkey
# rest of the project
import database
import htmlmodules
2019-05-21 19:03:57 +03:00
import htmlgenerators
import malojatime
2019-10-24 16:52:06 +03:00
import utilities
2019-06-24 13:22:47 +03:00
from utilities import resolveImage
from urihandler import uri_to_internal, remove_identical
2019-05-23 17:39:30 +03:00
import urihandler
2019-11-19 22:52:07 +03:00
import info
# doreah toolkit
from doreah import settings
2019-03-29 21:44:42 +03:00
from doreah.logging import log
# technical
from importlib.machinery import SourceFileLoader
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
# url handling
import urllib
2018-11-24 18:29:24 +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")
HOST = settings.get_settings("HOST")
2018-11-24 18:29:24 +03:00
2018-12-19 17:28:10 +03:00
webserver = Bottle()
2018-11-24 18:29:24 +03:00
2018-12-19 17:28:10 +03:00
2019-10-11 06:17:15 +03:00
import lesscpy
css = ""
for f in os.listdir("website/less"):
css += lesscpy.compile("website/less/" + f)
os.makedirs("website/css",exist_ok=True)
with open("website/css/style.css","w") as f:
f.write(css)
2018-12-19 17:28:10 +03:00
@webserver.route("")
@webserver.route("/")
2018-11-24 18:29:24 +03:00
def mainpage():
response = static_html("start")
return response
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):
code = int(str(error).split(",")[0][1:])
2019-04-11 18:44:33 +03:00
log("HTTP Error: " + str(code),module="error")
2019-04-03 19:03:55 +03:00
if os.path.exists("website/errors/" + str(code) + ".html"):
return static_file("website/errors/" + str(code) + ".html",root="")
else:
with open("website/errors/generic.html") as htmlfile:
html = htmlfile.read()
# apply global substitutions
with open("website/common/footer.html") as footerfile:
footerhtml = footerfile.read()
with open("website/common/header.html") as headerfile:
headerhtml = headerfile.read()
html = html.replace("</body>",footerhtml + "</body>").replace("</head>",headerhtml + "</head>")
html = html.replace("ERROR_CODE",str(code))
return html
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):
#urllib.request.urlopen("http://[::1]:" + str(DATABASE_PORT) + "/sync")
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)
@webserver.route("/image")
def dynamic_image():
keys = FormsDict.decode(request.query)
relevant, _, _, _ = uri_to_internal(keys)
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)
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>")
@webserver.route("/images/<pth:re:.*\\.gif>")
2018-12-17 17:10:10 +03:00
def static_image(pth):
small_pth = pth + "-small"
if os.path.exists("images/" + small_pth):
response = static_file("images/" + small_pth,root="")
else:
try:
from wand.image import Image
img = Image(filename="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="images/" + small_pth)
response = static_file("images/" + small_pth,root="")
else:
response = static_file("images/" + pth,root="")
except:
response = static_file("images/" + pth,root="")
#response = static_file("images/" + pth,root="")
response.set_header("Cache-Control", "public, max-age=86400")
return response
2018-12-17 17:10:10 +03:00
#@webserver.route("/<name:re:.*\\.html>")
2018-12-19 17:28:10 +03:00
@webserver.route("/<name:re:.*\\.js>")
@webserver.route("/<name:re:.*\\.css>")
2019-10-08 19:55:37 +03:00
@webserver.route("/<name:re:.*\\.less>")
2018-12-19 17:28:10 +03:00
@webserver.route("/<name:re:.*\\.png>")
@webserver.route("/<name:re:.*\\.jpeg>")
2019-02-02 20:08:30 +03:00
@webserver.route("/<name:re:.*\\.ico>")
2019-05-26 19:35:24 +03:00
@webserver.route("/<name:re:.*\\.txt>")
def static(name):
response = static_file("website/" + name,root="")
response.set_header("Cache-Control", "public, max-age=3600")
return response
2018-12-19 17:28:10 +03:00
@webserver.route("/<name>")
2018-11-29 18:53:25 +03:00
def static_html(name):
2019-10-11 06:17:15 +03:00
linkheaders = ["</css/style.css>; rel=preload; as=style"]
keys = remove_identical(FormsDict.decode(request.query))
pyhp_file = os.path.exists("website/" + name + ".pyhp")
html_file = os.path.exists("website/" + name + ".html")
pyhp_pref = settings.get_settings("USE_PYHP")
2019-11-20 07:19:02 +03:00
adminmode = request.cookies.get("adminmode") == "true" and database.checkAPIkey(request.cookies.get("apikey")) is not False
# if a pyhp file exists, use this
if (pyhp_file and pyhp_pref) or (pyhp_file and not html_file):
from doreah.pyhp import file
environ = {} #things we expose to the pyhp pages
2019-11-20 07:19:02 +03:00
environ["adminmode"] = adminmode
# maloja
environ["db"] = database
environ["htmlmodules"] = htmlmodules
2019-05-21 19:03:57 +03:00
environ["htmlgenerators"] = htmlgenerators
environ["malojatime"] = malojatime
environ["utilities"] = utilities
2019-05-23 17:39:30 +03:00
environ["urihandler"] = urihandler
2019-11-19 22:52:07 +03:00
environ["info"] = info
# external
environ["urllib"] = urllib
# request
environ["filterkeys"], environ["limitkeys"], environ["delimitkeys"], environ["amountkeys"] = uri_to_internal(keys)
#response.set_header("Content-Type","application/xhtml+xml")
return file("website/" + name + ".pyhp",environ)
# if not, use the old way
else:
with open("website/" + name + ".html") as htmlfile:
html = htmlfile.read()
# apply global substitutions
with open("website/common/footer.html") as footerfile:
footerhtml = footerfile.read()
with open("website/common/header.html") as headerfile:
headerhtml = headerfile.read()
html = html.replace("</body>",footerhtml + "</body>").replace("</head>",headerhtml + "</head>")
2019-03-11 22:04:23 +03:00
# If a python file exists, it provides the replacement dict for the html file
if os.path.exists("website/" + name + ".py"):
#txt_keys = SourceFileLoader(name,"website/" + name + ".py").load_module().replacedict(keys,DATABASE_PORT)
try:
txt_keys,resources = SourceFileLoader(name,"website/" + name + ".py").load_module().instructions(keys)
except Exception as e:
log("Error in website generation: " + str(sys.exc_info()),module="error")
raise
# add headers for server push
for resource in resources:
if all(ord(c) < 128 for c in resource["file"]):
# we can only put ascii stuff in the http header
linkheaders.append("<" + resource["file"] + ">; rel=preload; as=" + resource["type"])
# apply key substitutions
for k in txt_keys:
if isinstance(txt_keys[k],list):
# if list, we replace each occurence with the next item
for element in txt_keys[k]:
html = html.replace(k,element,1)
else:
html = html.replace(k,txt_keys[k])
2019-03-08 15:36:26 +03:00
response.set_header("Link",",".join(linkheaders))
return html
#return static_file("website/" + name + ".html",root="")
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")
## start database
2019-05-12 19:39:46 +03:00
database.start_db()
2019-05-23 14:13:42 +03:00
database.dbserver.mount(server=webserver)
2018-11-24 18:29:24 +03:00
log("Starting up Maloja server...")
run(webserver, host=HOST, port=MAIN_PORT, server='waitress')