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

180 lines
5.4 KiB
Python
Raw Normal View History

2018-12-05 16:30:50 +03:00
#!/usr/bin/env python
# server stuff
2019-02-02 18:54:01 +03:00
from bottle import Bottle, route, get, post, error, run, template, static_file, request, response, FormsDict, redirect, template
import waitress
# rest of the project
2018-12-26 19:42:55 +03:00
from htmlgenerators import removeIdentical
2019-01-10 01:29:01 +03:00
from utilities import *
from htmlgenerators import KeySplit
# 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.request
import urllib.parse
from urllib.error import *
2018-11-24 18:29:24 +03:00
2018-11-29 15:43:45 +03:00
MAIN_PORT = 42010
DATABASE_PORT = 42011
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
@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
# this is the fallback option. If you run this service behind a reverse proxy, it is recommended to rewrite /db/ requests to the port of the db server
# e.g. location /db { rewrite ^/db(.*)$ $1 break; proxy_pass http://yoururl:12349; }
2018-12-19 17:28:10 +03:00
@webserver.get("/db/<pth:path>")
2018-11-30 17:44:30 +03:00
def database_get(pth):
2018-11-28 14:16:13 +03:00
keys = FormsDict.decode(request.query) # The Dal★Shabet handler
2018-11-26 18:21:07 +03:00
keystring = "?"
for k in keys:
keystring += urllib.parse.quote(k) + "=" + urllib.parse.quote(keys[k]) + "&"
response.set_header("Access-Control-Allow-Origin","*")
try:
2019-03-06 19:50:36 +03:00
proxyresponse = urllib.request.urlopen("http://[::1]:" + str(DATABASE_PORT) + "/" + pth + keystring)
contents = proxyresponse.read()
response.status = proxyresponse.getcode()
response.content_type = "application/json"
return contents
except HTTPError as e:
response.status = e.code
return
2018-11-30 15:39:12 +03:00
2018-12-19 17:28:10 +03:00
@webserver.post("/db/<pth:path>")
2018-11-30 17:44:30 +03:00
def database_post(pth):
2018-11-30 18:01:32 +03:00
response.set_header("Access-Control-Allow-Origin","*")
2018-11-30 17:44:30 +03:00
try:
2019-03-06 19:50:36 +03:00
proxyresponse = urllib.request.urlopen("http://[::1]:" + str(DATABASE_PORT) + "/" + pth,request.body)
2018-11-30 17:44:30 +03:00
contents = proxyresponse.read()
response.status = proxyresponse.getcode()
2018-11-30 18:01:32 +03:00
response.content_type = "application/json"
return contents
2018-11-30 17:44:30 +03:00
except HTTPError as e:
response.status = e.code
2018-11-30 18:01:32 +03:00
return
2018-11-30 17:44:30 +03:00
2018-11-30 18:01:32 +03:00
return
2018-11-25 16:49:53 +03:00
2019-02-16 22:10:12 +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")
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, _, _, _ = KeySplit(keys)
result = resolveImage(**relevant)
2019-03-06 20:04:12 +03:00
if result == "": return ""
2019-02-22 17:03:38 +03:00
redirect(result,301)
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>")
2018-12-17 17:10:10 +03:00
def static_image(pth):
small_pth = pth.split(".")
small_pth.insert(-1,"small")
small_pth = ".".join(small_pth)
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=604800")
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>")
@webserver.route("/<name:re:.*\\.png>")
@webserver.route("/<name:re:.*\\.jpeg>")
2019-02-02 20:08:30 +03:00
@webserver.route("/<name:re:.*\\.ico>")
2018-12-17 17:10:10 +03:00
def static(name):
response = static_file("website/" + name,root="")
response.set_header("Cache-Control", "public, max-age=604800")
return response
2018-12-17 17:10:10 +03:00
2018-12-19 17:28:10 +03:00
@webserver.route("/<name>")
2018-11-29 18:53:25 +03:00
def static_html(name):
linkheaders = ["</maloja.css>; rel=preload; as=style"]
2018-12-24 23:25:09 +03:00
keys = removeIdentical(FormsDict.decode(request.query))
2018-12-17 17:10:10 +03:00
2019-03-08 15:36:26 +03:00
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
2019-03-08 15:36:26 +03:00
2018-12-17 17:10:10 +03:00
# If a python file exists, it provides the replacement dict for the html file
2018-12-16 19:52:13 +03:00
if os.path.exists("website/" + name + ".py"):
#txt_keys = SourceFileLoader(name,"website/" + name + ".py").load_module().replacedict(keys,DATABASE_PORT)
txt_keys,resources = SourceFileLoader(name,"website/" + name + ".py").load_module().instructions(keys)
# add headers for server push
for resource in resources:
linkheaders.append("<" + resource["file"] + ">; rel=preload; as=" + resource["type"])
# apply key substitutions
2019-03-08 15:36:26 +03:00
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])
response.set_header("Link",",".join(linkheaders))
2019-03-08 15:36:26 +03:00
return html
#return static_file("website/" + name + ".html",root="")
2018-11-24 18:29:24 +03:00
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")
2018-12-05 16:30:50 +03:00
## start database server
2018-11-24 18:29:24 +03:00
_thread.start_new_thread(SourceFileLoader("database","database.py").load_module().runserver,(DATABASE_PORT,))
2018-12-26 19:42:55 +03:00
run(webserver, host='::', port=MAIN_PORT, server='waitress')