Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Commit

Permalink
auto detect services (#1)
Browse files Browse the repository at this point in the history
* init swagger, update readme

* modified
  • Loading branch information
Windfarer committed Jun 29, 2021
1 parent e2daa13 commit b8ce342
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 70 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ h := openapiv2.NewHandler()
//将/q/路由放在最前匹配
httpSrv.HandlePrefix("/q/", h)
```
启动应用后,在浏览器中输入 [http://\<ip>:\<port>/q/services](http://ip:port/q/services),找到需要渲染的\<package_name>.\<service_name>
![Alt text](/img/services.png)
在浏览器中输入[http://\<ip>:\<port>/q/swagger-ui/](http://\<ip>:\<port>/q/swagger-ui/) (注意结尾有斜杠/),同时在Swagger ui界面的Explore栏目中输入[http://\<ip>:\<port>/q/service/\<package_name>.\<service_name>](http://<ip>:<port>/q/service/\<package_name>.\<service_name>)并点击Explore
![Alt text](/img/swagger.png)
启动应用后,在浏览器中输入 [http://\<ip>:\<port>/q/services](http://ip:port/q/services),在顶栏右侧选框选取希望查看的服务名,即可浏览接口文档。
![select service](/img/swagger.png)

## FAQ
如果访问/q/services出现报错`failed to decompress enc: bad gzipped descriptor: EOF`的报错说明部分依赖的proto文件生成的路径不对导致的,
如果启动时顶栏选框未显示可选的服务名,或访问/q/services出现报错`failed to decompress enc: bad gzipped descriptor: EOF`的报错说明部分依赖的proto文件生成的路径不对导致的,
比如:
- api/basedata/tag/v1/tag.proto
- api/basedata/article/v1/article.proto
Expand Down
Binary file removed img/services.png
Binary file not shown.
Binary file modified img/swagger.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion openapiv2/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/go-kratos/kratos/v2/api/metadata"
"github.com/go-kratos/kratos/v2/transport/http/binding"
_ "github.com/go-kratos/swagger-api/openapiv2/swagger_ui" // import statik static files
_ "github.com/go-kratos/swagger-api/openapiv2/swagger_ui/statik" // import statik static files
mux "github.com/gorilla/mux"
"github.com/rakyll/statik/fs"
)
Expand Down
108 changes: 58 additions & 50 deletions openapiv2/swagger_ui/dist/index.html
Original file line number Diff line number Diff line change
@@ -1,60 +1,68 @@
<!-- 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;
}
<head>
<meta charset="UTF-8">
<title>Swagger UI</title>
<link rel="stylesheet" type="text/css" href="/q/swagger-ui/swagger-ui.css"/>
<link rel="icon" type="image/png" href="/q/swagger-ui/favicon-32x32.png" sizes="32x32"/>
<link rel="icon" type="image/png" href="/q/swagger-ui/favicon-16x16.png" sizes="16x16"/>
<style>
html {
box-sizing: border-box;
overflow: -moz-scrollbars-vertical;
overflow-y: scroll;
}

*,
*:before,
*:after
{
box-sizing: inherit;
}
*,
*:before,
*:after {
box-sizing: inherit;
}

body
{
margin:0;
background: #fafafa;
}
</style>
</head>
body {
margin: 0;
background: #fafafa;
}
</style>
</head>

<body>
<div id="swagger-ui"></div>
<body>
<div id="swagger-ui"></div>

<script src="./swagger-ui-bundle.js" charset="UTF-8"> </script>
<script src="./swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
<script>
window.onload = function() {
// Begin Swagger UI call region
const ui = SwaggerUIBundle({
url: "https://petstore.swagger.io/v2/swagger.json",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
});
// End Swagger UI call region
<script src="/q/swagger-ui/swagger-ui-bundle.js" charset="UTF-8"></script>
<script src="/q/swagger-ui/swagger-ui-standalone-preset.js" charset="UTF-8"></script>
<script>
window.onload = function () {
const servicesUrl = new URL("/q/services",window.location.href);
fetch(servicesUrl.toString())
.then(response => response.json())
.then(data => {
const urls = data.services.filter((x)=>x!=="kratos.api.Metadata").map((x) => {
const url = new URL("/q/service/"+ x,window.location.href);
return {url: url.toString() , name: x}
});
// Begin Swagger UI call region
const ui = SwaggerUIBundle({
urls: urls,
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset,
],
plugins: [
SwaggerUIBundle.plugins.Topbar,
SwaggerUIBundle.plugins.DownloadUrl,
],
layout: "StandaloneLayout"
});
// End Swagger UI call region

window.ui = ui;
});

window.ui = ui;
};
</script>
</body>
</script>
</body>
</html>
1 change: 1 addition & 0 deletions openapiv2/swagger_ui/dist/swagger-ui-bundle.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions openapiv2/swagger_ui/dist/swagger-ui-es-bundle-core.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions openapiv2/swagger_ui/dist/swagger-ui-es-bundle.js.map

Large diffs are not rendered by default.

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions openapiv2/swagger_ui/dist/swagger-ui.js.map

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions openapiv2/swagger_ui/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//go:generate statik -src=./dist

package swagger_ui
14 changes: 14 additions & 0 deletions openapiv2/swagger_ui/statik/statik.go

Large diffs are not rendered by default.

14 changes: 0 additions & 14 deletions openapiv2/swagger_ui/swagger_ui.go

This file was deleted.

0 comments on commit b8ce342

Please sign in to comment.