0vergeared commited on
Commit
bb1d948
·
verified ·
1 Parent(s): ab04db2

Update server.py

Browse files
Files changed (1) hide show
  1. server.py +14 -26
server.py CHANGED
@@ -1,7 +1,6 @@
1
  import os
2
  import sqlite3
3
  import secrets
4
- import pathlib
5
  from datetime import datetime, timedelta
6
  from flask import Flask, request, redirect, url_for, render_template, session, flash
7
  from flask_limiter import Limiter
@@ -18,23 +17,14 @@ HF_REPO_ID = "0vergeared/otp-logs"
18
 
19
  # Paths
20
  DATABASE = "otp.db"
21
- TEMPLATES = "templates"
22
- STATIC = "static"
23
 
24
- # Setup
25
- app = Flask(__name__, template_folder=TEMPLATES, static_folder=STATIC)
26
  app.secret_key = FLASK_SECRET
27
-
28
- # Rate Limiting
29
  limiter = Limiter(get_remote_address, app=app)
30
 
31
- # Make sure Hugging Face Hub doesn't cache
32
- os.environ["HF_DATASETS_CACHE"] = "/tmp/hf_cache"
33
-
34
- # Make sure templates/static folders exist
35
- os.makedirs(TEMPLATES, exist_ok=True)
36
- os.makedirs(STATIC, exist_ok=True)
37
-
38
  def init_db():
39
  with sqlite3.connect(DATABASE) as conn:
40
  c = conn.cursor()
@@ -53,22 +43,21 @@ def generate_otp():
53
  return ''.join(secrets.choice("0123456789") for _ in range(6))
54
 
55
  def upload_to_hf(otp_row):
56
- csv_path = "/tmp/otp_all.csv"
57
- file_exists = os.path.exists(csv_path)
58
-
59
  df_new = pd.DataFrame([otp_row], columns=["otp", "generated_at", "expires_at", "used_at", "ip", "status"])
 
60
  if file_exists:
61
- df_old = pd.read_csv(csv_path)
62
  df = pd.concat([df_old, df_new], ignore_index=True)
63
  else:
64
  df = df_new
65
 
66
- df.to_csv(csv_path, index=False)
67
 
68
  try:
69
  api = HfApi()
70
  api.upload_file(
71
- path_or_fileobj=csv_path,
72
  path_in_repo="otp_all.csv",
73
  repo_id=HF_REPO_ID,
74
  token=HF_TOKEN,
@@ -76,8 +65,9 @@ def upload_to_hf(otp_row):
76
  )
77
  print("✅ OTP log uploaded to HF.")
78
  except Exception as e:
79
- print("⚠️ Failed to upload to HF:", e)
80
 
 
81
  @app.route("/", methods=["GET", "POST"])
82
  def index():
83
  if request.method == "POST":
@@ -101,14 +91,13 @@ def index():
101
  c.execute("UPDATE otps SET used_at = ?, status = ? WHERE otp = ?", (used_time, "used", otp_input))
102
  conn.commit()
103
 
104
- # Log to Hugging Face
105
  otp_row = [otp_input, gen_at, exp_at, used_time, request.remote_addr, "used"]
106
  upload_to_hf(otp_row)
107
 
108
- flash("OTP accepted. Installation can continue!", "success")
109
  return redirect(url_for("index"))
110
  else:
111
- flash("Invalid OTP!", "danger")
112
 
113
  return render_template("index.html")
114
 
@@ -121,7 +110,7 @@ def admin():
121
  session["admin"] = True
122
  return redirect(url_for("dashboard"))
123
  else:
124
- flash("Login failed", "danger")
125
  return render_template("admin_login.html")
126
 
127
  @app.route("/dashboard", methods=["GET", "POST"])
@@ -141,7 +130,6 @@ def dashboard():
141
  (otp, now.isoformat(), expires.isoformat(), ip, "generated"))
142
  conn.commit()
143
 
144
- # Log to HF
145
  otp_row = [otp, now.isoformat(), expires.isoformat(), None, ip, "generated"]
146
  upload_to_hf(otp_row)
147
 
 
1
  import os
2
  import sqlite3
3
  import secrets
 
4
  from datetime import datetime, timedelta
5
  from flask import Flask, request, redirect, url_for, render_template, session, flash
6
  from flask_limiter import Limiter
 
17
 
18
  # Paths
19
  DATABASE = "otp.db"
20
+ EXPORT_CSV = "/tmp/otp_all.csv"
 
21
 
22
+ # Flask App
23
+ app = Flask(__name__, template_folder="templates", static_folder="static")
24
  app.secret_key = FLASK_SECRET
 
 
25
  limiter = Limiter(get_remote_address, app=app)
26
 
27
+ # OTP utils
 
 
 
 
 
 
28
  def init_db():
29
  with sqlite3.connect(DATABASE) as conn:
30
  c = conn.cursor()
 
43
  return ''.join(secrets.choice("0123456789") for _ in range(6))
44
 
45
  def upload_to_hf(otp_row):
46
+ file_exists = os.path.exists(EXPORT_CSV)
 
 
47
  df_new = pd.DataFrame([otp_row], columns=["otp", "generated_at", "expires_at", "used_at", "ip", "status"])
48
+
49
  if file_exists:
50
+ df_old = pd.read_csv(EXPORT_CSV)
51
  df = pd.concat([df_old, df_new], ignore_index=True)
52
  else:
53
  df = df_new
54
 
55
+ df.to_csv(EXPORT_CSV, index=False)
56
 
57
  try:
58
  api = HfApi()
59
  api.upload_file(
60
+ path_or_fileobj=EXPORT_CSV,
61
  path_in_repo="otp_all.csv",
62
  repo_id=HF_REPO_ID,
63
  token=HF_TOKEN,
 
65
  )
66
  print("✅ OTP log uploaded to HF.")
67
  except Exception as e:
68
+ print("⚠️ Hugging Face upload failed:", e)
69
 
70
+ # Routes
71
  @app.route("/", methods=["GET", "POST"])
72
  def index():
73
  if request.method == "POST":
 
91
  c.execute("UPDATE otps SET used_at = ?, status = ? WHERE otp = ?", (used_time, "used", otp_input))
92
  conn.commit()
93
 
 
94
  otp_row = [otp_input, gen_at, exp_at, used_time, request.remote_addr, "used"]
95
  upload_to_hf(otp_row)
96
 
97
+ flash("OTP accepted. Installation can proceed!", "success")
98
  return redirect(url_for("index"))
99
  else:
100
+ flash("Invalid OTP!", "danger")
101
 
102
  return render_template("index.html")
103
 
 
110
  session["admin"] = True
111
  return redirect(url_for("dashboard"))
112
  else:
113
+ flash("Login failed", "danger")
114
  return render_template("admin_login.html")
115
 
116
  @app.route("/dashboard", methods=["GET", "POST"])
 
130
  (otp, now.isoformat(), expires.isoformat(), ip, "generated"))
131
  conn.commit()
132
 
 
133
  otp_row = [otp, now.isoformat(), expires.isoformat(), None, ip, "generated"]
134
  upload_to_hf(otp_row)
135