2023-12-31 12:59:13 +00:00
|
|
|
from flask import Flask, render_template, send_from_directory
|
|
|
|
from jinja2 import TemplateNotFound
|
|
|
|
|
|
|
|
import json
|
|
|
|
import pathlib
|
2024-05-27 15:37:30 +00:00
|
|
|
import os
|
2023-12-31 12:59:13 +00:00
|
|
|
|
2024-04-17 08:45:28 +00:00
|
|
|
from argparse import ArgumentParser
|
|
|
|
|
2023-12-31 12:59:13 +00:00
|
|
|
app = Flask(__name__)
|
|
|
|
|
2024-04-17 08:45:28 +00:00
|
|
|
|
|
|
|
@app.route("/assets/<path:path>")
|
2023-12-31 12:59:13 +00:00
|
|
|
def send_assets(path):
|
2024-04-17 08:45:28 +00:00
|
|
|
return send_from_directory("assets", path)
|
|
|
|
|
2023-12-31 12:59:13 +00:00
|
|
|
|
2024-04-17 08:45:28 +00:00
|
|
|
@app.route("/", defaults={"path": "index"})
|
|
|
|
@app.route("/<path:path>.html")
|
2023-12-31 12:59:13 +00:00
|
|
|
def catch_all(path):
|
|
|
|
try:
|
2024-04-17 08:45:28 +00:00
|
|
|
services = json.loads(
|
|
|
|
(pathlib.Path(__file__).parent / "services.json").read_text()
|
|
|
|
)
|
2024-05-27 15:37:30 +00:00
|
|
|
|
|
|
|
warning = None
|
|
|
|
|
|
|
|
if app.development_mode:
|
|
|
|
warning = render_template("prod-warning.html")
|
|
|
|
|
|
|
|
return render_template(
|
|
|
|
f"{path}.html", services=services, warning=warning
|
|
|
|
)
|
2023-12-31 12:59:13 +00:00
|
|
|
except TemplateNotFound:
|
|
|
|
return "404 Not Found", 404
|
|
|
|
|
2024-04-17 08:45:28 +00:00
|
|
|
|
2024-05-27 15:37:30 +00:00
|
|
|
app.development_mode = False
|
|
|
|
|
|
|
|
if os.environ.get("PRIVATECOFFEE_DEV"):
|
|
|
|
app.development_mode = True
|
|
|
|
|
|
|
|
|
2024-04-17 08:45:28 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = ArgumentParser(description="Run the private.coffee web server.")
|
|
|
|
parser.add_argument("--port", type=int, default=9810)
|
|
|
|
parser.add_argument("--debug", action="store_true")
|
2024-05-27 15:37:30 +00:00
|
|
|
parser.add_argument("--dev", action="store_true")
|
2024-04-17 08:45:28 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2024-05-27 15:37:30 +00:00
|
|
|
app.development_mode = args.dev or app.development_mode
|
|
|
|
|
2024-04-17 08:45:28 +00:00
|
|
|
app.run(port=args.port, debug=args.debug)
|