ginipick commited on
Commit
33d8993
ยท
verified ยท
1 Parent(s): f557217

Update pay.py

Browse files
Files changed (1) hide show
  1. pay.py +2 -175
pay.py CHANGED
@@ -1,175 +1,2 @@
1
- import gradio as gr
2
- import sqlite3
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'))