2018-12-05 16:30:50 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2018-12-19 17:28:10 +03:00
|
|
|
from bottle import Bottle, route, get, post, error, run, template, static_file, request, response, FormsDict
|
2018-11-24 18:29:24 +03:00
|
|
|
from importlib.machinery import SourceFileLoader
|
|
|
|
import _thread
|
|
|
|
import waitress
|
2018-11-25 16:49:53 +03:00
|
|
|
import urllib.request
|
2018-11-26 18:21:07 +03:00
|
|
|
import urllib.parse
|
2018-11-30 17:44:30 +03:00
|
|
|
from urllib.error import *
|
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
|
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():
|
2018-11-25 16:49:53 +03:00
|
|
|
return static_file("main.html",root="")
|
|
|
|
|
|
|
|
|
|
|
|
# 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:
|
|
|
|
proxyresponse = urllib.request.urlopen("http://localhost:" + 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:
|
|
|
|
proxyresponse = urllib.request.urlopen("http://localhost:" + str(DATABASE_PORT) + "/" + pth,request.body)
|
|
|
|
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
|
|
|
|
2018-12-19 17:28:10 +03:00
|
|
|
@webserver.route("/exit")
|
2018-11-27 18:08:14 +03:00
|
|
|
def shutdown():
|
2018-11-28 15:02:43 +03:00
|
|
|
graceful_exit()
|
|
|
|
|
|
|
|
def graceful_exit(sig=None,frame=None):
|
|
|
|
urllib.request.urlopen("http://localhost:" + str(DATABASE_PORT) + "/sync")
|
2018-11-27 18:08:14 +03:00
|
|
|
print("Server shutting down...")
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
|
2018-12-19 17:28:10 +03:00
|
|
|
@webserver.route("/info/<pth:re:.*\\.jpeg>")
|
|
|
|
@webserver.route("/info/<pth:re:.*\\.jpg>")
|
|
|
|
@webserver.route("/info/<pth:re:.*\\.png>")
|
2018-12-17 17:10:10 +03:00
|
|
|
def static_image(pth):
|
|
|
|
return static_file("info/" + pth,root="")
|
|
|
|
|
2018-12-19 17:28:10 +03:00
|
|
|
@webserver.route("/<name:re:.*\\.html>")
|
|
|
|
@webserver.route("/<name:re:.*\\.js>")
|
|
|
|
@webserver.route("/<name:re:.*\\.css>")
|
|
|
|
@webserver.route("/<name:re:.*\\.png>")
|
|
|
|
@webserver.route("/<name:re:.*\\.jpeg>")
|
2018-12-17 17:10:10 +03:00
|
|
|
def static(name):
|
2018-11-29 18:53:25 +03:00
|
|
|
return static_file("website/" + name,root="")
|
|
|
|
|
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):
|
2018-12-17 01:56:30 +03:00
|
|
|
keys = FormsDict.decode(request.query)
|
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"):
|
2018-12-17 01:56:30 +03:00
|
|
|
txt_keys = SourceFileLoader(name,"website/" + name + ".py").load_module().replacedict(keys,DATABASE_PORT)
|
|
|
|
with open("website/" + name + ".html") as htmlfile:
|
|
|
|
html = htmlfile.read()
|
|
|
|
for k in txt_keys:
|
|
|
|
html = html.replace(k,txt_keys[k])
|
|
|
|
return html
|
|
|
|
|
|
|
|
|
2018-12-17 17:10:10 +03:00
|
|
|
# Otherwise, we just serve the html file
|
2018-11-29 18:53:25 +03:00
|
|
|
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
|
|
|
|
2018-12-05 16:30:50 +03:00
|
|
|
#rename process, not important
|
|
|
|
try:
|
|
|
|
import setproctitle
|
|
|
|
setproctitle.setproctitle("Maloja")
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2018-11-27 18:08:14 +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-19 17:28:10 +03:00
|
|
|
run(webserver, host='0.0.0.0', port=MAIN_PORT, server='waitress')
|