开发模式是前后端分离,为了前端团队和测试团队可以及时的得到后端API的变更信息,需要开发及时的和两个团队进行沟通,恰好我们开发团队使用了swagger-core,该插件可以根据代码中的注释,自动的生产swagger-ui的web界面,这时候项目已经部署在stg/prd环境了,这时候通知前端和测试已经太晚了(无法实现前后端同时开发,测试用例同时编写的场景),所以我们采取了一个办法:在开发每次有API接口变更的时候,手动生成一份json配置文件,上传到gitlab上,通过gitlab的hooks机制,把项目的json文件传输到离线版的swagger-ui上,以供测试和前端进行查看。
gitlab hooks
post-receive
#!/bin/bash
Branch='develop'
Swagger='/opt/git-swagger/'
Url="http://root:123456@zp-prd-ops-13/bd-swagger/bd-swagger-json.git"
Clone_cmd="git clone --no-checkout -b $Branch $Url $Swagger"
export RSYNC_PASSWORD=swagger90
#while read oldrev newrev ref
#do
file=`git log $Branch -1 --name-only | grep json`
if [ $? -eq 0 ];then
file1=`echo $file | awk -F '.' '{print $1}'`
rm -fr /opt/git-swagger/*
git clone -b $Branch $Url $Swagger
rm -fr /opt/git-swagger/.git
rsync -azL $Swagger swagger@192.168.1.8::swagger-ui/ >> /dev/null
echo "swagger json file $file1 push success"
for i in 13611872863 13761194025
do
curl -d "phoneNumbers=$i&content=$file1 api doc update" 'http://192.168.1.9:8000/bd-notice-server/notice_server/send/smsv2' > /dev/null 2>&1
done
else
echo "swagger json file $file1 push fail"
fi
#done
swagger-ui
部署
nginx配置
server {
listen 192.168.1.6:80;
server_name swagger.bdeastmoney.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
access_log logs/swagger.bdeastmoney.com-access.log main;
error_log logs/swagger.bdeastmoney.com-error.log;
location /swagger-ui {
root /opt/app/applications;
index index.html index.htm;
}
location /swagger-editor {
root /opt/app/applications;
index index.html index.htm;
}
}
rsyncd
/etc/rsyncd.conf
# GLOBAL OPTIONS
motd file=/etc/motd
port=873
pid file=/var/run/rsyncd.pid
lock file = /var/lock/rsyncd
log file=/var/log/rsyncd
transfer logging = yes
log format = [op]:%o [ip]:%a [module]:%m [path]:%P [file]:%f [size]:%l
syslog facility=daemon
max connections=2000
# MODULE OPTIONS
[swagger-ui]
path = /opt/app/applications/swagger-ui/dist/json
list = yes
use chroot = no
uid = root
gid = root
read only = no
exclude =
include =
auth users = swagger
secrets file = /etc/rsyncd/rsyncd.secret
strict modes = no
测试