Update app.py
This commit is contained in:
parent
92c3213384
commit
22812b628d
42
app.py
42
app.py
@ -1,14 +1,42 @@
|
||||
#!/usr/bin/env python3
|
||||
import os
|
||||
from flask import Flask, render_template, request, redirect, flash
|
||||
from dotenv import load_dotenv # Optional: can be removed now
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from models import db, Config, Plan
|
||||
# import ORM models & Base from your bot module
|
||||
from discord_lnbits_bot import Base, Config, Plan
|
||||
|
||||
# Flask setup
|
||||
app = Flask(__name__)
|
||||
app.secret_key = os.environ["FLASK_SECRET"]
|
||||
|
||||
# Flask config from env vars
|
||||
app.secret_key = os.environ.get("FLASK_SECRET", "dev-only-key")
|
||||
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get("DATABASE_URL")
|
||||
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
|
||||
# Database setup
|
||||
DATABASE_URL = os.environ["DATABASE_URL"]
|
||||
engine = create_engine(DATABASE_URL, future=True)
|
||||
SessionLocal = sessionmaker(bind=engine, expire_on_commit=False)
|
||||
Base.metadata.create_all(engine)
|
||||
|
||||
db.init_app(app)
|
||||
@app.route("/", methods=["GET", "POST"])
|
||||
def settings():
|
||||
db = SessionLocal()
|
||||
if request.method == "POST":
|
||||
# save core config keys
|
||||
for key in ("discord_token", "guild_id", "lnbits_url", "lnbits_api_key"):
|
||||
val = request.form.get(key, "").strip()
|
||||
cfg = db.get(Config, key)
|
||||
if cfg:
|
||||
cfg.value = val
|
||||
else:
|
||||
db.add(Config(key=key, value=val))
|
||||
db.commit()
|
||||
flash("Configuration saved.", "success")
|
||||
return redirect("/")
|
||||
|
||||
configs = db.query(Config).all()
|
||||
plans = db.query(Plan).all()
|
||||
db.close()
|
||||
return render_template("settings.html", configs=configs, plans=plans)
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host="0.0.0.0", port=3000)
|
||||
|
Loading…
x
Reference in New Issue
Block a user