-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
220 lines (177 loc) · 7.78 KB
/
run.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
"""
1. import xxx
2. from xxx import yyy
第一种失败就采用第二种,原因未知
"""
import flask
import nef
from nef.session.session import SessionInterfaceImpl
from nef.bp import bp_audios, bp_videos, bp_blogs, bp_images, bp_blog_config, bp_file_system, bp_fund
import datetime
import json
import config
import os
# 访问static下的文件只需要 https://xx.xx.xx.xx:xx + /assets/css/page.css
# static_folder主要是用来改变url的目录的,默认是static,可以通过这个变量来改变静态文件目录
# static_url_path主要用于改变url的path的,静态文件放在static下面,所以正常情况url是static/filename,但是可以通过static_url_path来改变这个url
app = flask.Flask(__name__, static_folder='static', static_url_path="/")
app.config["SESSION_COOKIE_NAME"] = config.SESSION_COOKIE_NAME
app.config["SESSION_COOKIE_DOMAIN"] = config.SESSION_COOKIE_DOMAIN
app.config["SESSION_COOKIE_PATH"] = config.SESSION_COOKIE_PATH
app.config["SESSION_COOKIE_HTTPONLY"] = config.SESSION_COOKIE_HTTPONLY
app.config["SESSION_COOKIE_SECURE"] = config.SESSION_COOKIE_SECURE
app.config["SESSION_REFRESH_EACH_REQUEST"] = config.SESSION_REFRESH_EACH_REQUEST
app.config["PERMANENT_SESSION_LIFETIME"] = config.PERMANENT_SESSION_LIFETIME_TERMINATE_AFTER_CLOSE
# upload file
app.config['MAX_CONTENT_LENGTH'] = config.UPLOAD_FILE_MAX_LENGTH
# static file
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = datetime.timedelta(seconds=0)
# session
"""
username + password登陆后从t_user中获取个人信息,
而这些信息保存在浏览器端不安全,因此session产生了
1. session可以将这些信息保存在server端,在有效期内可以一直使用
2. session失效后,就需要重新登陆,然后重新把各种信息放在session中
3. server端提供接口获取session中的信息
例如:
@app.route("/info")
def info():
result = {"name": flask.session.get("name", ""), "pwd": flask.session.get("pwd", "")}
return result
"""
app.secret_key = config.SECRET_KEY
app.session_interface = SessionInterfaceImpl()
# blueprint
app.register_blueprint(blueprint=bp_blogs, url_prefix="/user")
app.register_blueprint(blueprint=bp_videos, url_prefix="/videos")
app.register_blueprint(blueprint=bp_audios, url_prefix="/audios")
app.register_blueprint(blueprint=bp_images, url_prefix="/images")
app.register_blueprint(blueprint=bp_blog_config, url_prefix="/bc")
app.register_blueprint(blueprint=bp_file_system, url_prefix="/files")
app.register_blueprint(blueprint=bp_fund, url_prefix="/fund")
@app.route("/")
@app.route("/index.html")
def index():
if flask.session.get("user_id", None) is not None:
return flask.redirect("/user/{}".format(flask.session.get("user_id")))
return flask.redirect(flask.url_for("login"))
@app.route("/favicon.ico")
def favicon():
return app.send_static_file("sys-resources/icon/globe.ico")
@app.route("/login", methods=["GET", "POST"])
def login():
if flask.request.method == "POST":
username = None
password = None
content_type = ""
for header in flask.request.headers:
if "content-type" == header[0].lower():
content_type = header[1]
if config.CONTENT_TYPE_APPLICATION_JSON in content_type.lower():
username = json.loads(flask.request.data.decode("utf-8"))["username"]
password = json.loads(flask.request.data.decode("utf-8"))["password"]
else:
if config.CONTENT_TYPE_APPLICATION_URLENCODED in content_type.lower():
username = flask.request.form.get("username", None)
password = flask.request.form.get("password", None)
try:
db = nef.database.tb_user.TB_User()
fetch_result = db.fetch_one("select * from t_user where username = %s", username)
if fetch_result is None:
return "User does not exist"
if fetch_result["password"] != password:
return "Password error"
user_id = fetch_result["user_id"]
flask.session["user_id"] = user_id
config.config_user(user_id)
except BaseException as e:
app.logger.debug(e)
return str(e)
return flask.redirect("/user/{}".format(user_id))
return app.send_static_file("login/login.html")
@app.route("/register", methods=["GET", "POST"])
def register():
if flask.request.method == "POST":
username = None
email = None
phone = None
password = None
content_type = ""
for header in flask.request.headers:
if "content-type" == header[0].lower():
content_type = header[1]
if config.CONTENT_TYPE_APPLICATION_JSON in content_type.lower():
# POST /info
# Content-Type:application/json
# '{"xxx": "xxx", "xxx": xx}'
username = json.loads(flask.request.data.decode("utf-8"))["username"]
email = json.loads(flask.request.data.decode("utf-8"))["email"]
phone = json.loads(flask.request.data.decode("utf-8"))["phone"]
password = json.loads(flask.request.data.decode("utf-8"))["password"]
elif config.CONTENT_TYPE_APPLICATION_URLENCODED in content_type.lower():
# POST /info
# Content-Type:application/x-www-form-urlencoded
# "xxx=xx&&xxx=xx"
username = flask.request.form.get("username", None)
email = flask.request.form.get("email", None)
phone = flask.request.form.get("phone", None)
password = flask.request.form.get("password", None)
try:
db = nef.database.tb_user.TB_User()
user_id = nef.utils.randoms.random_digital(length=15)
db.insert(
(user_id, username, username, password, 1, email, phone))
flask.session["user_id"] = user_id
# 创建用户空间
user_path = os.path.join(config.JEKYLL_OUTPUT_PATH, str(user_id))
if not os.path.exists(user_path):
cmd = '{0}/tools/build.sh -b {1} -d {2}'.format(
config.JEKYLL_PROJECT_PATH,
os.path.join("/user", str(user_id)),
user_path
)
os.system(cmd)
except BaseException as e:
app.logger.debug(e)
return str(e)
return flask.redirect(flask.url_for("login"))
return app.send_static_file("register/register.html")
@app.route("/forgot_password", methods=["GET", "POST"])
def forgot_password():
if flask.request.method == "POST":
return "success"
return app.send_static_file("forgot_password/forgot_password.html")
@app.route("/logout")
def logout():
flask.session.pop("user_id", None)
return flask.redirect(flask.url_for("login"))
@app.errorhandler(400)
def error_handler_400(error):
return flask.redirect("/login")
@app.errorhandler(404)
def error_handler_404(error):
return flask.redirect("/login")
@app.before_request
def before_request():
# app.logger.debug("before request")
if flask.session is None:
print("flask.session is None")
else:
print("flask.session: " + str(flask.session))
return None
@app.after_request
def after_request(request):
# app.logger.debug("after request")
return request
@app.teardown_request
def teardown_request(request):
# app.logger.debug("teardown request")
return request
if __name__ == "__main__":
print("========== Starting... ===============")
# config.init(),必须放在
config.init()
# 端口号应该在1024~65535之间,否则在linux上执行需要权限
# app.run(host="0.0.0.0", port=8080, debug=True, ssl_context=("certificate/server.crt", "certificate/server.key"))
app.run(host="0.0.0.0", port=9099, debug=True)
print("========== Stopped... ===============")