Initial commit
This commit is contained in:
75
app.py
Normal file
75
app.py
Normal file
@@ -0,0 +1,75 @@
|
||||
# /// script
|
||||
# dependencies = ["flask", "flask_cors", "waitress", "markdown"]
|
||||
# ///
|
||||
import os
|
||||
import json
|
||||
import markdown
|
||||
from flask import Flask, render_template, send_from_directory
|
||||
from flask_cors import CORS
|
||||
|
||||
app = Flask(__name__, template_folder='.', static_folder='static')
|
||||
|
||||
CORS(app)
|
||||
|
||||
@app.route("/favicon.ico")
|
||||
def favicon():
|
||||
return send_from_directory(
|
||||
os.path.join(app.root_path, 'static/assets/favicon.ico'),
|
||||
'favicon.ico', mimetype='image/x-icon'
|
||||
)
|
||||
|
||||
|
||||
@app.errorhandler(404)
|
||||
def not_found(e):
|
||||
return render_template("./templates/not_found.html")
|
||||
|
||||
@app.route("/home")
|
||||
def home():
|
||||
return render_template("./templates/home.html")
|
||||
|
||||
@app.route("/")
|
||||
def index():
|
||||
return render_template("./templates/home.html")
|
||||
|
||||
@app.route("/posts")
|
||||
def posts():
|
||||
posts = []
|
||||
for folder in os.listdir("./static/posts"):
|
||||
with open(os.path.join("./static/posts", folder, "metadata.json"), "r") as f:
|
||||
metadata = json.load(f)
|
||||
|
||||
post = {
|
||||
"post_id": folder,
|
||||
"metadata": metadata,
|
||||
}
|
||||
|
||||
posts.append(post)
|
||||
|
||||
return render_template("./templates/post_list.html", posts=posts)
|
||||
|
||||
|
||||
@app.route("/posts/<post_id>")
|
||||
def get_post(post_id):
|
||||
for folder in os.listdir("./static/posts"):
|
||||
if folder == post_id:
|
||||
with open(os.path.join("./static/posts", folder, "metadata.json"), "r") as f:
|
||||
metadata = json.load(f)
|
||||
with open(os.path.join("./static/posts", folder, "post.md"), "r") as f:
|
||||
post_md = f.read()
|
||||
post_html = markdown.markdown(post_md, extensions=["tables"])
|
||||
|
||||
post = {
|
||||
"post_id": post_id,
|
||||
"metadata": metadata,
|
||||
"post_md": post_html,
|
||||
}
|
||||
|
||||
return render_template("./templates/post.html", post=post)
|
||||
|
||||
return render_template("./templates/not_found.html")
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from waitress import serve
|
||||
serve(app, host="0.0.0.0", port=8080)
|
Reference in New Issue
Block a user