前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >开源API管理平台YAPI的安装和使用

开源API管理平台YAPI的安装和使用

原创
作者头像
码之有理
发布2024-11-16 10:53:05
发布2024-11-16 10:53:05
9280
举报

YApi 是高效、易用、功能强大的API管理平台,旨在为开发、产品、测试人员提供更优雅的接口管理服务。可以帮助开发者轻松创建、发布、维护 API,YApi 还为用户提供了优秀的交互体验,开发人员只需利用平台提供的接口数据写入工具以及简单的点击操作就可以实现接口的管理。

Github: https://github.com/YMFE/yapi

官方文档:https://hellosean1025.github.io/yapi/documents/index.html

安装YAPI

由于官方YAPI已经停止维护,直接安装会报错,可以使用另外一个维护的仓库:https://github.com/gozeon/yapii

安装的过程中需要安装npm和pm2工具作为应用管理,以及需要安装MongoDB作为API数据库,需要修改如下脚本中mongodb_xxx的数据库配置。

安装完成后,打开http://localhost:9090/即可打开管理端页面。后端可以通过systemctl 来管理服务的生命周期。

代码语言:bash
复制
#!/bin/bash
# 需要开放的端口
# 9090: API管理Web

if ! command -v npm &> /dev/null; then
  yum install -y npm
  npm install -g pm2
  curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
  source ~/.bashrc
  #  node和npm的版本对应关系:https://nodejs.org/zh-cn/about/previous-releases
  nvm install v14.21.3 # npm v6.14.18
  npm install -g node-gyp@v3.6.3
  sed -i 's/rU/r/g' /usr/local/lib/node_modules/npm/node_modules/node-gyp/gyp/pylib/gyp/input.py
  npm install fs-extra
fi

app_name="yapii"
app_entry="server/app.js"
install_dir=/opt/$app_name
log_file=$install_dir/log/yapi.log
mongodb_addr="xxx"
mongodb_db="yapi"
mongodb_username="yapi"
mongodb_password="xxx"

if [ ! -d "$install_dir" ]; then
  wget https://github.com/gozeon/yapii/archive/refs/heads/master.zip
  unzip master.zip -d $install_dir
  cd $install_dir
  mv yapii-master/* .
  rm -rf yapii-master
  npm install fs-extra
  npm install --production --prefix $install_dir
  cat > $install_dir/config.json<<EOF
{
  "port": "9090",
  "timeout":120000,
  "closeRegister":false,
  "db": {
    "servername": "$mongodb_addr",
    "DATABASE": "$mongodb_db",
    "user": "$mongodb_username",
    "pass": "$mongodb_password",
    "port": 27017
  },
  "plugins": []
}
EOF
  sed -i "s/app.listen(yapi.WEBCONFIG.port);/app.listen(yapi.WEBCONFIG.port, '0.0.0.0');/" $app_entry
#  pm2 start $install_dir/$app_entry --name $app_name
fi

# 创建 YAPI systemd 服务文件
cat > /etc/systemd/system/yapi.service <<EOF
[Unit]
Description=YAPI Server
Documentation=https://github.com/YMFE/yapi
Requires=network.target
After=network.target

[Service]
Type=forking
ExecStartPre=/bin/mkdir -p /var/run
ExecStart=/usr/local/bin/pm2 start $install_dir/$app_entry --name $app_name --log $log_file
ExecStartPost=/bin/bash -c 'echo \$MAINPID > /var/run/yapi.pid'
ExecReload=/usr/local/bin/pm2 reload $app_name
ExecStop=/usr/local/bin/pm2 stop $app_name
ExecStopPost=/bin/rm -f /var/run/yapi.pid
Restart=on-failure
RestartSec=5
Environment=NODE_ENV=production
Environment=HOME=/home/$(whoami)

[Install]
WantedBy=multi-user.target
EOF

cat > /etc/logrotate.d/yapi<<EOF
$log_file {
    daily
    rotate 30
    compress
    dateext
    dateformat -%Y-%m-%d
    missingok
    notifempty
    postrotate
        # 发送 HUP 信号以通知应用程序重新打开日志文件
        /bin/kill -HUP \$(cat /var/run/yapi.pid 2>/dev/null) 2>/dev/null || true
    endscript
}
EOF

# 重新加载 systemd 配置并启动服务
sudo systemctl daemon-reload
sudo systemctl enable yapi
sudo systemctl restart yapi
sudo systemctl status yapi

API调试

运行旧版的API需要安装Chrome浏览器插件,下载后解压拖入Chrome插件中心即可。

浏览器插件下载地址:https://github.com/YMFE/cross-request/archive/master.zip

运行新版的调试不需要插件,但是超时时间只有800ms,超过就报错,需要修改源码中的超时时间。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装YAPI
  • API调试
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档