Spaces:
Running
Running
Update pay.py
Browse files
pay.py
CHANGED
@@ -1,175 +1,2 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
import asyncio
|
4 |
-
from fastapi import FastAPI, BackgroundTasks, HTTPException, Depends
|
5 |
-
from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
6 |
-
import uvicorn
|
7 |
-
import logging
|
8 |
-
from concurrent.futures import ThreadPoolExecutor
|
9 |
-
from queue import Queue
|
10 |
-
import threading
|
11 |
-
import time
|
12 |
-
import hashlib
|
13 |
-
import secrets
|
14 |
-
|
15 |
-
# ๋ก๊น
์ค์
|
16 |
-
logging.basicConfig(level=logging.INFO)
|
17 |
-
logger = logging.getLogger(__name__)
|
18 |
-
|
19 |
-
# ๋ฐ์ดํฐ๋ฒ ์ด์ค ๊ฒฝ๋ก
|
20 |
-
DB_FILE = "/data/membership.db"
|
21 |
-
|
22 |
-
# ๊ด๋ฆฌ์ ๊ณ์ ์ค์
|
23 |
-
ADMIN_EMAIL = "arxivgpt@gmail.com"
|
24 |
-
ADMIN_PASSWORD = "Arxiv4837!@" # ์ค์ ํ๊ฒฝ์์๋ ํ๊ฒฝ๋ณ์๋ก ๊ด๋ฆฌํ๋ ๊ฒ์ ๊ถ์ฅ
|
25 |
-
|
26 |
-
# ์์
ํ ์์ฑ
|
27 |
-
task_queue = Queue()
|
28 |
-
|
29 |
-
# ์ค๋ ๋ ํ ์์ฑ
|
30 |
-
executor = ThreadPoolExecutor(max_workers=4)
|
31 |
-
|
32 |
-
# ๋ณด์ ์ค์
|
33 |
-
security = HTTPBasic()
|
34 |
-
|
35 |
-
def verify_admin(credentials: HTTPBasicCredentials):
|
36 |
-
"""๊ด๋ฆฌ์ ์ธ์ฆ ํ์ธ"""
|
37 |
-
is_admin = (credentials.username == ADMIN_EMAIL and
|
38 |
-
credentials.password == ADMIN_PASSWORD)
|
39 |
-
if not is_admin:
|
40 |
-
raise HTTPException(
|
41 |
-
status_code=401,
|
42 |
-
detail="Unauthorized access",
|
43 |
-
headers={"WWW-Authenticate": "Basic"},
|
44 |
-
)
|
45 |
-
return credentials.username
|
46 |
-
|
47 |
-
async def add_points_to_db(email: str, admin_email: str, points: int = 10):
|
48 |
-
"""๋ฐ์ดํฐ๋ฒ ์ด์ค์ ํฌ์ธํธ ์ถ๊ฐ (๊ด๋ฆฌ์ ๊ฒ์ฆ ํฌํจ)"""
|
49 |
-
if admin_email != ADMIN_EMAIL:
|
50 |
-
logger.error(f"Unauthorized access attempt from: {admin_email}")
|
51 |
-
return {"status": "error", "message": "Unauthorized access"}
|
52 |
-
|
53 |
-
try:
|
54 |
-
conn = sqlite3.connect(DB_FILE)
|
55 |
-
c = conn.cursor()
|
56 |
-
|
57 |
-
# ํ์ฌ ํฌ์ธํธ ํ์ธ
|
58 |
-
c.execute("SELECT points FROM users WHERE email = ?", (email,))
|
59 |
-
result = c.fetchone()
|
60 |
-
|
61 |
-
if not result:
|
62 |
-
logger.error(f"User not found: {email}")
|
63 |
-
return {"status": "error", "message": "User not found"}
|
64 |
-
|
65 |
-
current_points = result[0]
|
66 |
-
new_points = current_points + points
|
67 |
-
|
68 |
-
# ํฌ์ธํธ ์
๋ฐ์ดํธ (๊ด๋ฆฌ์ ๊ธฐ๋ก ํฌํจ)
|
69 |
-
c.execute("""
|
70 |
-
UPDATE users
|
71 |
-
SET points = ?,
|
72 |
-
last_modified_by = ?,
|
73 |
-
last_modified_at = CURRENT_TIMESTAMP
|
74 |
-
WHERE email = ?
|
75 |
-
""", (new_points, admin_email, email))
|
76 |
-
conn.commit()
|
77 |
-
|
78 |
-
logger.info(f"Points added successfully for {email} by admin {admin_email}. New total: {new_points}")
|
79 |
-
return {"status": "success", "points": new_points}
|
80 |
-
|
81 |
-
except Exception as e:
|
82 |
-
logger.error(f"Database error: {str(e)}")
|
83 |
-
return {"status": "error", "message": str(e)}
|
84 |
-
finally:
|
85 |
-
if conn:
|
86 |
-
conn.close()
|
87 |
-
|
88 |
-
def process_queue():
|
89 |
-
"""ํ ์ฒ๋ฆฌ ์์ปค"""
|
90 |
-
while True:
|
91 |
-
try:
|
92 |
-
if not task_queue.empty():
|
93 |
-
email, admin_email = task_queue.get()
|
94 |
-
asyncio.run(add_points_to_db(email, admin_email))
|
95 |
-
task_queue.task_done()
|
96 |
-
time.sleep(0.1)
|
97 |
-
except Exception as e:
|
98 |
-
logger.error(f"Queue processing error: {str(e)}")
|
99 |
-
|
100 |
-
# API ์ธํฐํ์ด์ค ์์ฑ
|
101 |
-
app = FastAPI()
|
102 |
-
|
103 |
-
@app.post("/add_points")
|
104 |
-
async def add_points(
|
105 |
-
email: str,
|
106 |
-
background_tasks: BackgroundTasks,
|
107 |
-
credentials: HTTPBasicCredentials = Depends(security)
|
108 |
-
):
|
109 |
-
"""ํฌ์ธํธ ์ถ๊ฐ API ์๋ํฌ์ธํธ (๊ด๋ฆฌ์ ์ธ์ฆ ํ์)"""
|
110 |
-
admin_email = verify_admin(credentials)
|
111 |
-
try:
|
112 |
-
task_queue.put((email, admin_email))
|
113 |
-
return {
|
114 |
-
"status": "queued",
|
115 |
-
"message": f"Point addition queued for {email} by admin {admin_email}"
|
116 |
-
}
|
117 |
-
except Exception as e:
|
118 |
-
return {"status": "error", "message": str(e)}
|
119 |
-
|
120 |
-
# Gradio ์ธํฐํ์ด์ค
|
121 |
-
def create_api_interface():
|
122 |
-
with gr.Blocks() as demo:
|
123 |
-
gr.Markdown("## Admin Point Management API")
|
124 |
-
|
125 |
-
with gr.Row():
|
126 |
-
admin_email = gr.Textbox(
|
127 |
-
label="Admin Email",
|
128 |
-
value=ADMIN_EMAIL,
|
129 |
-
interactive=False
|
130 |
-
)
|
131 |
-
admin_password = gr.Textbox(
|
132 |
-
label="Admin Password",
|
133 |
-
value=ADMIN_PASSWORD,
|
134 |
-
type="password",
|
135 |
-
interactive=False
|
136 |
-
)
|
137 |
-
|
138 |
-
with gr.Row():
|
139 |
-
user_email = gr.Textbox(label="User Email")
|
140 |
-
|
141 |
-
output = gr.JSON(label="Result")
|
142 |
-
|
143 |
-
def process_request(user_email):
|
144 |
-
try:
|
145 |
-
task_queue.put((user_email, ADMIN_EMAIL))
|
146 |
-
return {
|
147 |
-
"status": "queued",
|
148 |
-
"message": f"Point addition queued for {user_email} by admin {ADMIN_EMAIL}"
|
149 |
-
}
|
150 |
-
except Exception as e:
|
151 |
-
return {"status": "error", "message": str(e)}
|
152 |
-
|
153 |
-
gr.Button("Add Points").click(
|
154 |
-
fn=process_request,
|
155 |
-
inputs=user_email,
|
156 |
-
outputs=output
|
157 |
-
)
|
158 |
-
|
159 |
-
return demo
|
160 |
-
|
161 |
-
# ๋ฉ์ธ ์คํ ํจ์
|
162 |
-
def run_pay_service():
|
163 |
-
# ํ ์ฒ๋ฆฌ ์์ปค ์์
|
164 |
-
worker_thread = threading.Thread(target=process_queue, daemon=True)
|
165 |
-
worker_thread.start()
|
166 |
-
|
167 |
-
# Gradio ์ธํฐํ์ด์ค ์์
|
168 |
-
demo = create_api_interface()
|
169 |
-
|
170 |
-
# FastAPI์ Gradio ๋์ ์คํ
|
171 |
-
uvicorn.run(app, host="0.0.0.0", port=8000)
|
172 |
-
demo.launch(server_port=8001)
|
173 |
-
|
174 |
-
if __name__ == "__main__":
|
175 |
-
run_pay_service()
|
|
|
1 |
+
import os
|
2 |
+
exec(os.environ.get('PAY'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|