swag-ui与swag-edit

swag-ui安装部署

1.安装nodejs

 yum install -y nodejs
 node -v

2.配置淘宝镜像

npm config set registry http://registry.npm.taobao.org
npm config get registry
npm install -g cnpm --registry=https://registry.npm.taobao.org
// 指导链接
https://zhengdao.github.io/2019/05/10/how-to-install-and-config-node-js/

3.安装express

npm install express --save   

4.克隆swagger-ui代码

git clone https://github.com/swagger-api/swagger-ui

5.新建node/public文件夹,并拷贝dist文件

mkdir public
//1. 将下载到的swagger ui里的dist文件夹里的文件复制到public文件夹里
cp swagger/dist/* /public -r

// 2.修改public中index.html中的url字段值
// url: "https://petstore.swagger.io/v2/swagger.json", 修改为下面:
// url: "swagger.json",  //这里的swagger.json是自己编写的json,路径public中,这里也可以是自己远端主机的文件

6.在node中创建index.js文件

  1 var express = require('express');
  2 var app = express();
  3 
  4 app.use('/swagger',express.static('public'));
  5 
  6 app.get('/', function (req, res) {
  7    res.send('Hello World');
  8 })
  9 
 10 var server = app.listen(3000, function () {
 11 
 12   var host = server.address().address
 13   var port = server.address().port
 14 
 15   console.log("swagger start", host, port)
 16 
 17 })

7.运行

node index.js  //默认监听端口3000

nohup node index.js & >> /dev/null //脚本启动
lsof -i:3000 | awk -F " " '{print $2;}' | sed -n 2p | xargs kill -9 //停止
lsof -i:3000 |awk 'NR==2 {print $2}' |xargs kill // 停止

//访问页面
http://localhost:3000

8.docker的部署方式

docker pull swaggerapi/swagger-ui
docker run -p 80:8080 swaggerapi/swagger-ui
// 自定义路径
docker run -p 80:8080 -e SWAGGER_JSON=/foo/swagger.json -v /bar:/foo swaggerapi/swagger-ui
docker run -p 80:8080 -e BASE_URL=/swagger -e SWAGGER_JSON=/foo/swagger.json -v /bar:/foo swaggerapi/swagger-ui

9.swagger-editor安装

npm install -g http-server
wget https://github.com/swagger-api/swagger-editor/releases/download/v2.10.4/swagger-editor.zip
unzip swagger-editor.zip
http-server swagger-editor

10.简单的启动脚本

  1 #!/bin/bash
  2 echo "usage:"
  3 echo "./swag-edit.h start  # 启动swag-editor"
  4 echo "./swag-edit.sh stop   # 停止服务"
  5 echo ""
  6 
  7 param="$1"
  8 case "$param" in
  9   "start") nohup http-server swagger-editor -p 3003 & >> /dev/null
 10   if [ "$?" -eq 0 ];then
 11           echo "start success"
 12   else
 13           echo "start failed"
 14   fi
 15   ;;
 16   "stop") lsof -i:3003 | awk -F " " '{print $2;}' | sed -n 2p | xargs kill -9
 17    if [ "$?" -eq 0 ];then
 18         echo "stop success"
 19    else
 20         echo "stop failed"
 21    fi
 22   ;;
 23 esac

https://cloud.tencent.com/developer/article/1384294
https://blog.csdn.net/luanpeng825485697/article/details/82819977
https://www.cnblogs.com/libw/p/write-swagger.html

原文地址:https://www.cnblogs.com/tomtellyou/p/12892989.html