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.
63 lines
1.9 KiB
63 lines
1.9 KiB
import os |
|
import subprocess |
|
from flask import Flask, request, jsonify |
|
from flask_flatpages import FlatPages |
|
from flask import render_template |
|
|
|
FLATPAGES_AUTO_RELOAD = True |
|
|
|
app = Flask(__name__) |
|
app.config.from_object(__name__) |
|
pages = FlatPages(app) |
|
application = app |
|
pages.get('foo') |
|
|
|
|
|
@app.route('/') |
|
def index(): |
|
articles = (p for p in pages if 'date' in p.meta) |
|
latest = sorted(articles, reverse=True, |
|
key=lambda p: p.meta['published']) |
|
return render_template('base.html', articles=latest) |
|
|
|
|
|
@app.route('/<path:path>/') |
|
def page(path): |
|
page = pages.get_or_404(path) |
|
return render_template('single.html', page=page) |
|
|
|
|
|
@app.route('/webhook', methods=['POST']) |
|
def webhook(): |
|
# Verify the secret (if you used one in Gitea) |
|
secret = "your_secret_here" # Replace with your actual secret |
|
if secret: |
|
if 'X-Gitea-Signature' not in request.headers: |
|
return "Unauthorized", 401 |
|
received_signature = request.headers['X-Gitea-Signature'] |
|
expected_signature = f"sha256={compute_signature(request.data, secret)}" |
|
if received_signature != expected_signature: |
|
return "Unauthorized", 401 |
|
|
|
# Perform Git pull to update the website's code |
|
repo_path = "/path/to/your/website/repo" # Replace with the actual path |
|
subprocess.run(['git', 'pull'], cwd=repo_path) |
|
|
|
# Restart the Flask app |
|
#restart_flask_app() |
|
|
|
return "Webhook received and processed successfully", 200 |
|
|
|
def compute_signature(data, secret): |
|
import hmac |
|
import hashlib |
|
return hmac.new(secret.encode('utf-8'), data, hashlib.sha256).hexdigest() |
|
|
|
def restart_flask_app(): |
|
# Customize this function to restart your Flask app (e.g., using gunicorn, systemd, etc.) |
|
# Example: subprocess.run(['systemctl', 'restart', 'your-flask-app.service']) |
|
pass |
|
|
|
if __name__ == '__main__': |
|
app.run(host='0.0.0.0', port=5000) # Adjust the host and port as needed |
|
|
|
|