2021-02-12 18:24:30 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
# Launch as a eventlet.wsgi server instance.
|
|
|
|
|
|
|
|
import getopt
|
2021-04-09 02:49:50 +00:00
|
|
|
import os
|
2021-02-12 18:24:30 +00:00
|
|
|
import sys
|
|
|
|
|
|
|
|
import eventlet
|
|
|
|
import eventlet.wsgi
|
|
|
|
import backend
|
|
|
|
|
2021-02-12 18:43:05 +00:00
|
|
|
from backend import store
|
|
|
|
|
|
|
|
|
2021-04-30 06:47:13 +00:00
|
|
|
def init_app_secret(datastore_path):
|
|
|
|
secret = ""
|
|
|
|
|
|
|
|
path = "{}/secret.txt".format(datastore_path)
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(path, "r") as f:
|
|
|
|
secret = f.read()
|
|
|
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
|
|
|
import secrets
|
|
|
|
with open(path, "w") as f:
|
|
|
|
secret = secrets.token_hex(32)
|
|
|
|
f.write(secret)
|
|
|
|
|
|
|
|
return secret
|
|
|
|
|
|
|
|
|
2021-02-12 18:24:30 +00:00
|
|
|
def main(argv):
|
|
|
|
ssl_mode = False
|
|
|
|
port = 5000
|
2021-04-09 02:49:50 +00:00
|
|
|
|
|
|
|
# Must be absolute so that send_from_directory doesnt try to make it relative to backend/
|
|
|
|
datastore_path = os.path.join(os.getcwd(), "datastore")
|
2021-02-12 18:24:30 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
opts, args = getopt.getopt(argv, "sd:p:", "purge")
|
|
|
|
except getopt.GetoptError:
|
2021-02-12 18:43:05 +00:00
|
|
|
print('backend.py -s SSL enable -p [port] -d [datastore path]')
|
2021-02-12 18:24:30 +00:00
|
|
|
sys.exit(2)
|
|
|
|
|
|
|
|
for opt, arg in opts:
|
2021-02-12 18:43:05 +00:00
|
|
|
# if opt == '--purge':
|
|
|
|
# Remove history, the actual files you need to delete manually.
|
|
|
|
# for uuid, watch in datastore.data['watching'].items():
|
|
|
|
# watch.update({'history': {}, 'last_checked': 0, 'last_changed': 0, 'previous_md5': None})
|
2021-02-12 18:24:30 +00:00
|
|
|
|
|
|
|
if opt == '-s':
|
|
|
|
ssl_mode = True
|
|
|
|
|
|
|
|
if opt == '-p':
|
2021-02-21 12:40:48 +00:00
|
|
|
port = int(arg)
|
2021-02-12 18:24:30 +00:00
|
|
|
|
|
|
|
if opt == '-d':
|
|
|
|
datastore_path = arg
|
|
|
|
|
2021-02-16 20:35:28 +00:00
|
|
|
# isnt there some @thingy to attach to each route to tell it, that this route needs a datastore
|
2021-02-12 18:43:05 +00:00
|
|
|
app_config = {'datastore_path': datastore_path}
|
2021-02-16 20:35:28 +00:00
|
|
|
|
2021-02-12 18:43:05 +00:00
|
|
|
datastore = store.ChangeDetectionStore(datastore_path=app_config['datastore_path'])
|
|
|
|
app = backend.changedetection_app(app_config, datastore)
|
2021-02-12 18:24:30 +00:00
|
|
|
|
2021-04-30 15:18:59 +00:00
|
|
|
app.config['datastore_path'] = datastore_path
|
2021-04-30 06:47:13 +00:00
|
|
|
app.secret_key = init_app_secret(app_config['datastore_path'])
|
|
|
|
|
2021-02-24 13:44:35 +00:00
|
|
|
@app.context_processor
|
|
|
|
def inject_version():
|
2021-04-30 09:54:23 +00:00
|
|
|
return dict(version=datastore.data['version_tag'],
|
|
|
|
new_version_available=app.config['NEW_VERSION_AVAILABLE'],
|
|
|
|
has_password=datastore.data['settings']['application']['password'] != False
|
|
|
|
)
|
2021-02-24 13:44:35 +00:00
|
|
|
|
2021-02-12 18:24:30 +00:00
|
|
|
if ssl_mode:
|
2021-02-12 18:43:05 +00:00
|
|
|
# @todo finalise SSL config, but this should get you in the right direction if you need it.
|
2021-02-12 18:24:30 +00:00
|
|
|
eventlet.wsgi.server(eventlet.wrap_ssl(eventlet.listen(('', port)),
|
|
|
|
certfile='cert.pem',
|
|
|
|
keyfile='privkey.pem',
|
|
|
|
server_side=True), app)
|
|
|
|
|
|
|
|
else:
|
2021-02-12 18:43:05 +00:00
|
|
|
eventlet.wsgi.server(eventlet.listen(('', port)), app)
|
2021-02-12 18:24:30 +00:00
|
|
|
|
|
|
|
|
2021-02-12 18:43:05 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main(sys.argv[1:])
|