随着API数量越来越多,wiki已经体现不出优势了。冗长的文档资料让人难以维护,稍微一点小改动就需要对很多地方进行修改。最近一位前端大佬推荐了API集成管理平台YApi,上手后发现还挺不错的。
YAPI是去哪儿网团队YMFE开发的一个开源项目,用于API开发,帮助开发者轻松创建、发布、维护API,协议Apache 2.0,非常良心。官方的宣传:YAPI——高效、易用、功能强大的API管理平台,旨在为开发、产品、测试人员提供更优雅的接口管理服务。
安装nodeJS和npm
# brew install node
# node -v
# npm -v
// 检查node环境
# npm doctor
根据YApi文档进行安装,官方推荐图形界面方式:
# npm install -g yapi-cli --registry https://registry.npm.taobao.org
# yapi server
这里,我们选择命令行方式去安装:
# mkdir yapi
# cd yapi
# git clone https://github.com/YMFE/yapi.git vendors // 或者下载 zip 包解压到 vendors 目录
# cp vendors/config_example.json ./config.json // 复制完成后请修改相关配置
修改config.json文件,其中mongo集合yapi和该collection用户密码,需要提前建立与配置。
{
"port": "9233",
"adminAccount": "admin@admin.com",
"closeRegister": true,
"db": {
"servername": "127.0.0.1",
"DATABASE": "yapi",
"port": 27017,
"user": "yapi",
"pass": "yapi123*",
"authSource": ""
},
"mail": {
"enable": false,
"host": "smtp.163.com",
"port": 465,
"from": "***@163.com",
"auth": {
"user": "***@163.com",
"pass": "*****"
}
},
"ldapLogin": {
"enable": true,
"server": "ldap://localhost:389",
"baseDn": "cn=Manager,dc=bewindoweb,dc=com",
"bindPassword": "123456",
"searchDn": "dc=bewindoweb,dc=com",
"searchStandard": "mail",
"emailPostfix": "@bewindoweb.com",
"emailKey": "mail",
"usernameKey": "description"
}
}
adminAccount:和mongodb无关,是yapi登录使用账号名,密码默认ymfe.org
closeRegister:禁止注册
user/pass:刚才mongodb设置的yapi的用户名和密码
mail:暂时关闭,设置为false
ldapLogin:增加ldap登录方式,利用前面《openLDAP原理、安装和使用》搭建的服务器来测试,我们把账号稍微设置得更完整:
baseDn、bindPassword:用于搜索用户账号的系统账号;
searchDn:搜索根目录;
searchStandard:搜索条件;
emailPostfix:邮箱后缀;
emailKey:邮箱在LDAP账号里面的属性名称;
usernameKey:用户显示名在LDAP账号里面的属性名称
接着,执行npm安装依赖,生成node_module目录。
# cd vendors
# npm install --production --registry https://registry.npm.taobao.org
注:这一步很容易报错(一个bug),如果报错,请尝试:npm install --production --registry https://registry.npm.taobao.org --unsafe-perm=true --allow-root
npm依赖安装完成后,执行yapi服务器安装:
# npm run install-server // 安装程序会初始化数据库索引和管理员账号,管理员账号名可在 config.json 配置
> yapi-vendor@1.11.0 install-server
> node server/install.js
(node:12955) Warning: Accessing non-existent property 'count' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)
(node:12955) Warning: Accessing non-existent property 'findOne' of module exports inside circular dependency
(node:12955) Warning: Accessing non-existent property 'remove' of module exports inside circular dependency
(node:12955) Warning: Accessing non-existent property 'updateOne' of module exports inside circular dependency
log: mongodb load success...
初始化管理员账号成功,账号名:"admin@admin.com",密码:"ymfe.org"
完成之后,启动yapi
# node server/app.js
log: -------------------------------------swaggerSyncUtils constructor-----------------------------------------------
log: 服务已启动,请打开下面链接访问:
http://127.0.0.1:3000/
(node:12993) Warning: Accessing non-existent property 'count' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)
(node:12993) Warning: Accessing non-existent property 'findOne' of module exports inside circular dependency
(node:12993) Warning: Accessing non-existent property 'remove' of module exports inside circular dependency
(node:12993) Warning: Accessing non-existent property 'updateOne' of module exports inside circular dependency
log: mongodb load success...
(node:12993) [DEP0066] DeprecationWarning: OutgoingMessage.prototype._headers is deprecated
(node:12993) DeprecationWarning: collection.update is deprecated. Use updateOne, updateMany, or bulkWrite instead.
(node:12993) [DEP0106] DeprecationWarning: crypto.createCipher is deprecated.
浏览器中打开http://127.0.0.1:3000,输入用户名密码(admin@admin.com,ymfe.org)即可。
https://jiuaidu.com/jianzhan/1139161/
https://www.ewbang.com/community/article/details/960115002.html
https://blog.csdn.net/qq23001186/article/details/125832921