husstanden/main.py

52 lines
1.5 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__)
import routes # All flask app routes
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":
return "No"
data = json.loads( request.data.decode() )
if data["secret"] != glob.config["git"]["secret"]:
return "No"
import subprocess, os, sys
process = subprocess.Popen(["git", "pull"], stdout=subprocess.PIPE)
output = process.communicate()[0]
if output.decode().split("\n")[-2].find("fail") != -1:
return "Failed to pull changes"
# If needed; every open file or connection HAVE to be closed at this point
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)
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"])