husstanden/main.py

56 lines
1.7 KiB
Python
Raw Normal View History

2019-02-25 11:37:53 +01:00
import json
from flask import Flask, url_for, request
2019-02-12 16:59:43 +01:00
from objects import glob # Global sharing of python objects in a manageable way
glob.app = Flask(__name__)
2019-04-26 03:28:27 +02:00
glob.app.secret_key = "E2FGrJXLtOxPh70Q"
2019-02-12 16:59:43 +01:00
import routes # All flask app routes
import filters # All flask app filters
2019-02-12 16:59:43 +01:00
2019-02-25 11:37:53 +01:00
if glob.config["git"]["auto_pull_and_restart"]: # Only used on the VPS (Do not enable in config)
@glob.app.route(glob.config["git"]["webhook_endpoint"], methods = ["POST"])
def git_fetch():
if request.method != "POST":
2019-03-02 21:39:36 +01:00
return "No", 400
2019-02-25 11:37:53 +01:00
data = json.loads( request.data.decode() )
if data["secret"] != glob.config["git"]["secret"]:
2019-03-02 21:39:36 +01:00
return "No", 400
from subprocess import check_output
output = check_output(["git", "pull", "origin", "master"])
print( "GIT: {}".format( output.decode() ) )
2019-02-25 11:37:53 +01:00
if output.decode().split("\n")[-2].find("fail") != -1:
2019-03-02 21:39:36 +01:00
return "Failed to pull changes", 500
2019-02-25 11:37:53 +01:00
# If needed; every open file or connection HAVE to be closed at this point
2019-03-02 21:39:36 +01:00
import os, sys
2019-02-25 11:37:53 +01:00
os.execv(sys.executable, ["python"] + sys.argv) # Restart service
2019-02-12 16:59:43 +01:00
if glob.config["disable-static-cache"]:
import os
@glob.app.context_processor
def override_url_for():
"""
Generate a new token on every request to prevent the browser from
caching static files.
"""
return dict(url_for=dated_url_for)
2019-03-02 21:39:36 +01:00
2019-02-12 16:59:43 +01:00
def dated_url_for(endpoint, **values):
if endpoint == "static":
filename = values.get("filename", None)
if filename and not filename.startswith("const/"):
2019-02-12 16:59:43 +01:00
file_path = os.path.join(glob.app.root_path,
endpoint, filename)
values["q"] = int(os.stat(file_path).st_mtime)
2019-02-12 16:59:43 +01:00
return url_for(endpoint, **values)
if __name__ == "__main__":
glob.app.run(**glob.config["web"])