-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
44d86fe
commit 4288055
Showing
22 changed files
with
488 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# swagger-ui | ||
|
||
## 优点 | ||
|
||
- 操作方便🐶 | ||
- 可以同时部署多个项目的api文档🚀 | ||
|
||
## 快速使用 | ||
|
||
- 配置nginx | ||
- 把swagger生成的接口文件命名为项目名+环境名+json。如`project.env.json` | ||
- 把`project.env.json`放到docs目录下 | ||
- 执行`./swag-ui`即可 | ||
|
||
## 自定义 | ||
|
||
- 修改main文件 | ||
- 重新编译swag-ui命令 | ||
|
||
``` | ||
go build -o swag-ui main.go | ||
``` | ||
|
||
## 说明 | ||
|
||
- page目录下的文件来自[swagger-api/swagger-ui](https://github.com/swagger-api/swagger-ui/releases) v4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
{ | ||
"schemes": [ | ||
"https" | ||
], | ||
"swagger": "2.0", | ||
"info": { | ||
"description": "简单易用", | ||
"title": "swagger-ui示例", | ||
"contact": { | ||
"name": "swagger-ui", | ||
"url": "https://www.swagger-ui.com", | ||
"email": "[email protected]" | ||
}, | ||
"version": "1.0" | ||
}, | ||
"host": "api.swagger-ui.cn", | ||
"paths": { | ||
"/base/vcode": { | ||
"get": { | ||
"tags": [ | ||
"获取验证码" | ||
], | ||
"summary": "获取验证码", | ||
"parameters": [ | ||
{ | ||
"type": "string", | ||
"description": "11位的手机号码", | ||
"name": "phone", | ||
"in": "formData", | ||
"required": true | ||
} | ||
], | ||
"responses": { | ||
"200": { | ||
"description": "受限: `{\"code\":0,\"msg\":\"success\",\"data\":\"111000\"}`", | ||
"schema": { | ||
"allOf": [ | ||
{ | ||
"$ref": "#/definitions/response.Response" | ||
}, | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"data": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
"422": { | ||
"description": "参数验证失败: `{\"message\":\"Unprocessable Entity\"}`", | ||
"schema": { | ||
"allOf": [ | ||
{ | ||
"$ref": "#/definitions/response.Error" | ||
}, | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"message": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"definitions": { | ||
"response.Error": { | ||
"type": "object", | ||
"properties": { | ||
"message": { | ||
"description": "错误说明", | ||
"type": "string" | ||
} | ||
} | ||
}, | ||
"response.Response": { | ||
"type": "object", | ||
"properties": { | ||
"code": { | ||
"description": "业务状态码", | ||
"type": "integer" | ||
}, | ||
"data": { | ||
"description": "数据", | ||
"type": "object" | ||
}, | ||
"msg": { | ||
"description": "业务消息", | ||
"type": "string" | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<!-- HTML for static distribution bundle build --> | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Swagger UI</title> | ||
<link rel="stylesheet" type="text/css" href="./swagger-ui.css" /> | ||
<link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" /> | ||
<link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16" /> | ||
<style> | ||
html | ||
{ | ||
box-sizing: border-box; | ||
overflow: -moz-scrollbars-vertical; | ||
overflow-y: scroll; | ||
} | ||
|
||
*, | ||
*:before, | ||
*:after | ||
{ | ||
box-sizing: inherit; | ||
} | ||
|
||
body | ||
{ | ||
margin:0; | ||
background: #fafafa; | ||
} | ||
</style> | ||
</head> | ||
|
||
<body> | ||
<div style="background-color: #1f1f1f"> | ||
<select id="api-select"></select> | ||
</div> | ||
<div id="swagger-ui"> | ||
</div> | ||
<script src="./swagger-ui-bundle.js"> </script> | ||
<script src="./swagger-ui-standalone-preset.js"> </script> | ||
<script> | ||
let sites = {config-placeholder} | ||
window.onload = function() { | ||
let selectUrl = function(url) { | ||
window.ui = SwaggerUIBundle({ | ||
url: url, | ||
dom_id: '#swagger-ui', | ||
deepLinking: true, | ||
presets: [ | ||
SwaggerUIBundle.presets.apis, | ||
SwaggerUIStandalonePreset | ||
], | ||
plugins: [ | ||
SwaggerUIBundle.plugins.DownloadUrl | ||
], | ||
layout: "StandaloneLayout" | ||
}) | ||
} | ||
selectUrl(sites[0].url) | ||
let items = document.getElementById('api-select'); | ||
sites.forEach(site => { | ||
let option = document.createElement('option') | ||
option.innerHTML = site.name | ||
option.value = site.url | ||
items.appendChild(option) | ||
}); | ||
items.onchange = function() { | ||
let valOption = this.options[this.selectedIndex].value | ||
selectUrl(valOption) | ||
} | ||
} | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
"time" | ||
) | ||
|
||
var ( | ||
envMap = map[string]string{ | ||
"test": "测试", | ||
"prev": "预发", | ||
"prod": "生产", | ||
} | ||
docsDir = "./docs" | ||
) | ||
|
||
type swaggerJson struct { | ||
Info struct { | ||
Title string `json:"title"` | ||
Version string `json:"version"` | ||
} `json:"info"` | ||
} | ||
|
||
func main() { | ||
// 扫描docs中的文件 | ||
fis, err := ioutil.ReadDir(docsDir) | ||
if err != nil { | ||
fmt.Println("当前目录不存在docs目录") | ||
} | ||
|
||
var data []map[string]string | ||
for _, fi := range fis { | ||
fn := fi.Name() | ||
if !fi.IsDir() && strings.HasSuffix(fn, ".json") { | ||
np := "未知" | ||
for env, name := range envMap { | ||
if strings.Contains(fn, env) { | ||
np = name | ||
break | ||
} | ||
} | ||
dir := fmt.Sprintf("%s/%s", docsDir, fn) | ||
file, err := os.Open(dir) | ||
if err != nil { | ||
break | ||
} | ||
|
||
all, err := ioutil.ReadAll(file) | ||
if err != nil { | ||
_ = file.Close() | ||
break | ||
} else { | ||
_ = file.Close() | ||
} | ||
|
||
var sj swaggerJson | ||
err = json.Unmarshal(all, &sj) | ||
if err != nil { | ||
break | ||
} | ||
|
||
data = append(data, map[string]string{ | ||
"name": fmt.Sprintf("[%s]%s-%s", np, sj.Info.Title, sj.Info.Version), | ||
"url": dir, | ||
}) | ||
|
||
} | ||
} | ||
config, _ := json.Marshal(data) | ||
// 依次读取docs文件中的json,解析出title和文件名,生成map | ||
template, err := os.Open("./index-template.html") | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
return | ||
} | ||
defer template.Close() | ||
all, err := ioutil.ReadAll(template) | ||
content := strings.Replace(string(all), "{config-placeholder}", string(config), 1) | ||
err = ioutil.WriteFile("./page/index.html", []byte(content), 0644) | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
return | ||
} | ||
fmt.Printf("[%s] 🐶 init success.\n", time.Now().Format("2006-01-02 15:04:05")) | ||
os.Exit(0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
server { | ||
listen 80; | ||
server_name swag.swagger-ui.cn; | ||
|
||
gzip on; | ||
gzip_min_length 1k; | ||
gzip_buffers 4 16k; | ||
gzip_http_version 1.1; | ||
gzip_comp_level 7; | ||
gzip_types text/plain text/css text/javascript application/javascript application/json; | ||
gzip_vary on; | ||
|
||
location /docs { | ||
alias /dir/swagger-ui/docs; | ||
} | ||
|
||
location / { | ||
root /dir/swagger-ui/page; | ||
index index.html; | ||
} | ||
|
||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<!-- HTML for static distribution bundle build --> | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Swagger UI</title> | ||
<link rel="stylesheet" type="text/css" href="./swagger-ui.css" /> | ||
<link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" /> | ||
<link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16" /> | ||
<style> | ||
html | ||
{ | ||
box-sizing: border-box; | ||
overflow: -moz-scrollbars-vertical; | ||
overflow-y: scroll; | ||
} | ||
|
||
*, | ||
*:before, | ||
*:after | ||
{ | ||
box-sizing: inherit; | ||
} | ||
|
||
body | ||
{ | ||
margin:0; | ||
background: #fafafa; | ||
} | ||
</style> | ||
</head> | ||
|
||
<body> | ||
<div style="background-color: #1f1f1f"> | ||
<select id="api-select"></select> | ||
</div> | ||
<div id="swagger-ui"> | ||
</div> | ||
<script src="./swagger-ui-bundle.js"> </script> | ||
<script src="./swagger-ui-standalone-preset.js"> </script> | ||
<script> | ||
let sites = [{"name":"[测试]示例api-1.0","url":"./docs/example.test.json"}] | ||
window.onload = function() { | ||
let selectUrl = function(url) { | ||
window.ui = SwaggerUIBundle({ | ||
url: url, | ||
dom_id: '#swagger-ui', | ||
deepLinking: true, | ||
presets: [ | ||
SwaggerUIBundle.presets.apis, | ||
SwaggerUIStandalonePreset | ||
], | ||
plugins: [ | ||
SwaggerUIBundle.plugins.DownloadUrl | ||
], | ||
layout: "StandaloneLayout" | ||
}) | ||
} | ||
selectUrl(sites[0].url) | ||
let items = document.getElementById('api-select'); | ||
sites.forEach(site => { | ||
let option = document.createElement('option') | ||
option.innerHTML = site.name | ||
option.value = site.url | ||
items.appendChild(option) | ||
}); | ||
items.onchange = function() { | ||
let valOption = this.options[this.selectedIndex].value | ||
selectUrl(valOption) | ||
} | ||
} | ||
</script> | ||
</body> | ||
</html> |
Oops, something went wrong.