Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
1 Star 0 Fork 406

luyuan/FastapiAdmin

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (5)
Tags (5)
master
dev
v3.0.0
v2.0.0
v1.0.0
v3.0
v2.0
V2.0
v1.0
V1.0
master
Branches (5)
Tags (5)
master
dev
v3.0.0
v2.0.0
v1.0.0
v3.0
v2.0
V2.0
v1.0
V1.0
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (5)
Tags (5)
master
dev
v3.0.0
v2.0.0
v1.0.0
v3.0
v2.0
V2.0
v1.0
V1.0
main.py 3.51 KB
Copy Edit Raw Blame History
zhangtao authored 2026年06月21日 06:02 +08:00 . refactor: 完成项目大规模代码重构与依赖清理
import os
from typing import Annotated
import typer
import uvicorn
from alembic.config import Config
from fastapi import FastAPI
from alembic import command
from app.common.enums import EnvironmentEnum
from app.config.setting import settings
from app.core.logger import logger
from app.utils.banner import worship
fastapiadmin_cli = typer.Typer()
alembic_cfg = Config("alembic.ini")
def create_app() -> FastAPI:
"""
创建 FastAPI 应用实例并完成日志、中间件、路由与静态资源注册。
返回:
- FastAPI: 已配置生命周期的应用对象。
"""
from app.init_app import lifespan, register_exceptions, register_files, register_middlewares, register_routers, reset_api_docs
# 创建FastAPI应用
app = FastAPI(**settings.FASTAPI_CONFIG, lifespan=lifespan)
# 注册异常处理器
register_exceptions(app)
# 注册中间件
register_middlewares(app)
# 注册路由
register_routers(app)
# 注册静态文件
register_files(app)
# 重设API文档
reset_api_docs(app)
return app
# typer.Option是非必填;typer.Argument是必填
@fastapiadmin_cli.command(
name="run",
help="启动 FastapiAdmin 服务, 运行 uv run main.py run --env=dev 不加参数默认 dev 环境",
)
def run(
env: Annotated[
EnvironmentEnum, typer.Option("--env", help="运行环境 (dev, prod)")
] = EnvironmentEnum.DEV,
) -> None:
"""
按指定环境加载配置并启动 Uvicorn(开发环境开启 reload)。
参数:
- env (EnvironmentEnum): 运行环境,对应 `--env`。
返回:
- None
"""
# 设置环境变量(必须在 import settings 之前,确保加载正确环境)
os.environ["ENVIRONMENT"] = env.value
typer.secho(
message="FastapiAdmin 服务启动",
fg=typer.colors.GREEN,
)
logger.info(worship(env.value))
# 启动uvicorn服务
uvicorn.run(
app="main:create_app",
host=settings.SERVER_HOST,
port=settings.SERVER_PORT,
reload=env.value == EnvironmentEnum.DEV.value,
factory=True,
log_config=None,
)
@fastapiadmin_cli.command(
name="revision",
help="生成新的 Alembic 迁移脚本, 运行 python main.py revision --env=dev",
)
def revision(
env: Annotated[
EnvironmentEnum, typer.Option("--env", help="运行环境 (dev, prod)")
] = EnvironmentEnum.DEV,
) -> None:
"""
使用 Alembic 自动生成迁移脚本(autogenerate)。
参数:
- env (EnvironmentEnum): 运行环境,用于加载对应数据库模型元数据。
返回:
- None
"""
os.environ["ENVIRONMENT"] = env.value
from app.config.setting import get_settings
get_settings.cache_clear()
command.revision(alembic_cfg, autogenerate=True, message="迁移脚本")
typer.echo("迁移脚本已生成")
@fastapiadmin_cli.command(
name="upgrade",
help="应用最新的 Alembic 迁移, 运行 python main.py upgrade --env=dev",
)
def upgrade(
env: Annotated[
EnvironmentEnum, typer.Option("--env", help="运行环境 (dev, prod)")
] = EnvironmentEnum.DEV,
) -> None:
"""
将数据库升级到 Alembic 最新版本(head)。
参数:
- env (EnvironmentEnum): 运行环境。
返回:
- None
"""
os.environ["ENVIRONMENT"] = env.value
from app.config.setting import get_settings
get_settings.cache_clear()
command.upgrade(alembic_cfg, "head")
typer.echo("所有迁移已应用。")
if __name__ == "__main__":
fastapiadmin_cli()
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

FastapiAdmin是一套完全开源的快速开发平台,提供免费使用。它结合了现代、高性能的技术栈,后端采用Fastapi + SQLAlchemy,前端采用基于 vue3 + typescript + vite + pinia + Element-Plus。旨在帮助开发者快速搭建高质量的中后台系统。
Cancel

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/java_doc/FastapiAdmin.git
git@gitee.com:java_doc/FastapiAdmin.git
java_doc
FastapiAdmin
FastapiAdmin
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /