2018-12-05 16:30:50 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2019-02-20 23:10:58 +03:00
|
|
|
# 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
|
2019-02-20 23:10:58 +03:00
|
|
|
import waitress
|
|
|
|
# rest of the project
|
2019-03-28 16:18:12 +03:00
|
|
|
import database
|
2019-01-10 01:29:01 +03:00
|
|
|
from utilities import *
|
2019-04-08 14:04:31 +03:00
|
|
|
from urihandler import uri_to_internal, remove_identical
|
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-02-20 23:10:58 +03:00
|
|
|
# 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
|
2019-02-20 23:10:58 +03:00
|
|
|
# url handling
|
|
|
|
import urllib.request
|
|
|
|
import urllib.parse
|
|
|
|
from urllib.error import *
|
2018-11-24 18:29:24 +03:00
|
|
|
|
|
|
|
|
2019-02-17 14:49:06 +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-03-28 17:06:14 +03:00
|
|
|
MAIN_PORT, DATABASE_PORT = settings.get_settings("WEB_PORT","API_PORT")
|
|
|
|
|
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():
|
2019-02-17 16:25:40 +03:00
|
|
|
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:])
|
|
|
|
log("Error: " + str(code),module="error")
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
# 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]) + "&"
|
2018-11-27 18:08:14 +03:00
|
|
|
response.set_header("Access-Control-Allow-Origin","*")
|
2018-12-14 21:52:31 +03:00
|
|
|
try:
|
2019-03-06 19:50:36 +03:00
|
|
|
proxyresponse = urllib.request.urlopen("http://[::1]:" + str(DATABASE_PORT) + "/" + pth + keystring)
|
2018-12-14 21:52:31 +03:00
|
|
|
contents = proxyresponse.read()
|
|
|
|
response.status = proxyresponse.getcode()
|
|
|
|
response.content_type = "application/json"
|
|
|
|
return contents
|
|
|
|
except HTTPError as e:
|
|
|
|
response.status = e.code
|
|
|
|
return
|
2019-03-14 13:07:20 +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
|
2019-03-14 13:07:20 +03:00
|
|
|
|
2018-11-30 18:01:32 +03:00
|
|
|
return
|
2018-11-25 16:49:53 +03:00
|
|
|
|
2019-03-14 13:07:20 +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")
|
|
|
|
database.sync()
|
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)
|
2019-04-08 14:04:31 +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-02-17 14:49:06 +03:00
|
|
|
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="")
|
2019-03-14 13:07:20 +03:00
|
|
|
|
2019-02-17 14:49:06 +03:00
|
|
|
#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
|
|
|
|
2019-02-17 14:49:06 +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>")
|
2019-03-14 13:07:20 +03:00
|
|
|
def static(name):
|
2019-02-17 14:49:06 +03:00
|
|
|
response = static_file("website/" + name,root="")
|
|
|
|
response.set_header("Cache-Control", "public, max-age=604800")
|
|
|
|
return response
|
2019-03-14 13:07:20 +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):
|
2019-03-28 19:57:56 +03:00
|
|
|
linkheaders = ["</css/maloja.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
|
|
|
|
2019-03-08 15:36:26 +03:00
|
|
|
with open("website/" + name + ".html") as htmlfile:
|
|
|
|
html = htmlfile.read()
|
2019-03-14 13:07:20 +03:00
|
|
|
|
2019-03-08 15:36:26 +03:00
|
|
|
# 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-14 13:07:20 +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"):
|
2019-02-17 16:25:40 +03:00
|
|
|
#txt_keys = SourceFileLoader(name,"website/" + name + ".py").load_module().replacedict(keys,DATABASE_PORT)
|
2019-02-21 11:43:35 +03:00
|
|
|
txt_keys,resources = SourceFileLoader(name,"website/" + name + ".py").load_module().instructions(keys)
|
2019-03-14 13:07:20 +03:00
|
|
|
|
2019-02-17 16:25:40 +03:00
|
|
|
# add headers for server push
|
|
|
|
for resource in resources:
|
2019-04-03 17:03:48 +03:00
|
|
|
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"])
|
2019-03-14 13:07:20 +03:00
|
|
|
|
2019-02-17 16:25:40 +03:00
|
|
|
# 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])
|
|
|
|
|
2019-03-14 13:07:20 +03:00
|
|
|
|
2019-02-17 16:25:40 +03:00
|
|
|
response.set_header("Link",",".join(linkheaders))
|
2019-03-14 13:07:20 +03:00
|
|
|
|
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")
|
2019-03-14 13:07:20 +03:00
|
|
|
|
2018-11-27 18:08:14 +03:00
|
|
|
## start database server
|
2019-03-28 16:18:12 +03:00
|
|
|
#_thread.start_new_thread(SourceFileLoader("database","database.py").load_module().runserver,(DATABASE_PORT,))
|
|
|
|
_thread.start_new_thread(database.runserver,(DATABASE_PORT,))
|
2018-11-24 18:29:24 +03:00
|
|
|
|
2019-03-14 13:07:20 +03:00
|
|
|
log("Starting up Maloja server...")
|
2018-12-26 19:42:55 +03:00
|
|
|
run(webserver, host='::', port=MAIN_PORT, server='waitress')
|