Harbor配置Swagger遠端REST API

灬勿忘丶心安發表於2018-06-28

通過Swagger檢視和測試Harbor的遠端介面

Harbor 原始碼地址:https://github.com/vmware/harbor

一、下載 Swagger 執行指令碼和檔案

從 Harbor 原始碼倉庫中(docs)下載兩個檔案:prepare-swagger.shswagger.yaml

文件路徑:https://github.com/vmware/harbor/tree/master/docs

Harbor配置Swagger遠端REST API

二、修改 prepare-swagger.sh 指令碼
#!/bin/bash
# 設定成和Harbor倉庫相同的訪問方式
SCHEME=https
# Harbor倉庫所在的地址
SERVER_IP=192.168.80.42
set -e
echo "Doing some clean up..."
rm -f *.tar.gz
echo "Downloading Swagger UI release package..."
wget https://github.com/swagger-api/swagger-ui/archive/v2.1.4.tar.gz -O swagger.tar.gz
echo "Untarring Swagger UI package to the static file path..."
mkdir -p ../src/ui/static/vendors
tar -C ../src/ui/static/vendors -zxf swagger.tar.gz swagger-ui-2.1.4/dist
echo "Executing some processes..."
sed -i.bak 's/http:\/\/petstore\.swagger\.io\/v2\/swagger\.json/'$SCHEME':\/\/'$SERVER_IP'\/static\/resources\/yaml\/swagger\.yaml/g' \
../src/ui/static/vendors/swagger-ui-2.1.4/dist/index.html
sed -i.bak '/jsonEditor: false,/a\        validatorUrl: null,' ../src/ui/static/vendors/swagger-ui-2.1.4/dist/index.html
mkdir -p ../src/ui/static/resources/yaml
cp swagger.yaml ../src/ui/static/resources/yaml
sed -i.bak 's/host: localhost/host: '$SERVER_IP'/g' ../src/ui/static/resources/yaml/swagger.yaml
sed -i.bak 's/  \- http$/  \- '$SCHEME'/g' ../src/ui/static/resources/yaml/swagger.yaml
echo "Finish preparation for the Swagger UI."

修改好後執行指令碼,會把包解壓到Harbor的專案下static資料夾中

# 執行指令碼
./prepare-swagger.sh

Harbor配置Swagger遠端REST API

三、配置Harbor倉庫啟動編排的yml檔案

docker-compose.yml 檔案 ui.volumes 段中的新增兩行內容

- ../src/ui/static/vendors/swagger-ui-2.1.4/dist:/harbor/static/vendors/swagger
- ../src/ui/static/resources/yaml/swagger.yaml:/harbor/static/resources/yaml/swagger.yaml

Harbor配置Swagger遠端REST API

修改好後重啟啟動Harbor倉庫,我們可以訪問Swagger的頁面:https://192.168.80.42/static/vendors/swagger/index.html

Harbor配置Swagger遠端REST API

相關文章