husstanden/main.py

54 lines
1.6 KiB
Python

import json
from flask import Flask, url_for, request
from objects import glob # Global sharing of python objects in a manageable way
glob.app = Flask(__name__)
import routes # All flask app routes
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":
return "No", 400
data = json.loads( request.data.decode() )
if data["secret"] != glob.config["git"]["secret"]:
return "No", 400
from subprocess import check_output
output = check_output(["git", "pull", "origin", "master"])
print( "GIT: {}".format( output.decode() ) )
if output.decode().split("\n")[-2].find("fail") != -1:
return "Failed to pull changes", 500
# If needed; every open file or connection HAVE to be closed at this point
import os, sys
os.execv(sys.executable, ["python"] + sys.argv) # Restart service
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)
def dated_url_for(endpoint, **values):
if endpoint == "static":
filename = values.get("filename", None)
if filename and not filename.startswith("const/"):
file_path = os.path.join(glob.app.root_path,
endpoint, filename)
values["q"] = int(os.stat(file_path).st_mtime)
return url_for(endpoint, **values)
if __name__ == "__main__":
glob.app.run(**glob.config["web"])