补充中间件Doc

This commit is contained in:
yumaojun03 2024-12-08 17:31:55 +08:00
parent dff4f15b04
commit c537d965e6
5 changed files with 111 additions and 5 deletions

View File

@ -244,13 +244,72 @@ func (h *TokenApiHandler) RevolkToken(ctx *gin.Context) {
} }
``` ```
### 组装程序 ### 组装程序(v1)
怎么做开发显得专业 怎么做开发显得专业
```go
package main
import (
"log"
"github.com/gin-gonic/gin"
"github.com/infraboard/mcube/v2/ioc/config/http"
blogApi "gitlab.com/go-course-project/go17/vblog/apps/blog/api"
tokenApi "gitlab.com/go-course-project/go17/vblog/apps/token/api"
"gitlab.com/go-course-project/go17/vblog/config"
)
func main() {
config.LoadConfig()
// gin Engine, 它包装了http server
server := gin.Default()
// 注册业务模块的路有
tokenApi.NewTokenApiHandler().Registry(server)
blogApi.NewBlogApiHandler().Registry(server)
// 服务器启动
if err := server.Run(http.Get().Addr()); err != nil {
log.Println(err)
}
}
```
功能分层架构(MVC): Book Api
业务分区架构(DDD): Vblog
### 测试验证
```sh
curl --location 'http://127.0.0.1:8080/vblog/api/v1/blogs' \
--header 'Content-Type: application/json' \
--data '{
"title": "POSTMAN测试01",
"author": "will",
"content": "post man 测试",
"summary": "Go全栈项目",
"category": "软件开发"
}'
```
```sh
curl --location 'http://127.0.0.1:8080/vblog/api/v1/tokens' \
--header 'Content-Type: application/json' \
--data '{
"username": "admin",
"password": "123456"
}'
```
### 中间件鉴权 ### 中间件鉴权
![](./docs/middleware.png)
开发一个认证中间件: 用于根据用户携带的Token信息判断用户身份并把用户身份信息方到上下文中传递给后面HandleFunc中使用
### ioc优化 ### ioc优化

View File

@ -20,7 +20,7 @@ type BlogApiHandler struct {
// 提供注册功能, 提供一个Group // 提供注册功能, 提供一个Group
// book := server.Group("/api/tokens") // book := server.Group("/api/tokens")
func (h *BlogApiHandler) Registry(r *gin.Engine) { func (h *BlogApiHandler) Registry(r *gin.Engine) {
router := r.Group("/api/blogs") router := r.Group("/vblog/api/v1/blogs")
router.POST("", h.CreateBlog) router.POST("", h.CreateBlog)
router.GET("", h.QueryBlog) router.GET("", h.QueryBlog)
} }

View File

@ -21,9 +21,9 @@ type TokenApiHandler struct {
// 提供注册功能, 提供一个Group // 提供注册功能, 提供一个Group
// book := server.Group("/api/tokens") // book := server.Group("/api/tokens")
func (h *TokenApiHandler) Registry(r *gin.Engine) { func (h *TokenApiHandler) Registry(r *gin.Engine) {
router := r.Group("/api/tokens") router := r.Group("/vblog/api/v1/tokens")
router.POST("/issue", h.IssueToken) router.POST("", h.IssueToken)
router.POST("/revolk", h.RevolkToken) router.DELETE("", h.RevolkToken)
} }
func (h *TokenApiHandler) IssueToken(ctx *gin.Context) { func (h *TokenApiHandler) IssueToken(ctx *gin.Context) {

View File

@ -0,0 +1,47 @@
<mxfile host="65bd71144e">
<diagram id="GIsrK_2abnF8TOuNHqx3" name="第 1 页">
<mxGraphModel dx="937" dy="537" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
<root>
<mxCell id="0"/>
<mxCell id="1" parent="0"/>
<mxCell id="7" style="edgeStyle=none;html=1;exitX=0;exitY=0.5;exitDx=0;exitDy=0;entryX=1;entryY=0.3333333333333333;entryDx=0;entryDy=0;entryPerimeter=0;" edge="1" parent="1" source="2" target="6">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="9" value="Resp" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" vertex="1" connectable="0" parent="7">
<mxGeometry x="-0.0947" y="-1" relative="1" as="geometry">
<mxPoint as="offset"/>
</mxGeometry>
</mxCell>
<mxCell id="2" value="Handle Func" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="670" y="250" width="120" height="110" as="geometry"/>
</mxCell>
<mxCell id="3" value="打印访问日志" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="120" y="90" width="90" height="390" as="geometry"/>
</mxCell>
<mxCell id="5" style="edgeStyle=none;html=1;exitX=1;exitY=0.3333333333333333;exitDx=0;exitDy=0;exitPerimeter=0;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="4" target="2">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="8" value="Req" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" vertex="1" connectable="0" parent="5">
<mxGeometry x="0.0637" y="-2" relative="1" as="geometry">
<mxPoint x="-1" as="offset"/>
</mxGeometry>
</mxCell>
<mxCell id="4" value="Actor" style="shape=umlActor;verticalLabelPosition=bottom;verticalAlign=top;html=1;outlineConnect=0;" vertex="1" parent="1">
<mxGeometry x="30" y="110" width="30" height="60" as="geometry"/>
</mxCell>
<mxCell id="6" value="Actor" style="shape=umlActor;verticalLabelPosition=bottom;verticalAlign=top;html=1;outlineConnect=0;" vertex="1" parent="1">
<mxGeometry x="30" y="400" width="30" height="60" as="geometry"/>
</mxCell>
<mxCell id="10" value="认证&lt;br&gt;&lt;br&gt;将用户的信息方到上下文中" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="230" y="90" width="90" height="390" as="geometry"/>
</mxCell>
<mxCell id="11" value="鉴权&lt;br&gt;&lt;br&gt;用户有没有权限访问该接口" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="400" y="90" width="90" height="390" as="geometry"/>
</mxCell>
<mxCell id="14" value="链路追踪&lt;br&gt;trace Id" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="500" y="90" width="90" height="390" as="geometry"/>
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>

BIN
vblog/docs/middleware.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB