新加入 Typer 命令行应用,简单化数据初始化,数据表模型迁移。
This commit is contained in:
parent
a8d32b0c61
commit
e7b5664f3b
113
README.md
113
README.md
@ -21,6 +21,7 @@ Kinit 是一套全部开源的快速开发平台,毫无保留给个人及企
|
||||
|
||||
- 后端采用 Python 语言现代、快速(高性能) [FastAPI](https://fastapi.tiangolo.com/zh/) 异步框架 + [SQLAlchemy](https://www.sqlalchemy.org/) 异步操作 [MySQL](https://www.mysql.com/) 数据库。
|
||||
- 前端采用 [vue-element-plus-admin](https://gitee.com/kailong110120130/vue-element-plus-admin) 、[Vue3](https://cn.vuejs.org/guide/introduction.html)、[Element Plus](https://element-plus.gitee.io/zh-CN/guide/design.html)、[TypeScript](https://www.tslang.cn/),等主流技术开发。
|
||||
- 新加入 [Typer](https://typer.tiangolo.com/) 命令行应用,简单化数据初始化,数据表模型迁移。
|
||||
- 权限认证使用[(哈希)密码和 JWT Bearer 令牌的 OAuth2](https://fastapi.tiangolo.com/zh/tutorial/security/oauth2-jwt/),支持多终端认证系统。
|
||||
- 支持加载动态权限菜单,多方式轻松权限控制,按钮级别权限控制。
|
||||
- 已加入常见的`Redis`、`MYSQL`、`MongoDB`数据库异步操作。
|
||||
@ -110,6 +111,8 @@ github地址:https://github.com/vvandk/kinit
|
||||
|
||||
- [x] 已加入常见的`Redis`、`MYSQL`、`MongoDB`数据库异步操作。
|
||||
|
||||
- [x] 命令行操作:新加入 `Typer` 命令行应用,简单化数据初始化,数据表模型迁移。
|
||||
|
||||
## TODO
|
||||
|
||||
- [ ] 考虑支持多机部署方案,如果接口使用多机,那么用户是否支持统一认证
|
||||
@ -121,6 +124,7 @@ github地址:https://github.com/vvandk/kinit
|
||||
## 前序准备
|
||||
|
||||
- [FastAPI](https://fastapi.tiangolo.com/zh/) - 熟悉后台接口 Web 框架
|
||||
- [Typer](https://typer.tiangolo.com/) - 熟悉命令行工具的使用
|
||||
- [node](https://gitee.com/link?target=http%3A%2F%2Fnodejs.org%2F) 和 [git](https://gitee.com/link?target=https%3A%2F%2Fgit-scm.com%2F) - 项目开发环境
|
||||
- [Vite](https://gitee.com/link?target=https%3A%2F%2Fvitejs.dev%2F) - 熟悉 vite 特性
|
||||
- [Vue3](https://gitee.com/link?target=https%3A%2F%2Fv3.vuejs.org%2F) - 熟悉 Vue 基础语法
|
||||
@ -154,9 +158,11 @@ git clone https://gitee.com/ktianc/kinit.git
|
||||
### 准备工作
|
||||
|
||||
```
|
||||
Python >= 3.8.0 (推荐3.8+版本)
|
||||
nodejs >= 14.0 (推荐最新)
|
||||
Python >= 3.10.0
|
||||
nodejs >= 14.0 (推荐使用最新稳定版)
|
||||
Mysql >= 8.0
|
||||
MongoDB (推荐使用最新稳定版)
|
||||
Redis (推荐使用最新稳定版)
|
||||
```
|
||||
|
||||
### 后端
|
||||
@ -169,27 +175,60 @@ cd kinit-api
|
||||
pip3 install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
|
||||
```
|
||||
|
||||
2. 修改数据库信息
|
||||
2. 修改项目数据库配置信息
|
||||
|
||||
在 `application/settings.py` 文件中配置数据库信息,用于项目连接
|
||||
在 `application/config` 目录中
|
||||
|
||||
- mysql数据库版本建议:8.0
|
||||
- mysql数据库字符集:utf8mb4
|
||||
- development.py:开发环境
|
||||
|
||||
- production.py:生产环境
|
||||
|
||||
```python
|
||||
"""
|
||||
数据库配置项
|
||||
Mysql 数据库配置项
|
||||
连接引擎官方文档:https://www.osgeo.cn/sqlalchemy/core/engines.html
|
||||
数据库链接配置说明:mysql+asyncmy://数据库用户名:数据库密码@数据库地址:数据库端口/数据库名称
|
||||
"""
|
||||
if DEBUG:
|
||||
# 测试库
|
||||
SQLALCHEMY_DATABASE_URL = "mysql+asyncmy://root:123456@127.0.0.1:3306/kinit"
|
||||
SQLALCHEMY_DATABASE_TYPE = "mysql"
|
||||
else:
|
||||
# 正式库
|
||||
SQLALCHEMY_DATABASE_URL = "mysql+asyncmy://root:123456@127.0.0.1:3306/kinit"
|
||||
SQLALCHEMY_DATABASE_URL = "mysql+asyncmy://数据库用户名:数据库密码@数据库地址:数据库端口/数据库名称"
|
||||
SQLALCHEMY_DATABASE_TYPE = "mysql"
|
||||
|
||||
|
||||
"""
|
||||
Redis 数据库配置
|
||||
"""
|
||||
REDIS_DB_ENABLE = True
|
||||
REDIS_DB_URL = "redis://:密码@地址:端口/数据库"
|
||||
|
||||
"""
|
||||
MongoDB 数据库配置
|
||||
"""
|
||||
MONGO_DB_ENABLE = True
|
||||
MONGO_DB_NAME = "数据库名称"
|
||||
MONGO_DB_URL = f"mongodb://用户名:密码@地址:端口/?authSource={MONGO_DB_NAME}"
|
||||
|
||||
"""
|
||||
阿里云对象存储OSS配置
|
||||
阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
|
||||
yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
|
||||
* [accessKeyId] {String}:通过阿里云控制台创建的AccessKey。
|
||||
* [accessKeySecret] {String}:通过阿里云控制台创建的AccessSecret。
|
||||
* [bucket] {String}:通过控制台或PutBucket创建的bucket。
|
||||
* [endpoint] {String}:bucket所在的区域, 默认oss-cn-hangzhou。
|
||||
"""
|
||||
ALIYUN_OSS = {
|
||||
"accessKeyId": "accessKeyId",
|
||||
"accessKeySecret": "accessKeySecret",
|
||||
"endpoint": "endpoint",
|
||||
"bucket": "bucket",
|
||||
"baseUrl": "baseUrl"
|
||||
}
|
||||
|
||||
"""
|
||||
获取IP地址归属地
|
||||
文档:https://user.ip138.com/ip/doc
|
||||
"""
|
||||
IP_PARSE_ENABLE = True
|
||||
IP_PARSE_TOKEN = "IP_PARSE_TOKEN"
|
||||
```
|
||||
|
||||
并在`alembic.ini`文件中配置数据库信息,用于数据库映射
|
||||
@ -207,39 +246,35 @@ mysql> use kinit; # 使用已创建的数据库
|
||||
mysql> set names utf8; # 设置编码
|
||||
```
|
||||
|
||||
4. 映射数据库
|
||||
4. 初始化数据库数据
|
||||
|
||||
```shell
|
||||
# 初次生成映射文件
|
||||
alembic revision -m "生成映射文件"
|
||||
|
||||
# 通过该命令可以将模型映射到数据库
|
||||
alembic upgrade head
|
||||
|
||||
# 如果有更新,则可以使用这个命令再次生成映射文件,初次也可以使用
|
||||
alembic revision --autogenerate -m "update"
|
||||
# --autogenerate:自动将当前模型的修改,生成映射脚本。
|
||||
|
||||
# 通过该命令可以将模型映射到数据库
|
||||
alembic upgrade head
|
||||
# 进入项目根目录下执行
|
||||
python3 main.py init
|
||||
```
|
||||
|
||||
5. 导入数据库数据
|
||||
5. 修改项目基本配置信息
|
||||
|
||||
导入数据库数据前,请先保存映射后数据库中`alembic_version`表中的`version_num`数据
|
||||
修改数据库表 - vadmin_system_settings 中的关键信息
|
||||
|
||||
导入完成后,将此数据替换到导入后的对应字段
|
||||
```python
|
||||
# 阿里云短信配置
|
||||
sms_access_key
|
||||
sms_access_key_secret
|
||||
sms_sign_name_1
|
||||
sms_template_code_1
|
||||
sms_sign_name_2
|
||||
sms_template_code_2
|
||||
|
||||
# 高德地图配置
|
||||
map_key
|
||||
```
|
||||
|
||||
6. 启动
|
||||
|
||||
```shell
|
||||
# 数据库文件地址:kinit-api/static/kinit.sql
|
||||
# 导入命令
|
||||
mysql> source kinit-api/static/kinit.sql # 导入备份数据库
|
||||
```
|
||||
|
||||
5. 启动
|
||||
|
||||
```
|
||||
python3 main.py
|
||||
# 进入项目根目录下执行
|
||||
python3 main.py run
|
||||
```
|
||||
|
||||
### 前端
|
||||
|
@ -37,9 +37,9 @@ SQLAlchemy-Utils:https://sqlalchemy-utils.readthedocs.io/en/latest/
|
||||
|
||||
## 开发环境
|
||||
|
||||
开发语言:Python 3.8
|
||||
开发语言:Python 3.10
|
||||
|
||||
开发框架:Fastapi 0.73.0
|
||||
开发框架:Fastapi 0.87.0
|
||||
|
||||
## 使用
|
||||
|
||||
@ -54,15 +54,22 @@ pip3 install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
|
||||
1. 阿里源: https://mirrors.aliyun.com/pypi/simple/
|
||||
```
|
||||
|
||||
### 数据初始化
|
||||
|
||||
```shell
|
||||
# 项目根目录下执行,需提前创建好数据库
|
||||
# 会自动将模型迁移到数据库,并生成初始化数据
|
||||
python main.py init
|
||||
```
|
||||
|
||||
### 运行启动
|
||||
|
||||
```shell
|
||||
# 直接运行main文件
|
||||
python main.py run
|
||||
```
|
||||
# 命令行运行(开发模式)
|
||||
uvicorn main:app --host=127.0.0.1 --port=9000 --reload
|
||||
|
||||
# 或者直接运行main文件
|
||||
python main.py
|
||||
```
|
||||
## 其他
|
||||
|
||||
在线文档地址(在配置文件里面设置路径或者关闭)
|
||||
|
||||
@ -86,19 +93,10 @@ git commit -m "clear cached"
|
||||
|
||||
执行数据库迁移命令(终端执行)
|
||||
|
||||
```python
|
||||
# 初次生成迁移文件
|
||||
alembic revision -m "生成迁移文件"
|
||||
```shell
|
||||
# 执行命令:
|
||||
|
||||
# 通过该命令可以将模型迁移到数据库
|
||||
alembic upgrade head
|
||||
|
||||
# 如果有更新,则可以使用这个命令再次生成迁移文件,初次也可以使用
|
||||
alembic revision --autogenerate -m "update"
|
||||
# --autogenerate:自动将当前模型的修改,生成迁移脚本。
|
||||
|
||||
# 通过该命令可以将模型迁移到数据库
|
||||
alembic upgrade head
|
||||
python main.py migrate
|
||||
```
|
||||
|
||||
生成迁移文件后,会在alembic迁移目录中的version目录中多个迁移文件
|
@ -8,8 +8,13 @@
|
||||
import os
|
||||
from fastapi.security import OAuth2PasswordBearer
|
||||
|
||||
"""
|
||||
系统版本
|
||||
"""
|
||||
VERSION = "1.2.5"
|
||||
|
||||
"""安全警告: 不要在生产中打开调试运行!"""
|
||||
DEBUG = False
|
||||
DEBUG = True
|
||||
|
||||
"""是否开启演示功能:取消所有POST,DELETE,PUT操作权限"""
|
||||
DEMO = False
|
||||
|
@ -6,6 +6,10 @@
|
||||
# @IDE : PyCharm
|
||||
# @desc : 关联中间表
|
||||
|
||||
"""
|
||||
Table 操作博客:http://www.ttlsa.com/python/sqlalchemy-concise-guide/
|
||||
"""
|
||||
|
||||
from db.db_base import Model
|
||||
from sqlalchemy import Column, Table, Integer, ForeignKey, INT
|
||||
|
||||
|
@ -12,7 +12,6 @@
|
||||
from typing import List, Union
|
||||
from sqlalchemy import select, update
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from utils.file_manage import FileManage
|
||||
from . import models, schemas
|
||||
from core.crud import DalBase
|
||||
|
File diff suppressed because one or more lines are too long
@ -19,9 +19,18 @@ from starlette.staticfiles import StaticFiles # 依赖安装:pip install aiof
|
||||
import importlib
|
||||
from core.logger import logger
|
||||
from core.exception import register_exception
|
||||
import typer
|
||||
from scripts.initialize.initialize import InitializeData
|
||||
import asyncio
|
||||
|
||||
|
||||
shell_app = typer.Typer()
|
||||
|
||||
|
||||
def create_app():
|
||||
"""
|
||||
其他配置:
|
||||
启动项目
|
||||
|
||||
docs_url:配置交互文档的路由地址,如果禁用则为None,默认为 /docs
|
||||
redoc_url: 配置 Redoc 文档的路由地址,如果禁用则为None,默认为 /redoc
|
||||
openapi_url:配置接口文件json数据文件路由地址,如果禁用则为None,默认为/openapi.json
|
||||
@ -32,7 +41,6 @@ app = FastAPI(
|
||||
version="1.0.0"
|
||||
)
|
||||
|
||||
|
||||
def import_module(modules: list, desc: str):
|
||||
for module in modules:
|
||||
if not module:
|
||||
@ -46,25 +54,11 @@ def import_module(modules: list, desc: str):
|
||||
except AttributeError:
|
||||
logger.error(f"ModuleNotFoundError:导入{desc}失败,未找到该模块下的方法:{module}")
|
||||
|
||||
|
||||
"""
|
||||
添加中间件
|
||||
"""
|
||||
import_module(settings.MIDDLEWARES, "中间件")
|
||||
|
||||
"""
|
||||
添加全局事件
|
||||
"""
|
||||
import_module(settings.EVENTS, "全局事件")
|
||||
|
||||
"""
|
||||
全局异常捕捉处理
|
||||
"""
|
||||
# 全局异常捕捉处理
|
||||
register_exception(app)
|
||||
|
||||
"""
|
||||
跨域解决
|
||||
"""
|
||||
# 跨域解决
|
||||
if settings.CORS_ORIGIN_ENABLE:
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
@ -72,26 +66,43 @@ if settings.CORS_ORIGIN_ENABLE:
|
||||
allow_credentials=settings.ALLOW_CREDENTIALS,
|
||||
allow_methods=settings.ALLOW_METHODS,
|
||||
allow_headers=settings.ALLOW_HEADERS)
|
||||
|
||||
"""
|
||||
挂在静态目录
|
||||
"""
|
||||
# 挂在静态目录
|
||||
if settings.STATIC_ENABLE:
|
||||
app.mount(settings.STATIC_URL, app=StaticFiles(directory=settings.STATIC_ROOT))
|
||||
if settings.TEMP_ENABLE:
|
||||
app.mount(settings.TEMP_URL, app=StaticFiles(directory=settings.TEMP_DIR))
|
||||
|
||||
"""
|
||||
引入应用中的路由
|
||||
"""
|
||||
# 引入应用中的路由
|
||||
for url in urls.urlpatterns:
|
||||
app.include_router(url["ApiRouter"], prefix=url["prefix"], tags=url["tags"])
|
||||
return app
|
||||
|
||||
|
||||
@shell_app.command()
|
||||
def run():
|
||||
"""
|
||||
启动项目
|
||||
"""
|
||||
uvicorn.run(app='main:create_app', host="0.0.0.0", port=9000)
|
||||
|
||||
|
||||
@shell_app.command()
|
||||
def init():
|
||||
"""
|
||||
初始化数据
|
||||
"""
|
||||
print("开始初始化数据")
|
||||
data = InitializeData()
|
||||
asyncio.run(data.run())
|
||||
|
||||
|
||||
@shell_app.command()
|
||||
def migrate():
|
||||
"""
|
||||
将模型迁移到数据库,更新数据库表结构
|
||||
"""
|
||||
print("开始更新数据库表")
|
||||
InitializeData().migrate_model()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
"""
|
||||
# 启动项目
|
||||
# reload:自动重载项目
|
||||
# debug:调试
|
||||
# workers:启动几个进程
|
||||
"""
|
||||
uvicorn.run(app='main:app', host="0.0.0.0", port=9000)
|
||||
shell_app()
|
||||
|
@ -1,82 +1,83 @@
|
||||
aiohttp==3.8.3
|
||||
aioredis==2.0.1
|
||||
aiosignal==1.2.0
|
||||
alembic==1.7.5
|
||||
alibabacloud-credentials==0.3.0
|
||||
alibabacloud-dysmsapi20170525==2.0.22
|
||||
alibabacloud-endpoint-util==0.0.3
|
||||
alibabacloud-gateway-spi==0.0.1
|
||||
alibabacloud-openapi-util==0.1.6
|
||||
alibabacloud-tea==0.3.0
|
||||
alibabacloud-tea-openapi==0.3.5
|
||||
alibabacloud-tea-util==0.3.7
|
||||
alibabacloud-tea-xml==0.0.2
|
||||
aliyun-python-sdk-core==2.13.36
|
||||
aliyun-python-sdk-kms==2.16.0
|
||||
anyio==3.5.0
|
||||
asgiref==3.5.0
|
||||
async-timeout==4.0.2
|
||||
asyncmy==0.2.5
|
||||
attrs==22.1.0
|
||||
bcrypt==3.2.2
|
||||
certifi==2022.6.15
|
||||
cffi==1.15.1
|
||||
charset-normalizer==2.0.12
|
||||
click==8.0.3
|
||||
colorama==0.4.4
|
||||
crcmod==1.7
|
||||
cryptography==37.0.4
|
||||
dnspython==2.2.1
|
||||
ecdsa==0.18.0
|
||||
et-xmlfile==1.1.0
|
||||
fastapi==0.85.1
|
||||
frozenlist==1.3.1
|
||||
greenlet==2.0.0
|
||||
gunicorn==20.1.0
|
||||
h11==0.13.0
|
||||
httptools==0.3.0
|
||||
idna==3.3
|
||||
importlib-metadata==4.10.1
|
||||
importlib-resources==5.4.0
|
||||
jmespath==0.10.0
|
||||
loguru==0.5.3
|
||||
Mako==1.1.6
|
||||
MarkupSafe==2.0.1
|
||||
motor==3.1.1
|
||||
multidict==6.0.2
|
||||
openpyxl==3.0.10
|
||||
orjson==3.6.6
|
||||
oss2==2.16.0
|
||||
passlib==1.7.4
|
||||
Pillow==9.2.0
|
||||
pyasn1==0.4.8
|
||||
pycparser==2.21
|
||||
pycryptodome==3.15.0
|
||||
pydantic==1.9.0
|
||||
pymongo==4.3.2
|
||||
PyMySQL==1.0.2
|
||||
python-dotenv==0.19.2
|
||||
python-jose==3.3.0
|
||||
python-multipart==0.0.5
|
||||
PyYAML==6.0
|
||||
requests==2.28.0
|
||||
rsa==4.9
|
||||
six==1.16.0
|
||||
sniffio==1.2.0
|
||||
SQLAlchemy==1.4.31
|
||||
SQLAlchemy-Utils==0.38.3
|
||||
SSIM-PIL==1.0.14
|
||||
starlette==0.20.4
|
||||
typing-extensions==4.0.1
|
||||
ua-parser==0.15.0
|
||||
urllib3==1.26.9
|
||||
user-agents==2.2.0
|
||||
uvicorn==0.17.0.post1
|
||||
watchgod==0.7
|
||||
websockets==10.1
|
||||
win32-setctime==1.1.0
|
||||
XlsxWriter==3.0.3
|
||||
yarl==1.8.1
|
||||
zipp==3.7.0
|
||||
zope.event==4.5.0
|
||||
zope.interface==5.5.0
|
||||
aiohttp
|
||||
aioredis
|
||||
aiosignal
|
||||
alembic
|
||||
alibabacloud-credentials
|
||||
alibabacloud-dysmsapi20170525
|
||||
alibabacloud-endpoint-util
|
||||
alibabacloud-gateway-spi
|
||||
alibabacloud-openapi-util
|
||||
alibabacloud-tea
|
||||
alibabacloud-tea-openapi
|
||||
alibabacloud-tea-util
|
||||
alibabacloud-tea-xml
|
||||
aliyun-python-sdk-core
|
||||
aliyun-python-sdk-kms
|
||||
anyio
|
||||
asgiref
|
||||
async-timeout
|
||||
asyncmy
|
||||
attrs
|
||||
bcrypt
|
||||
certifi
|
||||
cffi
|
||||
charset-normalizer
|
||||
click
|
||||
colorama
|
||||
crcmod
|
||||
cryptography
|
||||
dnspython
|
||||
ecdsa
|
||||
et-xmlfile
|
||||
fastapi
|
||||
frozenlist
|
||||
greenlet
|
||||
gunicorn
|
||||
h11
|
||||
httptools
|
||||
idna
|
||||
importlib-metadata
|
||||
importlib-resources
|
||||
jmespath
|
||||
loguru
|
||||
Mako
|
||||
MarkupSafe
|
||||
motor
|
||||
multidict
|
||||
openpyxl
|
||||
orjson
|
||||
oss2
|
||||
passlib
|
||||
Pillow
|
||||
pyasn1
|
||||
pycparser
|
||||
pycryptodome
|
||||
pydantic
|
||||
pymongo
|
||||
PyMySQL
|
||||
python-dotenv
|
||||
python-jose
|
||||
python-multipart
|
||||
PyYAML
|
||||
requests
|
||||
rsa
|
||||
six
|
||||
sniffio
|
||||
SQLAlchemy
|
||||
SQLAlchemy-Utils
|
||||
SSIM-PIL
|
||||
starlette
|
||||
typing-extensions
|
||||
ua-parser
|
||||
urllib3
|
||||
user-agents
|
||||
uvicorn
|
||||
watchgod
|
||||
websockets
|
||||
win32-setctime
|
||||
XlsxWriter
|
||||
yarl
|
||||
zipp
|
||||
zope.event
|
||||
zope.interface
|
||||
typer
|
0
kinit-api/scripts/__init__.py
Normal file
0
kinit-api/scripts/__init__.py
Normal file
7
kinit-api/scripts/initialize/__init__.py
Normal file
7
kinit-api/scripts/initialize/__init__.py
Normal file
@ -0,0 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# @version : 1.0
|
||||
# @Creaet Time : 2021/10/19 15:47
|
||||
# @File : initialize.py
|
||||
# @IDE : PyCharm
|
||||
# @desc : 初始化数据
|
||||
|
BIN
kinit-api/scripts/initialize/data/init.xlsx
Normal file
BIN
kinit-api/scripts/initialize/data/init.xlsx
Normal file
Binary file not shown.
148
kinit-api/scripts/initialize/initialize.py
Normal file
148
kinit-api/scripts/initialize/initialize.py
Normal file
@ -0,0 +1,148 @@
|
||||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
# @version : 1.0
|
||||
# @Creaet Time : 2022/11/23 11:21
|
||||
# @File : initialize.py
|
||||
# @IDE : PyCharm
|
||||
# @desc : 简要说明
|
||||
from core.database import db_getter
|
||||
from utils.excel.excel_manage import ExcelManage
|
||||
from application.settings import BASE_DIR, VERSION
|
||||
import os
|
||||
from apps.vadmin.auth import models as auth_models
|
||||
from apps.vadmin.system import models as system_models
|
||||
from sqlalchemy.sql.schema import Table
|
||||
import subprocess
|
||||
|
||||
|
||||
class InitializeData:
|
||||
"""
|
||||
初始化数据
|
||||
|
||||
生成步骤:
|
||||
1. 读取数据
|
||||
2. 获取数据库
|
||||
3. 创建数据
|
||||
"""
|
||||
|
||||
SCRIPT_DIR = os.path.join(BASE_DIR, 'scripts', 'initialize')
|
||||
|
||||
def __init__(self):
|
||||
self.sheet_names = []
|
||||
self.datas = {}
|
||||
self.ex = None
|
||||
self.db = None
|
||||
self.__serializer_data()
|
||||
self.__get_sheet_data()
|
||||
|
||||
@classmethod
|
||||
def migrate_model(cls):
|
||||
"""
|
||||
模型迁移映射到数据库
|
||||
"""
|
||||
subprocess.check_call(f'alembic revision --autogenerate -m "{VERSION}"', cwd=BASE_DIR)
|
||||
subprocess.check_call('alembic upgrade head', cwd=BASE_DIR)
|
||||
print(f"{VERSION} 数据库表迁移完成")
|
||||
|
||||
def __serializer_data(self):
|
||||
"""
|
||||
序列化数据,将excel数据转为python对象
|
||||
"""
|
||||
self.ex = ExcelManage()
|
||||
self.ex.open_workbook(os.path.join(self.SCRIPT_DIR, 'data', 'init.xlsx'), read_only=True)
|
||||
self.sheet_names = self.ex.get_sheets()
|
||||
|
||||
def __get_sheet_data(self):
|
||||
"""
|
||||
获取工作区数据
|
||||
"""
|
||||
for sheet in self.sheet_names:
|
||||
sheet_data = []
|
||||
self.ex.open_sheet(sheet)
|
||||
headers = self.ex.get_header()
|
||||
datas = self.ex.readlines(min_row=2, max_col=len(headers))
|
||||
for row in datas:
|
||||
sheet_data.append(dict(zip(headers, row)))
|
||||
self.datas[sheet] = sheet_data
|
||||
|
||||
async def __generate_data(self, table_name: str, model):
|
||||
"""
|
||||
生成数据
|
||||
|
||||
@params table_name: 表名
|
||||
@params model: 数据表模型
|
||||
"""
|
||||
async_session = db_getter()
|
||||
db = await async_session.__anext__()
|
||||
if isinstance(model, Table):
|
||||
for data in self.datas.get(table_name):
|
||||
await db.execute(model.insert().values(**data))
|
||||
else:
|
||||
for data in self.datas.get(table_name):
|
||||
db.add(model(**data))
|
||||
print(f"{table_name} 表数据已生成")
|
||||
await db.flush()
|
||||
await db.commit()
|
||||
|
||||
async def generate_menu(self):
|
||||
"""
|
||||
生成菜单数据
|
||||
"""
|
||||
await self.__generate_data("vadmin_auth_menu", auth_models.VadminMenu)
|
||||
|
||||
async def generate_role(self):
|
||||
"""
|
||||
生成角色
|
||||
"""
|
||||
await self.__generate_data("vadmin_auth_role", auth_models.VadminRole)
|
||||
|
||||
async def generate_user(self):
|
||||
"""
|
||||
生成用户
|
||||
"""
|
||||
await self.__generate_data("vadmin_auth_user", auth_models.VadminUser)
|
||||
|
||||
async def generate_user_role(self):
|
||||
"""
|
||||
生成用户
|
||||
"""
|
||||
await self.__generate_data("vadmin_auth_user_roles", auth_models.vadmin_user_roles)
|
||||
|
||||
async def generate_system_tab(self):
|
||||
"""
|
||||
生成系统配置分类数据
|
||||
"""
|
||||
await self.__generate_data("vadmin_system_settings_tab", system_models.VadminSystemSettingsTab)
|
||||
|
||||
async def generate_system_config(self):
|
||||
"""
|
||||
生成系统配置数据
|
||||
"""
|
||||
await self.__generate_data("vadmin_system_settings", system_models.VadminSystemSettings)
|
||||
|
||||
async def generate_dict_type(self):
|
||||
"""
|
||||
生成字典类型数据
|
||||
"""
|
||||
await self.__generate_data("vadmin_system_dict_type", system_models.VadminDictType)
|
||||
|
||||
async def generate_dict_details(self):
|
||||
"""
|
||||
生成字典详情数据
|
||||
"""
|
||||
await self.__generate_data("vadmin_system_dict_details", system_models.VadminDictDetails)
|
||||
|
||||
async def run(self):
|
||||
"""
|
||||
执行初始化工作
|
||||
"""
|
||||
self.migrate_model()
|
||||
await self.generate_menu()
|
||||
await self.generate_role()
|
||||
await self.generate_user()
|
||||
await self.generate_user_role()
|
||||
await self.generate_system_tab()
|
||||
await self.generate_dict_type()
|
||||
await self.generate_system_config()
|
||||
await self.generate_dict_details()
|
||||
print(f"{VERSION} 数据已初始化完成")
|
@ -29,21 +29,38 @@ class ExcelManage:
|
||||
self.sheet = None
|
||||
self.wb = None
|
||||
|
||||
def open_sheet(self, file: str, sheet_name: str = None, read_only: bool = False, data_only: bool = False) -> None:
|
||||
def open_workbook(self, file: str, read_only: bool = False, data_only: bool = False) -> None:
|
||||
"""
|
||||
初始化 excel 文件
|
||||
|
||||
@param file: 文件名称或者对象
|
||||
@param sheet_name: 表单名称,为空则默认第一个
|
||||
@param read_only: 是否只读,优化读取速度
|
||||
@param data_only: 是否加载文件对象
|
||||
"""
|
||||
# 加载excel文件,获取表单
|
||||
self.wb = load_workbook(file, read_only=read_only, data_only=data_only)
|
||||
|
||||
def open_sheet(self, sheet_name: str = None, **kwargs) -> None:
|
||||
"""
|
||||
初始化 excel 文件
|
||||
|
||||
@param sheet_name: 表单名称,为空则默认第一个
|
||||
"""
|
||||
# 加载excel文件,获取表单
|
||||
if not self.wb:
|
||||
self.open_workbook(kwargs.get("file"), kwargs.get("read_only", False), kwargs.get("data_only", False))
|
||||
if sheet_name:
|
||||
self.sheet = self.wb[sheet_name]
|
||||
else:
|
||||
self.sheet = self.wb.active
|
||||
|
||||
def get_sheets(self) -> list:
|
||||
"""
|
||||
读取所有工作区名称
|
||||
@return: 一维数组
|
||||
"""
|
||||
return self.wb.sheetnames
|
||||
|
||||
def create_excel(self, sheet_name: str = None) -> None:
|
||||
"""
|
||||
创建 excel 文件
|
||||
@ -72,7 +89,7 @@ class ExcelManage:
|
||||
result.append(_row)
|
||||
return result
|
||||
|
||||
def get_header(self, row: int = 1, col: int = 1, asterisk: bool = False) -> list:
|
||||
def get_header(self, row: int = 1, col: int = None, asterisk: bool = False) -> list:
|
||||
"""
|
||||
读取指定表单的表头(第一行数据)
|
||||
|
||||
|
313
项目部署.md
313
项目部署.md
@ -1,313 +0,0 @@
|
||||
# 项目部署
|
||||
|
||||
## 初始化工作
|
||||
|
||||
### 部署目录
|
||||
|
||||
根目录:/opt
|
||||
|
||||
项目目录:/opt/project
|
||||
|
||||
项目启动日志目录:/opt/logs
|
||||
|
||||
## 生产环境要求
|
||||
|
||||
服务器系统版本:Centos 7.2以上
|
||||
|
||||
| 软件名称 | 版本 |
|
||||
| ---------- | ------------ |
|
||||
| Python | >= 3.8 |
|
||||
| Mysql | >= 8.0 |
|
||||
| Nginx | >= 1.16 |
|
||||
| Git | 最新,非必须 |
|
||||
| Supervisor | >= 3.4.0 |
|
||||
|
||||
## 项目配置
|
||||
|
||||
### 接口项目配置
|
||||
|
||||
**项目主要配置信息:**
|
||||
|
||||
文件路径:kinit-api/application/settings.py
|
||||
|
||||
```python
|
||||
# 关闭代码调试
|
||||
DEBUG = False
|
||||
```
|
||||
|
||||
**导出项目接口依赖包:**
|
||||
|
||||
kinit-api 根目录下执行命令:
|
||||
|
||||
```
|
||||
pip freeze > requirements.txt
|
||||
```
|
||||
|
||||
### 前端项目打包
|
||||
|
||||
kinit-admin 打包项目根目录下执行命令:
|
||||
|
||||
```
|
||||
pnpm run build:pro
|
||||
```
|
||||
|
||||
### 软件安装配置
|
||||
|
||||
#### Mysql
|
||||
|
||||
安装配置请查看链接:https://www.cnblogs.com/yanglang/p/10782941.html
|
||||
|
||||
```
|
||||
scp mysql-8.0.27-1.el7.x86_64.rpm-bundle.tar root@127.0.0.1:/usr/local/pag
|
||||
|
||||
// 安装命令:
|
||||
tar -xvf mysql-8.0.27-1.el7.x86_64.rpm-bundle.tar
|
||||
rpm -ivh mysql-community-common-8.0.27-1.el7.x86_64.rpm --nodeps --force
|
||||
rpm -ivh mysql-community-libs-8.0.27-1.el7.x86_64.rpm --nodeps --force
|
||||
rpm -ivh mysql-community-client-8.0.27-1.el7.x86_64.rpm --nodeps --force
|
||||
rpm -ivh mysql-community-server-8.0.27-1.el7.x86_64.rpm --nodeps --force
|
||||
rpm -qa | grep mysql
|
||||
|
||||
rpm -e mysql-community-common --nodeps
|
||||
rpm -e mysql-community-libs --nodeps
|
||||
rpm -e mysql-community-client --nodeps
|
||||
rpm -e mysql-community-server --nodeps
|
||||
|
||||
mysqld --initialize
|
||||
chown mysql:mysql /var/lib/mysql -R
|
||||
systemctl start mysqld.service
|
||||
systemctl enable mysqld
|
||||
cat /var/log/mysqld.log | grep password
|
||||
mysql -uroot -p
|
||||
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'b0Nrzh97G9lJkQMK';
|
||||
create user 'root'@'%' identified with mysql_native_password by 'b0Nrzh97G9lJkQMK';
|
||||
grant all privileges on *.* to 'root'@'%' with grant option;
|
||||
flush privileges;
|
||||
```
|
||||
|
||||
安装配置完成后建议使用Navicat Premium 作为 Mysql 的客户端
|
||||
|
||||
1. 首先需要创建 kinit 系统数据库
|
||||
|
||||
2. 然后将 kinit-api\static\kinit.sql 文件导入
|
||||
|
||||
#### Python
|
||||
|
||||
```shell
|
||||
# 下载python安装包
|
||||
wget https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tgz
|
||||
# 创建安装目录
|
||||
mkdir /usr/local/python3
|
||||
# 解压安装包
|
||||
tar -zxvf Python-3.8.2.tgz
|
||||
# 进入解压目录
|
||||
cd Python-3.8.2
|
||||
# 配置
|
||||
./configure --prefix=/usr/local/python3
|
||||
#编译
|
||||
make && make install
|
||||
#创建软链接
|
||||
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
|
||||
ln -s /usr/local/python3/bin/python3 /usr/local/bin/python3
|
||||
#pip创建软链接
|
||||
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
|
||||
# 更新pip
|
||||
pip3 install --upgrade pip
|
||||
```
|
||||
|
||||
#### Git
|
||||
|
||||
安装:
|
||||
|
||||
```
|
||||
yum -y install git
|
||||
```
|
||||
|
||||
获取项目文件:
|
||||
|
||||
```
|
||||
mkdir -p /opt/project/
|
||||
git clone -b release 项目Git仓库
|
||||
```
|
||||
|
||||
或者也可以通过压缩包上传的方式上传到服务器,推荐使用 Git 方式
|
||||
|
||||
#### Python 虚拟环境
|
||||
|
||||
创建项目所使用的 python 虚拟环境:
|
||||
|
||||
```shell
|
||||
# 安装 virtualenv
|
||||
pip3 install virtualenv
|
||||
|
||||
# 找到虚拟环境的安装目录
|
||||
find / -name virtualenv
|
||||
# 创建软链接
|
||||
ln -s /usr/local/python3/bin/virtualenv /usr/bin/
|
||||
|
||||
# 进入虚拟环境目录
|
||||
mkdir -p /opt/env
|
||||
cd /opt/env
|
||||
|
||||
# 新建虚拟环境,会在当前目录生成一个文件夹
|
||||
virtualenv kinit
|
||||
|
||||
# 进入虚拟环境(命令行前面会出现虚拟环境名称)
|
||||
cd kinit
|
||||
source bin/activate
|
||||
|
||||
# 退出虚拟环境命令(之后的设置都是在虚拟环境中设置,这里不需要退出)
|
||||
deactivate
|
||||
```
|
||||
|
||||
安装接口项目依赖包:
|
||||
|
||||
```shell
|
||||
# 在虚拟环境中执行
|
||||
# 进入到 saas-api 根目录下执行
|
||||
pip3 install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
|
||||
```
|
||||
|
||||
#### Supervisor
|
||||
|
||||
官方文档:http://supervisord.org/
|
||||
|
||||
安装配置请查看:https://www.jianshu.com/p/0b9054b33db3
|
||||
|
||||
创建项目接口 supervisor 配置文件:
|
||||
|
||||
```shell
|
||||
vim /etc/supervisord.d/kinit.api.ini
|
||||
```
|
||||
|
||||
内容如下:
|
||||
|
||||
```ini
|
||||
[program:kinit.api]
|
||||
directory=/opt/project/kinit/kinit-api/
|
||||
command=/opt/env/kinit/bin/gunicorn main:app -w 2 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:9000
|
||||
startsecs=10
|
||||
stopwaitsecs=10
|
||||
autostart=true
|
||||
autorestart=true
|
||||
redirect_stderr=true
|
||||
stdout_logfile_maxbytes=10MB
|
||||
stdout_logfile_backups = 3
|
||||
stdout_logfile=/opt/logs/kinit.api.log
|
||||
```
|
||||
|
||||
更新 supervisor 配置:
|
||||
|
||||
```
|
||||
supervisorctl update
|
||||
```
|
||||
|
||||
更新后会自动启动进程
|
||||
|
||||
可以通过命令查看:
|
||||
|
||||
```
|
||||
supervisorctl status
|
||||
```
|
||||
|
||||
#### Nginx
|
||||
|
||||
安装:
|
||||
|
||||
```
|
||||
yum -y install nginx
|
||||
```
|
||||
|
||||
增加配置文件
|
||||
|
||||
```
|
||||
vim /etc/nginx/conf.d/kinit.conf
|
||||
```
|
||||
|
||||
内容如下:
|
||||
|
||||
```nginx
|
||||
# 如果需要使用搭配小程序使用,则需要配置api的访问域名,并配置域名证书
|
||||
server
|
||||
{
|
||||
listen 80;
|
||||
listen 443 ssl http2;
|
||||
server_name 127.0.0.1;
|
||||
root /opt/project/kinit/kinit-api/;
|
||||
|
||||
keepalive_timeout 180s;
|
||||
client_max_body_size 20m;
|
||||
|
||||
if ($server_port !~ 443){
|
||||
rewrite ^(/.*)$ https://$host$1 permanent;
|
||||
}
|
||||
#HTTP_TO_HTTPS_END
|
||||
ssl_certificate /etc/nginx/conf.d/cert/kinit/kinit.pem;
|
||||
ssl_certificate_key /etc/nginx/conf.d/cert/kinit/kinit.key;
|
||||
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
|
||||
ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
|
||||
ssl_prefer_server_ciphers on;
|
||||
ssl_session_cache shared:SSL:10m;
|
||||
ssl_session_timeout 10m;
|
||||
add_header Strict-Transport-Security "max-age=31536000";
|
||||
error_page 497 https://$host$request_uri;
|
||||
|
||||
location ~ \.well-known{
|
||||
allow all;
|
||||
}
|
||||
location /api/ {
|
||||
rewrite ^/api/(.*) /$1 break;
|
||||
}
|
||||
|
||||
location / {
|
||||
proxy_pass http://127.0.0.1:9000;
|
||||
}
|
||||
access_log /opt/logs/kinit.api.nginx.log;
|
||||
error_log /opt/logs/kinit.api.nginx.error.log;
|
||||
}
|
||||
server
|
||||
{
|
||||
listen 80;
|
||||
server_name 127.0.0.1;
|
||||
root /opt/project/kinit/kinit-admin/dist-pro/;
|
||||
|
||||
keepalive_timeout 180s;
|
||||
client_max_body_size 20m;
|
||||
|
||||
location ~ \.well-known{
|
||||
allow all;
|
||||
}
|
||||
location / {
|
||||
try_files $uri $uri/ @router;
|
||||
root /opt/project/kinit/kinit-admin/dist-pro/;
|
||||
index index.html;
|
||||
}
|
||||
location /api/ {
|
||||
proxy_pass http://127.0.0.1:9000/;
|
||||
client_max_body_size 20m;
|
||||
}
|
||||
|
||||
location @router {
|
||||
rewrite ^.*$ /index.html break;
|
||||
}
|
||||
|
||||
access_log /opt/logs/kinit.nginx.log;
|
||||
error_log /opt/logs/kinit.nginx.error.log;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
配置完成后启动:
|
||||
|
||||
```
|
||||
systemctl start nginx
|
||||
# 开启自启动
|
||||
systemctl enable nginx
|
||||
```
|
||||
|
||||
查看当前状态:
|
||||
|
||||
```
|
||||
systemctl status nginx
|
||||
```
|
||||
|
Loading…
x
Reference in New Issue
Block a user