from flask import Flask, render_template, send_from_directory, redirect, request from markdown import markdown from prefix import PrefixMiddleware from dotenv import load_dotenv import os, subprocess load_dotenv() prefix = os.environ.get('URL_PREFIX', '') port = os.environ.get('PORT', '3000') debug = os.environ.get('DEBUG', 'False') update_script = os.environ.get('UPDATE', 'update.sh' ) def list_files(folder, remove_ext=False): ''' Read all the functions in a folder ''' names = [] for entry in os.scandir(folder): # add to the list only proper files if entry.is_file(follow_symlinks=False): # remove the extension from the filename n = entry.name if remove_ext: n = os.path.splitext(entry.name)[0] names.append(n) return names app = Flask(__name__) app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix=prefix) @app.route('/favicon.ico') def favicon(): return send_from_directory(os.path.join(app.root_path, 'static'), 'favicon.ico', mimetype='image/vnd.microsoft.icon') @app.route('/') def home(): listos = list_files('txt', True) return render_template('home.html', listos = listos) @app.route('/') def list(slug): with open(f'txt/{slug}.md',"r") as f: text = f.read() list = markdown(text) return render_template('list.html', slug=slug, list = list) @app.route('/img/') def send_img(file): return send_from_directory(app.root_path + '/img/', file, conditional=True) @app.route('/api/update', methods=['GET', 'POST']) def update(): if request.method == 'POST': subprocess.run(update_script, shell=True) return 'Updated!' app.run(port=port, debug=debug)