keyfil/教程/20250103-YApi部署指南.md
liuxiaohua cd0b30796f
All checks were successful
Publish to Confluence / confluence (push) Successful in 3m39s
♻️ [2025-02-10] 梳理项目结构
2025-02-10 09:21:02 +08:00

260 lines
6.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!-- Space: qifu -->
<!-- Parent: 后端技术&知识&规范 -->
<!-- Parent: 技术方案 -->
<!-- Parent: 基建 -->
<!-- Parent: 03-接入指南 -->
<!-- Title: 20250103-YApi部署指南 -->
<!-- Macro: :anchor\((.*)\):
Template: ac:anchor
Anchor: ${1} -->
<!-- Macro: \!\[.*\]\((.+)\)\<\!\-\- width=(.*) \-\-\>
Template: ac:image
Url: ${1}
Width: ${2} -->
<!-- Macro: \<\!\-\- :toc: \-\-\>
Template: ac:toc
Printable: 'false'
MinLevel: 2
MaxLevel: 4 -->
<!-- Include: 杂项/声明文件.md -->
<!-- :toc: -->
# YApi 部署指南
## Docker 安装
### 项目准备
#### 拉取项目
```shell
mkdir yapi
cd yapi
git clone https://xxxx.xxxx.xx/yapi.git vendors
cp vendors/config_cp.json ./config.json
```
#### 准备配置文件 `yapi/config.json`
- [配置文件](#配置文件)
### 项目结构
```dockerfile
- yapi
--- vendors
----- package.json
----- config_cp.json
----- server
----- Dockerfile
--- config.json
```
### Dockerfile
```dockerfile
FROM node:12-alpine3.15
ENV TZ="Asia/Shanghai"
#-使用阿里云镜像
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
WORKDIR /yapi/vendors
COPY . /yapi/vendors/
RUN mv /yapi/vendors/config_cp.json /yapi/config.json
RUN npm config set registry https://registry.npmmirror.com
#-构建 yapi
RUN apk add --no-cache wget python2 make g++ && cd /yapi/vendors && npm set strict-ssl false && npm install --production --registry https://registry.npmmirror.com
#-插件安装
RUN npm install -g ykit --registry https://registry.npmmirror.com
RUN npm install yapi-plugin-kc-oidc --registry https://registry.npmmirror.com
RUN npm rebuild node-sass --registry https://registry.npmmirror.com
RUN npm run build-client
EXPOSE 3000
ENTRYPOINT ["node"]
```
### 构建镜像 `yapi/vendors`
```shell
docker build -t qifu-base-yapi:1.0.0 .
```
### 启动 YApi
#### 初始化数据库(可选,这边已有,不需要再执行)
- **由于之前已经有 YApi 了,所以这一步不需要**
- config.json 在 `yapi`
- 指令跟着实际情况改改
```shell
docker run -d --rm \
--name yapi-init \
--link mongodb:mongo \
--net=yapi \
-v $PWD/config.json:/yapi/config.json \
qifu-base-yapi:1.0.0 \
server/install.js
#-初始化管理员账号在上面的 config.json 配置中 hexiaohei1024@gmail.com初始密码是 ymfe.org可以登录后进入个人中心修改
```
### 启动 YApi
- 指令跟着实际情况改改
```shell
docker run -d \
--name yapi \
--link mongodb:mongo \
--restart always \
--net=yapi \
-p 3000:3000 \
-v $PWD/config.json:/yapi/config.json \
qifu-base-yapi:1.0.0 \
server/app.js
```
## 宿主机安装
### Nodejs 安装 `版本12`
- 下载 Nodejs[下载地址](https://nodejs.org/zh-cn/download)
- 解压
- 配置环境变量 `/etc/profile`
- 使环境变量生效 `source /etc/profile`
### Python 安装
- python 版本 2.7.18
- python 安装https://blog.csdn.net/lsqtzj/article/details/114298091
- pip 安装https://www.cnblogs.com/shenfeng/p/install_pip_offline.html
#### 可能遇到的问题
- 可能用得上https://blog.csdn.net/loovelj/article/details/90274059
### YAPI 安装启动
```shell
#-配置淘宝镜像
npm config set registry https://registry.npmmirror.com
#-安装windows构建工具, windows 才需要
##-npm install --g --production windows-build-tools
#-构建 yapi
mkdir yapi
#-进入文件夹
cd yapi
#-或者下载 zip 包解压到 vendors 目录clone 整个仓库大概 140+ M可以通过 `git clone --depth=1 https://github.com/YMFE/yapi.git vendors` 命令减少,大概 10+ M
git clone https://github.com/YMFE/yapi.git vendors
#-复制完成后请修改相关配置
cp vendors/config_example.json ./config.json
cd vendors
npm install --production --registry https://registry.npmmirror.com
#-npm install --production --unsafe-perm=true --allow-root --registry https://registry.npmmirror.com
#-安装程序会初始化数据库索引和管理员账号,管理员账号名可在 config.json 配置
npm run install-server
#-启动服务器后,请访问 127.0.0.1:{config.json配置的端口},初次运行会有个编译的过程,请耐心等候
node server/app.js
```
### YAPI OIDC 配置
可以参考https://www.npmjs.com/package/yapi-plugin-kc-oidc?activeTab=readme
- config.json 添加配置 `plugins` 节点
- [配置文件](#配置文件)
- 执行编译
```shell
#-2.0 进入工作目录
cd yapi/vendors
#-2.1 安装所有依赖包
##-安装过程中可能出现node-sass报错替换到新的版本就可解决比如我将"node-sass": "^4.9.0" 替换成了 "node-sass": "^4.14.0"
##-sed -i s/'"node-sass": "^4.9.0"'/'"node-sass": "^4.14.0"'/ package.json
npm install --registry https://registry.npmmirror.com
#-2.2 安装ykit工具
npm install -g ykit --registry https://registry.npmmirror.com
#-2.3 安装yapi-plugin-kc-oidc
npm install yapi-plugin-kc-oidc --registry https://registry.npmmirror.com
##-或者不用npm安装自己去git clone代码
##-cd node_modules && git clone --depth 1 https://gitee.com/joshu/yapi-plugin-kc-oidc.git
#-2.4 开始编译
##-cd yapi/vendors
##-linux
npm run build-client
##-windows
###-set NODE_ENV=production
###-ykit pack -m
```
## 配置文件
```json
{
"port": "3000",
"adminAccount": "admin@admin.com",
"timeout": 120000,
"db": {
"servername": "127.0.0.1",
"DATABASE": "yapi",
"port": 27017,
"user": "test",
"pass": "test"
},
"mail": {
"enable": true,
"host": "smtp.163.com",
"port": 465,
"from": "***@163.com",
"auth": {
"user": "***@163.com",
"pass": "*****"
}
},
"plugins": [
{
"name": "kc-oidc",
"options": {
"authUrl": "",
"tokenUrl": "",
"userUrl": "",
"callbackUrl": "${host}/api/user/login_by_token",
"clientId": "",
"clientSecret": "",
"scope": "openid email profile phone ",
"userKey": "preferred_username",
"emailPostfix": "@xxx.com"
}
}
]
}
```
- mongodb 没有开启鉴权则删除 `user``pass` 字段
## 参考
- [YAPI安装教程](https://blog.csdn.net/IT_ZRS/article/details/118642997)
- [踩坑可查](https://juejin.cn/post/7246596616694317114)