FuckingChat/app.py

84 lines
2.0 KiB
Python

"""
app.py — FuckingChat entry point.
Usage:
python app.py # run server
python app.py --configure # run TUI configurator
python app.py --config path.json # specify config file
"""
import os
import sys
from bottle import Bottle, static_file, run
from config.config import load, save
from config.schema import AppConfig
from models.db import DBfrontend
from controllers.chat import init as chat_init, register_routes as chat_routes
from controllers.tenor import init as tenor_init, register_routes as tenor_routes
# Override config path from CLI
if "--config" in sys.argv:
idx = sys.argv.index("--config")
if idx + 1 < len(sys.argv):
os.environ["CHAT_CONFIG"] = sys.argv[idx + 1]
cfg: AppConfig = load()
def _make_db() -> DBfrontend:
return DBfrontend(
cfg.db,
secret=cfg.security.secret.encode(),
salt=cfg.security.salt.encode(),
)
def create_app() -> Bottle:
app = Bottle()
# Static frontend
static_dir = os.path.dirname(os.path.abspath(__file__))
@app.route("/")
def serve_index():
return static_file("index.html", root=static_dir)
@app.route("/static/<filename:path>")
def serve_static(filename):
return static_file(filename, root=os.path.join(static_dir, "static"))
# Init controllers
db = _make_db()
chat_init(db, cfg.security)
tenor_init(cfg.tenor)
# Register route blueprints
chat_routes(app)
tenor_routes(app)
return app
def run_server():
app = create_app()
host = cfg.server.host
port = cfg.server.port
debug = cfg.server.debug
if debug:
print("⚠️ DEBUG MODE — do not use in production!")
print(f"🚀 Chat running on http://{host}:{port}")
run(app=app, host=host, port=port, debug=debug, reloader=debug)
def run_configurator():
from views.config_tui import main
main()
if __name__ == "__main__":
if "--configure" in sys.argv or "-c" in sys.argv:
run_configurator()
else:
run_server()