Toggle navigation
Toggle navigation
This project
Loading...
Sign in
万朱浩
/
Venue-Ops
Go to a project
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
sukiun
2024-12-10 20:54:11 +0800
Browse Files
Options
Browse Files
Download
Plain Diff
Committed by
GitHub
2024-12-10 20:54:11 +0800
Commit
72f7c2aa616d36c7884d009f1fdf2361d2bf5de2
72f7c2aa
2 parents
933471a4
158c0b8c
Merge pull request #3 from sukiyra/main
Update app.py
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
0 deletions
app.py
app.py
View file @
72f7c2a
...
...
@@ -4,6 +4,7 @@ from apscheduler.schedulers.background import BackgroundScheduler
import
subprocess
import
os
from
pytz
import
utc
import
logging
app
=
Flask
(
__name__
)
app
.
secret_key
=
'this is secret_key you know ?'
...
...
@@ -17,6 +18,7 @@ app.register_blueprint(user.ub)
def
hello_world
():
# put application's code here
return
session
.
clear
()
"""
@app.before_request
def before_reuqest():
pat = re.compile(r'^/static')
...
...
@@ -24,6 +26,21 @@ def before_reuqest():
elif request.path == '/user/login' or request.path == '/user/register':return
elif session.get('username'):return
return redirect('/user/login')
"""
#中间件代码逻辑可以优化,以减少重复的 return 语句,并提高可读性:
@app.before_request
def
before_request
():
# 静态文件路径允许直接访问
if
request
.
path
.
startswith
(
'/static'
):
return
# 登录和注册页面无需验证会话
if
request
.
path
in
[
'/user/login'
,
'/user/register'
]:
return
# 验证用户是否登录
if
not
session
.
get
(
'username'
):
return
redirect
(
'/user/login'
)
@app.route
(
'/<path:path>'
)
def
catch_all
(
path
):
...
...
@@ -59,3 +76,9 @@ if __name__ == '__main__':
app
.
run
()
finally
:
scheduler
.
shutdown
()
#为了更好地调试和监控,建议为应用添加日志记录,捕获用户请求和错误:
logging
.
basicConfig
(
level
=
logging
.
INFO
)
@app.before_request
def
log_request_info
():
logging
.
info
(
f
"Request: {request.method} {request.path}"
)
...
...
Please
register
or
login
to post a comment