This repository was archived by the owner on Jan 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
391 lines (315 loc) · 9.17 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
from typing import Optional
from pydantic import BaseModel
from fastapi import FastAPI, Response, Cookie
from fastapi.middleware.cors import CORSMiddleware
from connect import mongodb
from tools import jwttools
from tools import sandbox
app = FastAPI()
# TODO
origins = ["http://localhost:3000", "http://127.0.0.1:3000", "YOUR_URL_HERE"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class User(BaseModel):
username: str
password: str
nickname: Optional[str]
bio: Optional[str]
class _TestCase(BaseModel):
in_: str
out: str
description: str
class Config:
fields = {
'in_': 'in'
}
class Problem(BaseModel):
testcases: list[_TestCase]
description: str
title: str
class SolveProblem(BaseModel):
id: str
code: str
lang: str
@app.get("/")
def read_root():
return {"여러분!": "블루아카는 갓겜입니다!"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: Optional[str] = None):
return {"item_id": item_id, "q": q}
@app.post("/user", status_code=201)
def create_user(user: User):
if len(user.password) < 6:
return {
'result': 'error',
'message': '비밀번호는 6자 이상으로!'
}
username = user.username.lower()
result = mongodb.create_user(username, user.password, user.nickname, user.bio)
if result:
# Issue JWT
jwt = jwttools.issue_jwt(username, user.nickname)
return {
'result': 'success',
'data': {
'token': jwt
}
}
else:
return {
'result': 'error',
'message': '이미 사용 중인 사용자 이름/닉네임입니다. 다른 이름으로 시도해 주세요.'
}
# 사용자 조회
@app.get('/info')
def get_info(jwt: Optional[str] = Cookie(None)):
if jwt:
decoded = jwttools.validate_jwt(jwt)
if not decoded:
return {
'result': 'error',
'message': '토큰 오류'
}
else:
return {
'result': 'success',
'data':{
'username': decoded['username'],
'nickname': decoded['nickname']
}
}
else:
return {
'result': 'error',
'message': '토큰 오류'
}
# 사용자 조회
@app.get('/user')
def get_user(username: str, token: Optional[str] = None):
result = mongodb.get_user(username)
if result:
if token:
# JWT 확인
decoded = jwttools.validate_jwt(token)
if not decoded:
return {
'result': 'error',
'message': '토큰 오류'
}
# 자기꺼 조회시에는
if username == decoded['username']:
# 나중에 필요시 구현
print('디버그: 동일인물')
return {
'result': 'success',
'data': result
}
return {
'result': 'success',
'data': result
}
else:
return {
'result': 'error',
'message': '그런 사람은 읎어요'
}
@app.post("/logout")
def logout(response: Response):
response.set_cookie(key="jwt", value='', samesite="none", httponly=True)
return {
'result': 'success'
}
@app.post("/login")
def login(user: User, response: Response):
username = user.username.lower()
result = mongodb.get_user(username, user.password)
if result:
# Issue JWT
jwt = jwttools.issue_jwt(username, result['nickname'])
response.set_cookie(key="jwt", value=jwt, samesite="none", httponly=True)
return {
'result': 'success',
'data': {
'token': jwt
}
}
else:
return {
'result': 'error',
'message': '로그인 실패! ㅋㅋㅋ'
}
@app.get('/rank')
def get_rank():
"""
{'username': 'elon', 'nickname': '머스크캣걸', 'score': 3453535, 'num_solved': 0}
"""
return {
'result': 'success',
'data': mongodb.get_rank()
}
# 문제 등록
@app.post('/p')
def create_problem(params: Problem, jwt: Optional[str] = Cookie(None)):
"""
DB 구조:
{
_id: ...
author: str
testcases: [
{
in
out
description
}
]
description: str (마크다운)
success: [ # 사용자 이름
str
str
...
]
}
"""
if not jwt:
return {
'result': 'error',
'message': '토큰 오류'
}
# JWT Auth
decoded = jwttools.validate_jwt(jwt)
if not decoded:
return {
'result': 'error',
'message': '토큰 오류'
}
tc = []
for i in params.testcases:
a = i.dict()
a['in'] = a.pop('in_')
tc.append(a)
req = {
'testcases': tc,
'description': params.description,
'author': decoded['username'],
'title': params.title.strip()
}
result = mongodb.create_problem(req)
if result:
return {
'result': 'success',
'data': {
'id': result
}
}
else:
return {
'result': 'error',
'message': '문제 생성 실패'
}
# 문제 고유 id 나 이런걸 돌려줘야겠지?
@app.get('/p')
def get_problems(id: Optional[str] = None):
# 이미 풀이한 문제는 클라에서 따로 user get 요청 때려서 처리.
# id가 주어질 경우에는 주어진 문제꺼를 가져오고, 아니면 전부다(리스트를) 가져온다.
if id:
result = mongodb.get_problem(id)
if result:
return {
'result': 'success',
'data': result
}
else:
return {
'result': 'error',
'message': 'Not Found'
}
else:
result = mongodb.list_problems()
return {
'result': 'success',
'data': result
}
# 문제 풀이 시도
@app.post('/solve')
def solve(params: SolveProblem, jwt: Optional[str] = Cookie(None)):
# params.id
# params.lang
# params.code
if not jwt:
return {
'result': 'error',
'message': '토큰 오류'
}
# JWT Auth
decoded = jwttools.validate_jwt(jwt)
if not decoded:
return {
'result': 'error',
'message': '토큰 오류'
}
problem = mongodb.get_problem(params.id)
if problem:
for testcase in problem['testcases']:
out, err, timeout = sandbox.execute_in_sandbox(params.code, params.lang, testcase['in'].strip() + '\n')
if timeout:
return {
'result': 'fail',
'data': {
'message': '타임아웃: 제한 시간 5초를 초과했습니다.',
'out': out.strip(),
'err': err.strip(),
'expected': testcase['out'].strip()
}
}
elif err:
return {
'result': 'fail',
'data': {
'message': '오류가 발생했습니다:',
'out': out.strip(),
'err': err.strip(),
'expected': testcase['out'].strip()
}
}
else:
if out.strip() == testcase['out'].strip():
continue
else:
return {
'result': 'fail',
'data': {
'message': '틀렸습니다: 테스트케이스 검증 실패!',
'out': out.strip(),
'err': '',
'expected': testcase['out'].strip()
}
}
# 성공했을 경우
mongodb.append_successor(params.id, decoded['username'])
mongodb.levelup_user(decoded['username'], params.id, 100)
return {
'result': 'success',
'data': {
'message': '정답!',
'out': out.strip(),
'err': '',
'expected': testcase['out'].strip()
}
}
else:
return {
'result': 'error',
'message': '그런 문제는 읎어요'
}
# 지정된 언어 샌드박스 실행후 결과 비교
# 풀었으면 유저 (score, solved: id), 문제(success append) 수정한후 success 리턴
pass
# 필요한 기능
# 1. 회원가입 v / 로그인 v / 유저 푼문제 v , 랭킹 v , etc.
# 2. 문제 등록 (stdin/stdout) / 솔빙처리, 기록 / 문제 목록
# 3. 문제 solve