app.py
Python script, ASCII text executable
1import datetime 2 3import flask 4from flask_sqlalchemy import SQLAlchemy 5from flask_bcrypt import Bcrypt 6from flask_migrate import Migrate 7 8from sqlalchemy.orm import declarative_base 9 10import httpx 11 12app = flask.Flask(__name__) 13app.config["SQLALCHEMY_DATABASE_URI"] = \ 14"postgresql://echo:1234@localhost:5432/echo" 15db = SQLAlchemy(app) 16bcrypt = Bcrypt(app) 17migrate = Migrate(app, db) 18app.config["SESSION_TYPE"] = "filesystem" 19app.config["SECRET_KEY"] = "super secret" 20 21with app.app_context(): 22class User(db.Model): 23username = db.Column(db.String(64), unique=True, nullable=False, primary_key=True) 24password = db.Column(db.String(72), nullable=False) 25admin = db.Column(db.Boolean, nullable=False, default=False) 26 27applications = db.relationship("Application", back_populates="owner") 28 29def __init__(self, username, password, admin=False): 30self.username = username 31self.password = bcrypt.generate_password_hash(password).decode("utf-8") 32self.admin = admin 33 34class Application(db.Model): 35id = db.Column(db.Integer, primary_key=True, autoincrement=True, default=0) 36name = db.Column(db.String(64), unique=True, nullable=False) 37owner_name = db.Column(db.String(64), db.ForeignKey("user.username"), nullable=False) 38 39owner = db.relationship("User", back_populates="applications") 40 41endpoints = db.relationship("Endpoint", back_populates="application") 42 43def __init__(self, name, owner): 44self.name = name 45self.owner_name = owner.username 46 47class Endpoint(db.Model): 48id = db.Column(db.Integer, unique=True, nullable=False, primary_key=True, autoincrement=True) 49application_name = db.Column(db.String(64), db.ForeignKey("application.name"), nullable=False) 50address = db.Column(db.String(2048), nullable=False) 51name = db.Column(db.String(64), nullable=False) 52comment = db.Column(db.String(2048), nullable=True) 53 54application = db.relationship("Application", back_populates="endpoints") 55 56def __init__(self, application_name): 57self.application_name = application_name 58 59Base = declarative_base() 60 61class Status(Base): 62__table_args = ( 63{ 64"timescaledb_hypertable": { 65"time_column_name": "time", 66}, 67} 68) 69__tablename__ = "status" 70id = db.Column(db.Integer, unique=True, nullable=False, autoincrement=True) 71endpoint_id = db.Column(db.Integer, nullable=False) 72time = db.Column(db.DateTime, index=True, default=datetime.datetime.utcnow, primary_key=True) 73 74status = db.Column(db.SmallInteger, nullable=False) 75 76endpoint = db.relationship("Endpoint", back_populates="statuses") 77 78def __init__(self, endpoint, status): 79self.endpoint_id = endpoint.id 80self.status = status 81 82 83def ping(endpoint): 84url = endpoint.address 85response = httpx.get(url) 86return response.status_code 87 88 89@app.context_processor 90def default(): 91return { 92"session": flask.session, 93} 94 95 96@app.route("/") 97def dashboard(): 98return flask.render_template("dashboard.html", apps=Application.query.all()) 99 100 101@app.route("/login", methods=["GET"]) 102def login(): 103return flask.render_template("login.html") 104 105 106@app.route("/signup", methods=["GET"]) 107def signup(): 108return flask.render_template("signup.html") 109 110 111@app.route("/new-app", methods=["GET"]) 112def new_app(): 113if not flask.session.get("username"): 114return flask.redirect("/login", code=303) 115return flask.render_template("new-app.html") 116 117 118@app.route("/new-app", methods=["POST"]) 119def new_app_post(): 120if not flask.session.get("username"): 121return flask.redirect("/login", code=303) 122if Application.query.filter_by(name=flask.request.form["name"]).first(): 123flask.flash("Application already exists") 124return flask.redirect("/new-app", code=303) 125 126new_app_ = Application( 127flask.request.form["name"], 128db.session.get(User, flask.session["username"]), 129) 130db.session.add(new_app_) 131db.session.commit() 132return flask.redirect("/", code=303) 133 134 135@app.route("/login", methods=["POST"]) 136def login_post(): 137user = db.session.get(User, flask.request.form["username"]) 138if not user: 139flask.flash("Username doesn't exist") 140return flask.redirect("/signup", code=303) 141if not bcrypt.check_password_hash(user.password, flask.request.form["password"]): 142flask.flash("Wrong password") 143return flask.redirect("/signup", code=303) 144 145flask.session["username"] = user.username 146return flask.redirect("/", code=303) 147 148 149@app.route("/logout") 150def logout(): 151flask.session.pop("username", None) 152return flask.redirect("/", code=303) 153 154 155@app.route("/signup", methods=["POST"]) 156def signup_post(): 157if flask.request.form["password"] != flask.request.form["password2"]: 158flask.flash("Passwords do not match") 159return flask.redirect("/signup", code=303) 160if db.session.get(User, flask.request.form["username"]): 161flask.flash("Username already exists") 162return flask.redirect("/signup", code=303) 163if len(flask.request.form["password"]) < 8: 164flask.flash("Password must be at least 8 characters") 165return flask.redirect("/signup", code=303) 166if len(flask.request.form["username"]) < 4: 167flask.flash("Username must be at least 4 characters") 168return flask.redirect("/signup", code=303) 169 170new_user = User( 171flask.request.form["username"], 172flask.request.form["password"], 173) 174db.session.add(new_user) 175db.session.commit() 176flask.session["username"] = new_user.username 177return flask.redirect("/", code=303) 178 179 180@app.route("/timeline/<endpoint_id>") 181def info(endpoint_id): 182return flask.render_template("timeline.html", endpoint=endpoint_id) 183 184 185@app.route("/app/<int:app_id>") 186def app_info(app_id): 187app_ = db.session.get(Application, app_id) 188return flask.render_template("app.html", app=app_) 189 190 191@app.route("/app/<int:app_id>/edit") 192def app_editor(app_id): 193if flask.session.get("username") != db.session.get(Application, app_id).owner_name: 194flask.abort(403) 195app_ = db.session.get(Application, app_id) 196return flask.render_template("app-editor.html", app=app_) 197