From a5d02f3efd0db2b662738d1d0d7930875c96343a Mon Sep 17 00:00:00 2001 From: MathiasDPX Date: Thu, 17 Apr 2025 23:27:25 +0200 Subject: [PATCH] add admin panel --- .env.example | 3 +++ main.py | 12 ++++++++++++ requirements.txt | 5 +++-- templates/admin.html | 30 ++++++++++++++++++++++++++++++ templates/index.html | 2 +- 5 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 templates/admin.html diff --git a/.env.example b/.env.example index 62c035d..023cad9 100644 --- a/.env.example +++ b/.env.example @@ -4,6 +4,9 @@ db_password="" db_host=postgres.hackclub.app db_port=5432 +BASIC_AUTH_USERNAME="admin" +BASIC_AUTH_PASSWORD="veryverylongpasswordthatshouldbesafe" + SYNC_SERVER_URL="sync.example.com" HOST="0.0.0.0" PORT=80 \ No newline at end of file diff --git a/main.py b/main.py index 77592c4..933e373 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,5 @@ from flask import Flask, request, make_response, render_template +from flask_basicauth import BasicAuth from dotenv import load_dotenv from flask_sock import Sock from glob import glob @@ -9,6 +10,10 @@ import os load_dotenv() app = Flask(__name__) sock = Sock(app) +basic_auth = BasicAuth(app) + +app.config['BASIC_AUTH_USERNAME'] = os.getenv("AUTH_USERNAME") +app.config['BASIC_AUTH_PASSWORD'] = os.getenv("AUTH_PASSWORD") def make_resp(data=''): response = make_response(data) @@ -136,6 +141,13 @@ def websocket(ws): if operation == "usernames": # TODO ws.send(json.dumps({"1": "Mathias"})) +@app.route("/admin") +@basic_auth.required +def admin_panel(): + users = database.db.execute_query("SELECT name, email FROM users;") + vaults = database.db.execute_query("SELECT name, (SELECT name FROM users WHERE uid=owner) FROM vaults;") + return render_template("admin.html", users=users, vaults=vaults) + @app.route("/") def index_page(): return render_template("index.html") diff --git a/requirements.txt b/requirements.txt index b734722..6446844 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ psycopg2 python-dotenv -flask -flask-sock \ No newline at end of file +Flask +flask-sock +Flask-BasicAuth \ No newline at end of file diff --git a/templates/admin.html b/templates/admin.html new file mode 100644 index 0000000..e40a590 --- /dev/null +++ b/templates/admin.html @@ -0,0 +1,30 @@ + + + + + + Obsidian Sync server + + + + + + + +

Obsidian Sync / Admin Panel

+ Users: + + Vaults: + + + \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index f35a7b6..ca41d13 100644 --- a/templates/index.html +++ b/templates/index.html @@ -13,7 +13,7 @@ -

Obsidian Sync server

+

Obsidian Sync

If you see this page, your Obsidian Sync server server is successfully installed and working.

Available at mathias/obsidian_freesync