Skip to content

Commit 865dfcc

Browse files
authored
Add demo site mode (#173)
* Add demo site mode * fix demo site whitelist
1 parent 5064903 commit 865dfcc

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

backend/app/core/conf.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ def validator_api_url(cls, values):
4949
values['OPENAPI_URL'] = None
5050
return values
5151

52+
# Demo mode
53+
# Only GET, OPTIONS requests are allowed
54+
DEMO_MODE: bool = True
55+
DEMO_MODE_EXCLUDE: set[tuple[str, str]] = {
56+
('POST', f'{API_V1_STR}/auth/login'),
57+
('POST', f'{API_V1_STR}/auth/logout'),
58+
('GET', f'{API_V1_STR}/auth/captcha'),
59+
}
60+
5261
# Uvicorn
5362
UVICORN_HOST: str = '127.0.0.1'
5463
UVICORN_PORT: int = 8000
@@ -109,6 +118,7 @@ def validator_api_url(cls, values):
109118
CASBIN_EXCLUDE: set[tuple[str, str]] = {
110119
('POST', f'{API_V1_STR}/auth/swagger_login'),
111120
('POST', f'{API_V1_STR}/auth/login'),
121+
('POST', f'{API_V1_STR}/auth/logout'),
112122
('POST', f'{API_V1_STR}/auth/register'),
113123
('GET', f'{API_V1_STR}/auth/captcha'),
114124
}
@@ -118,6 +128,7 @@ def validator_api_url(cls, values):
118128
MENU_EXCLUDE: list[str] = [
119129
'auth:swagger_login',
120130
'auth:login',
131+
'auth:logout',
121132
'auth:register',
122133
'auth:captcha',
123134
]

backend/app/core/registrar.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# -*- coding: utf-8 -*-
33
from contextlib import asynccontextmanager
44

5-
from fastapi import FastAPI
5+
from fastapi import FastAPI, Depends
66
from fastapi_limiter import FastAPILimiter
77
from fastapi_pagination import add_pagination
88
from starlette.middleware.authentication import AuthenticationMiddleware
@@ -15,6 +15,7 @@
1515
from backend.app.database.db_mysql import create_table
1616
from backend.app.middleware.jwt_auth_middleware import JwtAuthMiddleware
1717
from backend.app.middleware.opera_log_middleware import OperaLogMiddleware
18+
from backend.app.utils.demo_site import demo_site
1819
from backend.app.utils.health_check import ensure_unique_route_names, http_limit_callback
1920
from backend.app.utils.openapi import simplify_operation_ids
2021

@@ -135,8 +136,10 @@ def register_router(app: FastAPI):
135136
:param app: FastAPI
136137
:return:
137138
"""
139+
dependencies = [Depends(demo_site)] if settings.DEMO_MODE else None
140+
138141
# API
139-
app.include_router(v1)
142+
app.include_router(v1, dependencies=dependencies)
140143

141144
# Extra
142145
ensure_unique_route_names(app)

backend/app/utils/demo_site.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
from fastapi import Request
4+
5+
from backend.app.common.exception import errors
6+
from backend.app.core.conf import settings
7+
8+
9+
async def demo_site(request: Request):
10+
"""演示站点"""
11+
12+
method = request.method
13+
path = request.url.path
14+
if (
15+
settings.DEMO_MODE
16+
and method != 'GET'
17+
and method != 'OPTIONS'
18+
and (method, path) not in settings.DEMO_MODE_EXCLUDE
19+
):
20+
raise errors.ForbiddenError(msg='演示环境下禁止执行此操作')

0 commit comments

Comments
 (0)