补充面向对象编程
This commit is contained in:
parent
6329093b01
commit
5b25cfefce
@ -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)
|
||||||
|
@ -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
|
||||||
|
+ 掌握原理: 自己造,手写一个简单
|
||||||
|
|
||||||
|
### 问题
|
||||||
|
|
||||||
|
|
||||||
|
@ -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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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
54
vblog/docs/oop.drawio
Normal 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="<div style="color: rgb(248, 248, 242); background-color: rgb(39, 40, 34); font-family: &quot;Cascadia Code NF&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace, Menlo, Monaco, &quot;Courier New&quot;, monospace; line-height: 18px;"><span style="color: rgb(166, 226, 46); text-decoration-line: underline;">TokenApiHandler</span></div>" 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="<div style="color: rgb(248, 248, 242); background-color: rgb(39, 40, 34); font-family: &quot;Cascadia Code NF&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace, Menlo, Monaco, &quot;Courier New&quot;, monospace; line-height: 18px;"><span style="color: rgb(166, 226, 46); text-decoration-line: underline;">BlogApiHandler</span></div>" 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="<div style="color: rgb(248, 248, 242); background-color: rgb(39, 40, 34); font-family: &quot;Cascadia Code NF&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace, Menlo, Monaco, &quot;Courier New&quot;, monospace; line-height: 18px;"><span style="color: rgb(166, 226, 46); text-decoration-line: underline;">TokenServiceImpl</span></div>" 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="<div style="color: rgb(248, 248, 242); background-color: rgb(39, 40, 34); font-family: &quot;Cascadia Code NF&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace, Menlo, Monaco, &quot;Courier New&quot;, monospace; line-height: 18px;"><div style="line-height: 18px;"><span style="color: rgb(166, 226, 46); text-decoration-line: underline;">BlogServiceImpl</span></div></div>" 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="<div style="color: rgb(248, 248, 242); background-color: rgb(39, 40, 34); font-family: &quot;Cascadia Code NF&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace, Menlo, Monaco, &quot;Courier New&quot;, monospace; line-height: 18px;"><span style="color: rgb(166, 226, 46); text-align: left; text-decoration-line: underline;">UserServiceImpl</span><br></div>" 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="<meta charset="utf-8"><div style="color: rgb(248, 248, 242); background-color: rgb(39, 40, 34); font-family: &quot;Cascadia Code NF&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-weight: normal; font-size: 12px; line-height: 18px;"><div><span style="color: #f8f8f2;"> blogApi </span><span style="color: #e6db74;">"gitlab.com/go-course-project/go17/vblog/apps/blog/api"</span></div><div><span style="color: #f8f8f2;"> blogImpl </span><span style="color: #e6db74;">"gitlab.com/go-course-project/go17/vblog/apps/blog/impl"</span></div><div><span style="color: #f8f8f2;"> tokenApi </span><span style="color: #e6db74;">"gitlab.com/go-course-project/go17/vblog/apps/token/api"</span></div><div><span style="color: #f8f8f2;"> tokenImpl </span><span style="color: #e6db74;">"gitlab.com/go-course-project/go17/vblog/apps/token/impl"</span></div><div><span style="color: #f8f8f2;"> userImpl </span><span style="color: #e6db74;">"gitlab.com/go-course-project/go17/vblog/apps/user/impl"</span></div></div>" 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="<meta charset="utf-8"><div style="color: rgb(248, 248, 242); background-color: rgb(39, 40, 34); font-family: &quot;Cascadia Code NF&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-weight: normal; font-size: 12px; line-height: 18px;"><div><span style="color: #f8f8f2;"> </span><span style="color: #88846f;">// 注册业务模块的路有</span></div><div><span style="color: #f8f8f2;"> tokenApi.</span><span style="color: #a6e22e;">NewTokenApiHandler</span><span style="color: #f8f8f2;">(tokenImpl.</span><span style="color: #a6e22e;">NewTokenService</span><span style="color: #f8f8f2;">(</span><span style="color: #f92672;">&amp;</span><span style="color: #a6e22e;text-decoration: underline;">userImpl</span><span style="color: #f8f8f2;">.</span><span style="color: #a6e22e;text-decoration: underline;">UserServiceImpl</span><span style="color: #f8f8f2;">{})).</span><span style="color: #a6e22e;">Registry</span><span style="color: #f8f8f2;">(server)</span></div><div><span style="color: #f8f8f2;"> blogApi.</span><span style="color: #a6e22e;">NewBlogApiHandler</span><span style="color: #f8f8f2;">(</span><span style="color: #f92672;">&amp;</span><span style="color: #a6e22e;text-decoration: underline;">blogImpl</span><span style="color: #f8f8f2;">.</span><span style="color: #a6e22e;text-decoration: underline;">BlogServiceImpl</span><span style="color: #f8f8f2;">{}).</span><span style="color: #a6e22e;">Registry</span><span style="color: #f8f8f2;">(server)</span></div></div>" 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>
|
@ -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 {
|
||||||
|
@ -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()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user