Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
为了解决部分开发者需要查看接口文档的需求,所以使用 Swagger 提供接口文档。
假设在本地运行此 PR 的代码,当我们的 apiserver 运行在
http://localhost:8080
时,可以通过http://localhost:8080/swagger/index.html
访问到接口文档,访问时,会询问你提供账号和密码(此账号密码非后台系统账号密码,是独立的),填入后就能访问。接口文档的账号密码
可以通过
API_DOC_USERNAME
,API_DOC_PASSWORD
环境变量设置 API 文档的访问账号,如果没有设置,则会使用默认的账号:apidoc,密码:showmethedoc。接口文档的 Host
如果需要在生产环境也能正常使用接口文档对接口进行请求测试,则需要将生产环境下 apiserver 所在的 Host,例如 a.com 设置到
API_DOC_HOST
环境变量。开发环境下无需额外设置。
接口文档中直接调用接口的用法
此系统目前除了登入接口外,其他所有接口都需要验证登录态,简单来说就是在调用其他接口之前,需要先通过登入接口获取一个令牌,后续要调用其他接口时,都会用到这个令牌。
1. 调用登入接口,获取令牌
点击 Try it out
![](https://user-images.githubusercontent.com/13995345/111772043-5f24c400-88e7-11eb-9db6-d1ee3d099728.png)
编辑请求参数并请求登入接口
![](https://user-images.githubusercontent.com/13995345/111772416-c8a4d280-88e7-11eb-8dde-398c14a7308a.png)
请求成功,查看令牌
![](https://user-images.githubusercontent.com/13995345/111772582-fab63480-88e7-11eb-8c6f-5cfb4674ef4f.png)
设置接口文档授权信息
回到接口文档顶部,点击 Authorize 按钮
![](https://user-images.githubusercontent.com/13995345/111771978-4a483080-88e7-11eb-8b43-9c4371672195.png)
将令牌以特定的格式填入(Bearer xxxx),然后点击 Authorize 按钮
![](https://user-images.githubusercontent.com/13995345/111773079-a3649400-88e8-11eb-8df3-9af7b5630219.png)
设置完毕,点击 Close,当前页面设置完毕后切勿刷新,否则需要再次设置(Swagger UI 的默认行为,实在想吐槽)
![](https://user-images.githubusercontent.com/13995345/111773217-d7d85000-88e8-11eb-994c-8205992fa666.png)
请求需要校验登录态的接口,如获取短链接列表接口
在接口页面找到短链接列表接口对应的卡片,点击 Try it out,然后点击 Execute,正常来说下方就会出现短链接列表接口返回的数据了。