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

54 lines
1.5 KiB
Python
Raw Normal View History

2018-11-28 14:16:13 +03:00
from bottle import route, 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-27 18:21:33 +03:00
import sys
2018-11-24 18:29:24 +03:00
MAIN_PORT = 12345
DATABASE_PORT = 12349
@route("")
@route("/")
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; }
@route("/db/<pth:path>")
def database(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]) + "&"
contents = urllib.request.urlopen("http://localhost:" + str(DATABASE_PORT) + "/" + pth + keystring).read()
2018-11-25 16:49:53 +03:00
response.content_type = "application/json"
response.set_header("Access-Control-Allow-Origin","*")
2018-11-25 16:49:53 +03:00
#print("Returning " + "http://localhost:" + str(DATABASE_PORT) + "/" + pth)
return contents
@route("/exit")
def shutdown():
urllib.request.urlopen("http://localhost:" + str(DATABASE_PORT) + "/flush")
print("Server shutting down...")
sys.exit()
@route("/<pth:path>")
def static(pth):
return static_file(pth,root="")
2018-11-24 18:29:24 +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,))
run(host='0.0.0.0', port=MAIN_PORT, server='waitress')