From a6157c1f1eac6b720c5ef3b6c06829154195921a Mon Sep 17 00:00:00 2001 From: Kumi Date: Wed, 17 Apr 2024 10:45:28 +0200 Subject: [PATCH] feat: add CLI for server config Introduced argument parsing to provide command-line options for configuring the server port and enabling debug mode, enhancing flexibility for local development or deployment needs. The changes include adjusting string quotes for consistency and minor formatting improvements. This addition allows users to start the web server with custom configurations without modifying the source code directly, making it more convenient to run in different environments or debug as needed. --- main.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/main.py b/main.py index f553ac2..f42bc36 100644 --- a/main.py +++ b/main.py @@ -4,20 +4,32 @@ from jinja2 import TemplateNotFound import json import pathlib +from argparse import ArgumentParser + app = Flask(__name__) -@app.route('/assets/') -def send_assets(path): - return send_from_directory('assets', path) -@app.route('/', defaults={'path': 'index'}) -@app.route('/.html') +@app.route("/assets/") +def send_assets(path): + return send_from_directory("assets", path) + + +@app.route("/", defaults={"path": "index"}) +@app.route("/.html") def catch_all(path): try: - services = json.loads((pathlib.Path(__file__).parent / "services.json").read_text()) - return render_template(f'{path}.html', services=services) + services = json.loads( + (pathlib.Path(__file__).parent / "services.json").read_text() + ) + return render_template(f"{path}.html", services=services) except TemplateNotFound: return "404 Not Found", 404 -if __name__ == '__main__': - app.run(port=9810) + +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") + args = parser.parse_args() + + app.run(port=args.port, debug=args.debug)