You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
changedetection.io/backend/backend.py

90 lines
2.2 KiB

4 years ago
#!/usr/bin/python3
import json
import eventlet
import eventlet.wsgi
import time
import os
import getopt
import sys
import datetime
4 years ago
from flask import Flask, render_template, request, send_file, send_from_directory, safe_join, abort, redirect, url_for
4 years ago
# Local
import store
4 years ago
datastore = store.ChangeDetectionStore()
4 years ago
messages = []
4 years ago
app = Flask(__name__, static_url_path='/static')
app.config['STATIC_RESOURCES'] = "/app/static"
# app.config['SECRET_KEY'] = 'secret!'
# Disables caching of the templates
app.config['TEMPLATES_AUTO_RELOAD'] = True
@app.route("/", methods=['GET'])
def main_page():
4 years ago
global messages
# Show messages but once.
output = render_template("watch-overview.html", watches=datastore.data['watching'], messages=messages)
messages = []
return output
4 years ago
@app.route("/static/<string:group>/<string:filename>", methods=['GET'])
def static_content(group, filename):
try:
return send_from_directory("/app/static/{}".format(group), filename=filename)
except FileNotFoundError:
abort(404)
4 years ago
@app.route("/api/add", methods=['POST'])
def api_watch_add():
global messages
#@todo add_watch should throw a custom Exception for validation etc
datastore.add_watch(url=request.form.get('url'), tag=request.form.get('tag'))
messages.append({'class':'ok', 'message': 'Saved'})
return redirect(url_for('main_page'))
# datastore.add_watch
4 years ago
def main(argv):
ssl_mode = False
port = 5000
try:
opts, args = getopt.getopt(argv, "sp:")
except getopt.GetoptError:
print('backend.py -s SSL enable -p [port]')
sys.exit(2)
for opt, arg in opts:
if opt == '-s':
ssl_mode = True
if opt == '-p':
port = arg
# @todo finalise SSL config, but this should get you in the right direction if you need it.
if ssl_mode:
eventlet.wsgi.server(eventlet.wrap_ssl(eventlet.listen(('', port)),
certfile='cert.pem',
keyfile='privkey.pem',
server_side=True), app)
else:
eventlet.wsgi.server(eventlet.listen(('', port)), app)
if __name__ == '__main__':
main(sys.argv[1:])