You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.7 KiB
Python

2 years ago
from flask import Flask, render_template, send_from_directory, redirect, request
2 years ago
from markdown import markdown
2 years ago
from prefix import PrefixMiddleware
2 years ago
from dotenv import load_dotenv
2 years ago
import os, subprocess
2 years ago
load_dotenv()
prefix = os.environ.get('URL_PREFIX', '')
port = os.environ.get('PORT', '3000')
debug = os.environ.get('DEBUG', 'False')
2 years ago
update_script = os.environ.get('UPDATE', 'update.sh' )
2 years ago
2 years ago
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
2 years ago
app = Flask(__name__)
2 years ago
app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix=prefix)
2 years ago
2 years ago
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'),
'favicon.ico', mimetype='image/vnd.microsoft.icon')
2 years ago
@app.route('/')
2 years ago
def home():
listos = list_files('txt', True)
return render_template('home.html', listos = listos)
@app.route('/<slug>')
def list(slug):
with open(f'txt/{slug}.md',"r") as f:
2 years ago
text = f.read()
list = markdown(text)
2 years ago
return render_template('list.html', slug=slug, list = list)
2 years ago
@app.route('/img/<file>')
2 years ago
def send_img(file):
return send_from_directory(app.root_path + '/img/', file, conditional=True)
2 years ago
2 years ago
@app.route('/api/update', methods=['GET', 'POST'])
def update():
if request.method == 'POST':
subprocess.run(update_script, shell=True)
return 'Updated!'
2 years ago
app.run(port=port, debug=debug)