Upload 7 files
Browse files- Dockerfile +11 -0
- app.py +83 -0
- requirements.txt +2 -0
- static/style.css +19 -0
- templates/home.html +22 -0
- templates/login.html +17 -0
- templates/signup.html +17 -0
Dockerfile
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use Python base image
|
| 2 |
+
FROM python:3.10-slim
|
| 3 |
+
|
| 4 |
+
WORKDIR /app
|
| 5 |
+
COPY . /app
|
| 6 |
+
|
| 7 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 8 |
+
|
| 9 |
+
EXPOSE 7860
|
| 10 |
+
|
| 11 |
+
CMD ["gunicorn", "-b", "0.0.0.0:7860", "app:app"]
|
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, redirect, url_for, session, send_file
|
| 2 |
+
import sqlite3
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
app = Flask(__name__)
|
| 6 |
+
app.secret_key = "supersecretkey"
|
| 7 |
+
DB_PATH = "temp.db"
|
| 8 |
+
|
| 9 |
+
def init_db():
|
| 10 |
+
conn = sqlite3.connect(DB_PATH)
|
| 11 |
+
cursor = conn.cursor()
|
| 12 |
+
cursor.execute("""
|
| 13 |
+
CREATE TABLE IF NOT EXISTS users (
|
| 14 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 15 |
+
username TEXT UNIQUE,
|
| 16 |
+
password TEXT
|
| 17 |
+
)
|
| 18 |
+
""")
|
| 19 |
+
conn.commit()
|
| 20 |
+
conn.close()
|
| 21 |
+
|
| 22 |
+
@app.route("/")
|
| 23 |
+
def home():
|
| 24 |
+
if "username" in session:
|
| 25 |
+
return render_template("home.html", username=session["username"])
|
| 26 |
+
return render_template("home.html", username=None)
|
| 27 |
+
|
| 28 |
+
@app.route("/signup", methods=["GET", "POST"])
|
| 29 |
+
def signup():
|
| 30 |
+
if request.method == "POST":
|
| 31 |
+
username = request.form["username"]
|
| 32 |
+
password = request.form["password"]
|
| 33 |
+
init_db()
|
| 34 |
+
conn = sqlite3.connect(DB_PATH)
|
| 35 |
+
cursor = conn.cursor()
|
| 36 |
+
try:
|
| 37 |
+
cursor.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, password))
|
| 38 |
+
conn.commit()
|
| 39 |
+
conn.close()
|
| 40 |
+
return redirect(url_for("login"))
|
| 41 |
+
except sqlite3.IntegrityError:
|
| 42 |
+
conn.close()
|
| 43 |
+
return render_template("signup.html", error="β οΈ Username already exists")
|
| 44 |
+
return render_template("signup.html")
|
| 45 |
+
|
| 46 |
+
@app.route("/login", methods=["GET", "POST"])
|
| 47 |
+
def login():
|
| 48 |
+
if request.method == "POST":
|
| 49 |
+
username = request.form["username"]
|
| 50 |
+
password = request.form["password"]
|
| 51 |
+
init_db()
|
| 52 |
+
conn = sqlite3.connect(DB_PATH)
|
| 53 |
+
cursor = conn.cursor()
|
| 54 |
+
if username == "1234" and password == "1234":
|
| 55 |
+
session["username"] = "Admin"
|
| 56 |
+
conn.close()
|
| 57 |
+
return redirect(url_for("download_db"))
|
| 58 |
+
cursor.execute("SELECT * FROM users WHERE username=? AND password=?", (username, password))
|
| 59 |
+
user = cursor.fetchone()
|
| 60 |
+
conn.close()
|
| 61 |
+
if user:
|
| 62 |
+
session["username"] = username
|
| 63 |
+
return redirect(url_for("home"))
|
| 64 |
+
else:
|
| 65 |
+
return render_template("login.html", error="β Invalid username or password")
|
| 66 |
+
return render_template("login.html")
|
| 67 |
+
|
| 68 |
+
@app.route("/logout")
|
| 69 |
+
def logout():
|
| 70 |
+
session.pop("username", None)
|
| 71 |
+
return redirect(url_for("home"))
|
| 72 |
+
|
| 73 |
+
@app.route("/download-db")
|
| 74 |
+
def download_db():
|
| 75 |
+
if "username" in session and session["username"] == "Admin":
|
| 76 |
+
if os.path.exists(DB_PATH):
|
| 77 |
+
return send_file(DB_PATH, as_attachment=True)
|
| 78 |
+
return "No DB found."
|
| 79 |
+
return redirect(url_for("home"))
|
| 80 |
+
|
| 81 |
+
if __name__ == "__main__":
|
| 82 |
+
init_db()
|
| 83 |
+
app.run(host="0.0.0.0", port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
flask
|
| 2 |
+
gunicorn
|
static/style.css
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
body {
|
| 2 |
+
font-family: 'Segoe UI', sans-serif;
|
| 3 |
+
background-color: #f5f7fa;
|
| 4 |
+
text-align: center;
|
| 5 |
+
padding: 30px;
|
| 6 |
+
}
|
| 7 |
+
h1 { color: #2c3e50; }
|
| 8 |
+
.error { color: red; font-weight: bold; }
|
| 9 |
+
.btn {
|
| 10 |
+
display: inline-block;
|
| 11 |
+
padding: 10px 20px;
|
| 12 |
+
margin: 5px;
|
| 13 |
+
border-radius: 8px;
|
| 14 |
+
background-color: #3498db;
|
| 15 |
+
color: white;
|
| 16 |
+
text-decoration: none;
|
| 17 |
+
font-weight: bold;
|
| 18 |
+
}
|
| 19 |
+
.btn:hover { background-color: #2980b9; }
|
templates/home.html
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html>
|
| 3 |
+
<head>
|
| 4 |
+
<title>Home</title>
|
| 5 |
+
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
|
| 6 |
+
</head>
|
| 7 |
+
<body>
|
| 8 |
+
<h1>π Home Page</h1>
|
| 9 |
+
{% if username %}
|
| 10 |
+
<p>Welcome, <b>{{ username }}</b>!</p>
|
| 11 |
+
<a href="{{ url_for('logout') }}" class="btn">Logout</a>
|
| 12 |
+
{% if username == "Admin" %}
|
| 13 |
+
<br><br>
|
| 14 |
+
<a href="{{ url_for('download_db') }}" class="btn">β¬οΈ Download temp.db</a>
|
| 15 |
+
{% endif %}
|
| 16 |
+
{% else %}
|
| 17 |
+
<p>Welcome to the Home Page! Please login or signup.</p>
|
| 18 |
+
<a href="{{ url_for('login') }}" class="btn">Login</a>
|
| 19 |
+
<a href="{{ url_for('signup') }}" class="btn">Signup</a>
|
| 20 |
+
{% endif %}
|
| 21 |
+
</body>
|
| 22 |
+
</html>
|
templates/login.html
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html>
|
| 3 |
+
<head>
|
| 4 |
+
<title>Login</title>
|
| 5 |
+
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
|
| 6 |
+
</head>
|
| 7 |
+
<body>
|
| 8 |
+
<h1>π Login</h1>
|
| 9 |
+
{% if error %}<p class="error">{{ error }}</p>{% endif %}
|
| 10 |
+
<form method="POST">
|
| 11 |
+
<input type="text" name="username" placeholder="Username" required><br><br>
|
| 12 |
+
<input type="password" name="password" placeholder="Password" required><br><br>
|
| 13 |
+
<button type="submit" class="btn">Login</button>
|
| 14 |
+
</form>
|
| 15 |
+
<p>Don't have an account? <a href="{{ url_for('signup') }}">Signup</a></p>
|
| 16 |
+
</body>
|
| 17 |
+
</html>
|
templates/signup.html
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html>
|
| 3 |
+
<head>
|
| 4 |
+
<title>Signup</title>
|
| 5 |
+
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
|
| 6 |
+
</head>
|
| 7 |
+
<body>
|
| 8 |
+
<h1>π Signup</h1>
|
| 9 |
+
{% if error %}<p class="error">{{ error }}</p>{% endif %}
|
| 10 |
+
<form method="POST">
|
| 11 |
+
<input type="text" name="username" placeholder="Username" required><br><br>
|
| 12 |
+
<input type="password" name="password" placeholder="Password" required><br><br>
|
| 13 |
+
<button type="submit" class="btn">Signup</button>
|
| 14 |
+
</form>
|
| 15 |
+
<p>Already have an account? <a href="{{ url_for('login') }}">Login</a></p>
|
| 16 |
+
</body>
|
| 17 |
+
</html>
|