Skip to content

Add demo site mode #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions backend/app/core/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ def validator_api_url(cls, values):
values['OPENAPI_URL'] = None
return values

# Demo mode
# Only GET, OPTIONS requests are allowed
DEMO_MODE: bool = True
DEMO_MODE_EXCLUDE: set[tuple[str, str]] = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useless

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixd

('POST', f'{API_V1_STR}/auth/login'),
('POST', f'{API_V1_STR}/auth/logout'),
('GET', f'{API_V1_STR}/auth/captcha'),
}

# Uvicorn
UVICORN_HOST: str = '127.0.0.1'
UVICORN_PORT: int = 8000
Expand Down Expand Up @@ -109,6 +118,7 @@ def validator_api_url(cls, values):
CASBIN_EXCLUDE: set[tuple[str, str]] = {
('POST', f'{API_V1_STR}/auth/swagger_login'),
('POST', f'{API_V1_STR}/auth/login'),
('POST', f'{API_V1_STR}/auth/logout'),
('POST', f'{API_V1_STR}/auth/register'),
('GET', f'{API_V1_STR}/auth/captcha'),
}
Expand All @@ -118,6 +128,7 @@ def validator_api_url(cls, values):
MENU_EXCLUDE: list[str] = [
'auth:swagger_login',
'auth:login',
'auth:logout',
'auth:register',
'auth:captcha',
]
Expand Down
7 changes: 5 additions & 2 deletions backend/app/core/registrar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-
from contextlib import asynccontextmanager

from fastapi import FastAPI
from fastapi import FastAPI, Depends
from fastapi_limiter import FastAPILimiter
from fastapi_pagination import add_pagination
from starlette.middleware.authentication import AuthenticationMiddleware
Expand All @@ -15,6 +15,7 @@
from backend.app.database.db_mysql import create_table
from backend.app.middleware.jwt_auth_middleware import JwtAuthMiddleware
from backend.app.middleware.opera_log_middleware import OperaLogMiddleware
from backend.app.utils.demo_site import demo_site
from backend.app.utils.health_check import ensure_unique_route_names, http_limit_callback
from backend.app.utils.openapi import simplify_operation_ids

Expand Down Expand Up @@ -135,8 +136,10 @@ def register_router(app: FastAPI):
:param app: FastAPI
:return:
"""
dependencies = [Depends(demo_site)] if settings.DEMO_MODE else None

# API
app.include_router(v1)
app.include_router(v1, dependencies=dependencies)

# Extra
ensure_unique_route_names(app)
Expand Down
20 changes: 20 additions & 0 deletions backend/app/utils/demo_site.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from fastapi import Request

from backend.app.common.exception import errors
from backend.app.core.conf import settings


async def demo_site(request: Request):
"""演示站点"""

method = request.method
path = request.url.path
if (
settings.DEMO_MODE
and method != 'GET'
and method != 'OPTIONS'
and (method, path) not in settings.DEMO_MODE_EXCLUDE
):
raise errors.ForbiddenError(msg='演示环境下禁止执行此操作')