-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
139 lines (112 loc) · 4.37 KB
/
app.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
from pymongo import MongoClient
from flask import Flask, request, render_template, jsonify
import jwt, datetime
app = Flask(__name__)
# MongoDB
client = MongoClient('localhost', 27017)
db = client.typejungle
SECRET_KEY = "JUNGLE"
# [html 뷰어]
@app.route('/')
def start():
return render_template('login.html')
@app.route('/login')
def login():
return render_template('login.html')
@app.route('/signup')
def signup():
return render_template('signup.html')
@app.route('/home')
def home():
return render_template('home.html')
@app.route('/game')
def game():
return render_template('game.html')
@app.route('/result')
def result():
return render_template('result.html')
# [회원가입 API]
@app.route('/api/signup', methods=['POST'])
def api_signup():
id_receive = request.form['id_give']
pw_receive = request.form['pw_give']
check_receive = request.form['check_give']
if not id_receive:
return jsonify({'result': 'fail', 'msg': '아이디를 입력해주세요'})
if not pw_receive:
return jsonify({'result': 'fail', 'msg': '비밀번호를 입력해주세요'})
if pw_receive != check_receive:
return jsonify({'result': 'fail', 'msg': '비밀번호를 다시 확인해주세요'})
check = db.users.find_one({'id': id_receive})
if check is not None:
return jsonify({'result': 'fail', 'msg': '중복된 id가 존재합니다'})
db.users.insert_one({'id': id_receive, 'pw': pw_receive})
return jsonify({'result': 'success'})
# [로그인 API]
@app.route('/api/login', methods=['POST'])
def api_login():
id_receive = request.form['id_give']
pw_receive = request.form['pw_give']
result = db.users.find_one({'id': id_receive, 'pw': pw_receive})
if result:
payload = {
'id': id_receive,
'exp': datetime.datetime.utcnow() + datetime.timedelta(seconds=1000)
}
# token을 클라이언트에게 전달.
token = jwt.encode(payload, SECRET_KEY, algorithm='HS256')
return jsonify({'result': 'success', 'token': token})
else:
return jsonify({'result': 'fail', 'msg': '아이디/비밀번호가 일치하지 않습니다.'})
# [유저 정보 확인 API]
# 로그인된 유저만 call 할 수 있음.
@app.route('/api/token', methods=['GET'])
def api_valid():
token_receive = request.cookies.get('mytoken')
try:
# token을 시크릿키로 디코딩
payload = jwt.decode(token_receive, SECRET_KEY, algorithms=['HS256'])
return jsonify({'result': 'success', 'id': payload['id']})
except jwt.ExpiredSignatureError:
# 위를 실행했는데 만료시간이 지났으면 에러가 납니다.
return jsonify({'result': 'fail', 'msg': '로그인 시간이 만료되었습니다.'})
except jwt.exceptions.DecodeError:
return jsonify({'result': 'fail', 'msg': '로그인 정보가 존재하지 않습니다.'})
# [로그 API]
@app.route('/api/log', methods=['POST'])
def api_log():
id_receive = request.form['id_give']
score_receive = int(request.form['score_give'])
db.log.insert_one({'id':id_receive, 'score': score_receive, 'time': datetime.datetime.now()})
result = db.ranking.find_one({'id':id_receive})
if result is not None:
db.ranking.delete_one({'id': id_receive})
score_receive = min(result['score'], score_receive)
db.ranking.insert_one({'id': id_receive, 'score': score_receive})
return jsonify({'result': 'success'})
# [랭킹 API]
@app.route('/api/rank', methods=['GET'])
def api_rank():
ranking_ls = list(db.ranking.find({}, {'_id': False}).sort('score', 1))
if len(ranking_ls) > 10:
top10_ls = ranking_ls[:10]
else:
top10_ls = ranking_ls
return jsonify({'rank': top10_ls})
# [결과 API]
@app.route('/api/score', methods=['GET'])
def api_score():
name = request.args.get('id_give')
score = list(db.log.find({'id':name}, {'_id': False}))
return jsonify({'score': score[-1]})
# [내 기록 API]
@app.route('/api/best', methods=['GET'])
def api_best():
name = request.args.get('id_give')
score = list(db.log.find({'id':name}, {'_id': False}).sort('score', 1))
if score == []:
return jsonify({'result': 'fail'})
else:
return jsonify({'result': 'success','score': score[0]})
if __name__ == '__main__':
app.run(host = '0.0.0.0', port = 5001, debug = True)