补充面向对象编程

This commit is contained in:
yumaojun03 2024-12-15 10:13:35 +08:00
parent 6329093b01
commit 5b25cfefce
8 changed files with 87 additions and 13 deletions

View File

@ -8,13 +8,14 @@ Web全栈开发(Book Api): 必须完成
学号-名 学号-名
- 17007-杨熊猫 [vblog](https://gitlab.com/yangli350/vblogv2) - 17007-杨熊猫 [vblog](https://gitlab.com/yangli350/vblogv2)
- 17031-尹 [book](https://gitlab.com/unmask429/vblog)
- GO17024-mushroom [vblog](https://gitlab.com/go3751246/vblog) - GO17024-mushroom [vblog](https://gitlab.com/go3751246/vblog)
- GO17030-浩 [vblog](https://gitee.com/lihaostudy/vblog) - GO17030-浩 [vblog](https://gitee.com/lihaostudy/vblog)
- Go17005-健蛟 [vblog](https://gitlab.com/xyp-gif/vblog) - Go17005-健蛟 [vblog](https://gitlab.com/xyp-gif/vblog)
- GO17042-志勇 [vblog](https://gitlab.com/laizhiyo/vblog.git) - GO17042-志勇 [vblog](https://gitlab.com/laizhiyo/vblog.git)
- GO17027-wangtao [vblog](https://gitlab.com/wangtao4/go/vblog) - GO17027-wangtao [vblog](https://gitlab.com/wangtao4/go/vblog)
- 17031-尹 [book](https://gitlab.com/unmask429/vblog)
- GO17009-豪豪 [vblog](https://gitlab.com/wangjunhaohenshuai/vlog/-/tree/main/%E4%BD%9C%E4%B8%9A?ref_type=heads) - GO17009-豪豪 [vblog](https://gitlab.com/wangjunhaohenshuai/vlog/-/tree/main/%E4%BD%9C%E4%B8%9A?ref_type=heads)
- GO17023-靖宇 [vblog](https://gitee.com/ballooncode/vblog) - GO17023-靖宇 [vblog](https://gitee.com/ballooncode/vblog)
- 17015-小秦 [vblog](https://gitlab.com/Kevin-qwx/vblog) - 17015-小秦 [vblog](https://gitlab.com/Kevin-qwx/vblog)

View File

@ -311,6 +311,11 @@ curl --location 'http://127.0.0.1:8080/vblog/api/v1/tokens' \
开发一个认证中间件: 用于根据用户携带的Token信息判断用户身份并把用户身份信息方到上下文中传递给后面HandleFunc中使用 开发一个认证中间件: 用于根据用户携带的Token信息判断用户身份并把用户身份信息方到上下文中传递给后面HandleFunc中使用
### ioc优化 ## ioc优化
+ 会用: mcube ioc / golang-ioc
+ 掌握原理: 自己造,手写一个简单
### 问题

View File

@ -4,13 +4,15 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/infraboard/mcube/v2/http/gin/response" "github.com/infraboard/mcube/v2/http/gin/response"
"gitlab.com/go-course-project/go17/vblog/apps/blog" "gitlab.com/go-course-project/go17/vblog/apps/blog"
"gitlab.com/go-course-project/go17/vblog/apps/blog/impl"
"gitlab.com/go-course-project/go17/vblog/middleware" "gitlab.com/go-course-project/go17/vblog/middleware"
) )
func NewBlogApiHandler() *BlogApiHandler { // 上传怎么用:
// main.go 组装的时候,传入具体实现, 实现了业务的 插件化
// 访问我们mock测试, 有助于构造 小规模的单元环境
func NewBlogApiHandler(blogImpl blog.Service) *BlogApiHandler {
return &BlogApiHandler{ return &BlogApiHandler{
blog: impl.BlogService, blog: blogImpl,
} }
} }

View File

@ -4,12 +4,11 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/infraboard/mcube/v2/http/gin/response" "github.com/infraboard/mcube/v2/http/gin/response"
"gitlab.com/go-course-project/go17/vblog/apps/token" "gitlab.com/go-course-project/go17/vblog/apps/token"
"gitlab.com/go-course-project/go17/vblog/apps/token/impl"
) )
func NewTokenApiHandler() *TokenApiHandler { func NewTokenApiHandler(tokenImpl token.UserService) *TokenApiHandler {
return &TokenApiHandler{ return &TokenApiHandler{
token: impl.TokenService, token: tokenImpl,
} }
} }

View File

@ -15,6 +15,12 @@ var TokenService token.Service = &TokenServiceImpl{
user: impl.UserService, user: impl.UserService,
} }
func NewTokenService(user user.AdminService) token.Service {
return &TokenServiceImpl{
user: impl.UserService,
}
}
// 定义一个struct, 用于实现 UserService就是刚才定义的接口 // 定义一个struct, 用于实现 UserService就是刚才定义的接口
// 怎么才能判断这个结构体没有实现这个接口 // 怎么才能判断这个结构体没有实现这个接口
type TokenServiceImpl struct { type TokenServiceImpl struct {

54
vblog/docs/oop.drawio Normal file
View File

@ -0,0 +1,54 @@
<mxfile host="65bd71144e">
<diagram id="C3SzVdnRVYTlk7Q9AATH" name="第 1 页">
<mxGraphModel dx="893" dy="479" 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="8" value="" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="40" y="140" width="710" height="350" as="geometry"/>
</mxCell>
<mxCell id="15" style="edgeStyle=orthogonalEdgeStyle;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;" edge="1" parent="1" source="2" target="4">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="2" value="&lt;div style=&quot;color: rgb(248, 248, 242); background-color: rgb(39, 40, 34); font-family: &amp;quot;Cascadia Code NF&amp;quot;, Menlo, Monaco, &amp;quot;Courier New&amp;quot;, monospace, Menlo, Monaco, &amp;quot;Courier New&amp;quot;, monospace; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: rgb(166, 226, 46); text-decoration-line: underline;&quot;&gt;TokenApiHandler&lt;/span&gt;&lt;/div&gt;" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="290" y="200" width="120" height="60" as="geometry"/>
</mxCell>
<mxCell id="13" style="edgeStyle=orthogonalEdgeStyle;html=1;exitX=0;exitY=0.5;exitDx=0;exitDy=0;entryX=1;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="3" target="4">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="14" value="middle: 用户鉴权" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" vertex="1" connectable="0" parent="13">
<mxGeometry x="-0.12" y="3" relative="1" as="geometry">
<mxPoint as="offset"/>
</mxGeometry>
</mxCell>
<mxCell id="16" style="edgeStyle=orthogonalEdgeStyle;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;" edge="1" parent="1" source="3" target="5">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="3" value="&lt;div style=&quot;color: rgb(248, 248, 242); background-color: rgb(39, 40, 34); font-family: &amp;quot;Cascadia Code NF&amp;quot;, Menlo, Monaco, &amp;quot;Courier New&amp;quot;, monospace, Menlo, Monaco, &amp;quot;Courier New&amp;quot;, monospace; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: rgb(166, 226, 46); text-decoration-line: underline;&quot;&gt;BlogApiHandler&lt;/span&gt;&lt;/div&gt;" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="550" y="200" width="120" height="60" as="geometry"/>
</mxCell>
<mxCell id="12" style="edgeStyle=orthogonalEdgeStyle;html=1;exitX=0;exitY=0.5;exitDx=0;exitDy=0;entryX=1;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="4" target="7">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="4" value="&lt;div style=&quot;color: rgb(248, 248, 242); background-color: rgb(39, 40, 34); font-family: &amp;quot;Cascadia Code NF&amp;quot;, Menlo, Monaco, &amp;quot;Courier New&amp;quot;, monospace, Menlo, Monaco, &amp;quot;Courier New&amp;quot;, monospace; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: rgb(166, 226, 46); text-decoration-line: underline;&quot;&gt;TokenServiceImpl&lt;/span&gt;&lt;/div&gt;" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="290" y="360" width="120" height="60" as="geometry"/>
</mxCell>
<mxCell id="5" value="&lt;div style=&quot;color: rgb(248, 248, 242); background-color: rgb(39, 40, 34); font-family: &amp;quot;Cascadia Code NF&amp;quot;, Menlo, Monaco, &amp;quot;Courier New&amp;quot;, monospace, Menlo, Monaco, &amp;quot;Courier New&amp;quot;, monospace; line-height: 18px;&quot;&gt;&lt;div style=&quot;line-height: 18px;&quot;&gt;&lt;span style=&quot;color: rgb(166, 226, 46); text-decoration-line: underline;&quot;&gt;BlogServiceImpl&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="550" y="360" width="120" height="60" as="geometry"/>
</mxCell>
<mxCell id="7" value="&lt;div style=&quot;color: rgb(248, 248, 242); background-color: rgb(39, 40, 34); font-family: &amp;quot;Cascadia Code NF&amp;quot;, Menlo, Monaco, &amp;quot;Courier New&amp;quot;, monospace, Menlo, Monaco, &amp;quot;Courier New&amp;quot;, monospace; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: rgb(166, 226, 46); text-align: left; text-decoration-line: underline;&quot;&gt;UserServiceImpl&lt;/span&gt;&lt;br&gt;&lt;/div&gt;" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="80" y="300" width="120" height="60" as="geometry"/>
</mxCell>
<mxCell id="9" value="main" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
<mxGeometry x="350" y="100" width="60" height="30" as="geometry"/>
</mxCell>
<mxCell id="17" value="&lt;meta charset=&quot;utf-8&quot;&gt;&lt;div style=&quot;color: rgb(248, 248, 242); background-color: rgb(39, 40, 34); font-family: &amp;quot;Cascadia Code NF&amp;quot;, Menlo, Monaco, &amp;quot;Courier New&amp;quot;, monospace, Menlo, Monaco, &amp;quot;Courier New&amp;quot;, monospace; font-weight: normal; font-size: 12px; line-height: 18px;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; blogApi &lt;/span&gt;&lt;span style=&quot;color: #e6db74;&quot;&gt;&quot;gitlab.com/go-course-project/go17/vblog/apps/blog/api&quot;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; blogImpl &lt;/span&gt;&lt;span style=&quot;color: #e6db74;&quot;&gt;&quot;gitlab.com/go-course-project/go17/vblog/apps/blog/impl&quot;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; tokenApi &lt;/span&gt;&lt;span style=&quot;color: #e6db74;&quot;&gt;&quot;gitlab.com/go-course-project/go17/vblog/apps/token/api&quot;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; tokenImpl &lt;/span&gt;&lt;span style=&quot;color: #e6db74;&quot;&gt;&quot;gitlab.com/go-course-project/go17/vblog/apps/token/impl&quot;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; userImpl &lt;/span&gt;&lt;span style=&quot;color: #e6db74;&quot;&gt;&quot;gitlab.com/go-course-project/go17/vblog/apps/user/impl&quot;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;" style="text;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="160" y="540" width="500" height="110" as="geometry"/>
</mxCell>
<mxCell id="18" value="&lt;meta charset=&quot;utf-8&quot;&gt;&lt;div style=&quot;color: rgb(248, 248, 242); background-color: rgb(39, 40, 34); font-family: &amp;quot;Cascadia Code NF&amp;quot;, Menlo, Monaco, &amp;quot;Courier New&amp;quot;, monospace, Menlo, Monaco, &amp;quot;Courier New&amp;quot;, monospace; font-weight: normal; font-size: 12px; line-height: 18px;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #88846f;&quot;&gt;// 注册业务模块的路有&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; tokenApi.&lt;/span&gt;&lt;span style=&quot;color: #a6e22e;&quot;&gt;NewTokenApiHandler&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(tokenImpl.&lt;/span&gt;&lt;span style=&quot;color: #a6e22e;&quot;&gt;NewTokenService&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #f92672;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #a6e22e;text-decoration: underline;&quot;&gt;userImpl&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #a6e22e;text-decoration: underline;&quot;&gt;UserServiceImpl&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;{})).&lt;/span&gt;&lt;span style=&quot;color: #a6e22e;&quot;&gt;Registry&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(server)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; blogApi.&lt;/span&gt;&lt;span style=&quot;color: #a6e22e;&quot;&gt;NewBlogApiHandler&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #f92672;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #a6e22e;text-decoration: underline;&quot;&gt;blogImpl&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #a6e22e;text-decoration: underline;&quot;&gt;BlogServiceImpl&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;{}).&lt;/span&gt;&lt;span style=&quot;color: #a6e22e;&quot;&gt;Registry&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(server)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;" style="text;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="80" y="690" width="560" height="80" as="geometry"/>
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>

View File

@ -3,10 +3,14 @@ package main
import ( import (
"log" "log"
blogApi "gitlab.com/go-course-project/go17/vblog/apps/blog/api"
blogImpl "gitlab.com/go-course-project/go17/vblog/apps/blog/impl"
tokenApi "gitlab.com/go-course-project/go17/vblog/apps/token/api"
tokenImpl "gitlab.com/go-course-project/go17/vblog/apps/token/impl"
userImpl "gitlab.com/go-course-project/go17/vblog/apps/user/impl"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/infraboard/mcube/v2/ioc/config/http" "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" "gitlab.com/go-course-project/go17/vblog/config"
) )
@ -17,8 +21,10 @@ func main() {
server := gin.Default() server := gin.Default()
// 注册业务模块的路有 // 注册业务模块的路有
tokenApi.NewTokenApiHandler().Registry(server) tokenApi.NewTokenApiHandler(tokenImpl.NewTokenService(&userImpl.UserServiceImpl{})).Registry(server)
blogApi.NewBlogApiHandler().Registry(server) blogApi.NewBlogApiHandler(&blogImpl.BlogServiceImpl{}).Registry(server)
// ... 50 个API
//
// 服务器启动 // 服务器启动
if err := server.Run(http.Get().Addr()); err != nil { if err := server.Run(http.Get().Addr()); err != nil {

View File

@ -10,6 +10,7 @@ import (
"github.com/infraboard/mcube/v2/http/gin/response" "github.com/infraboard/mcube/v2/http/gin/response"
"gitlab.com/go-course-project/go17/vblog/apps/token" "gitlab.com/go-course-project/go17/vblog/apps/token"
"gitlab.com/go-course-project/go17/vblog/apps/token/impl" "gitlab.com/go-course-project/go17/vblog/apps/token/impl"
userImpl "gitlab.com/go-course-project/go17/vblog/apps/user/impl"
) )
// // HandlerFunc defines the handler used by gin middleware as return value. // // HandlerFunc defines the handler used by gin middleware as return value.
@ -29,7 +30,7 @@ func Auth(c *gin.Context) {
accessToken = tkList[1] accessToken = tkList[1]
} }
// 2. 校验Token // 2. 校验Token
tk, err := impl.TokenService.ValidateToken(c.Request.Context(), token.NewValidateTokenRequest(accessToken)) tk, err := impl.NewTokenService(&userImpl.UserServiceImpl{}).ValidateToken(c.Request.Context(), token.NewValidateTokenRequest(accessToken))
if err != nil { if err != nil {
response.Failed(c, exception.NewUnauthorized("令牌校验失败: %s", err)) response.Failed(c, exception.NewUnauthorized("令牌校验失败: %s", err))
c.Abort() c.Abort()