-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add di by using wire for handler (#17)
* add di for handler * modify wire * feat: use wire for handler * feat: use wire for handler
- Loading branch information
Showing
25 changed files
with
338 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package handler | ||
|
||
import ( | ||
"github.com/google/wire" | ||
|
||
v1 "github.com/go-eagle/eagle-layout/internal/handler/v1" | ||
) | ||
|
||
// here you can add other sets | ||
// ... | ||
|
||
// HandlerSet compose all services and all subsets. | ||
var HandlerSet = wire.NewSet( | ||
v1.NewLoginHandler, | ||
v1.NewRegisterHandler, | ||
) | ||
|
||
type Handler struct { | ||
Login *v1.LoginHandler | ||
Register *v1.RegisterHandler | ||
} | ||
|
||
var ( | ||
// Handle expose the handler to outside | ||
Handle *Handler | ||
) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package v1 | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/go-eagle/eagle-layout/internal/service" | ||
"github.com/go-eagle/eagle/pkg/app" | ||
"github.com/go-eagle/eagle/pkg/errcode" | ||
) | ||
|
||
// LoginHandler 包含 UserService | ||
type LoginHandler struct { | ||
UserService service.UserService | ||
} | ||
|
||
// NewLoginHandler 创建一个新的 LoginHandler | ||
func NewLoginHandler(userService service.UserService) *LoginHandler { | ||
return &LoginHandler{UserService: userService} | ||
} | ||
|
||
// LoginHandler 登录 | ||
// @Summary 用户名和密码登录 | ||
// @Description demo | ||
// @Tags user | ||
// @Accept json | ||
// @Produce json | ||
// @Router /v1/auth/login [post] | ||
func (h *LoginHandler) LoginHandler(c *gin.Context) { | ||
// 从请求中提取参数(例如 JSON 或表单参数) | ||
var loginRequest struct { | ||
Username string `json:"username"` | ||
Password string `json:"password"` | ||
} | ||
|
||
if err := c.ShouldBindJSON(&loginRequest); err != nil { | ||
app.Error(c, errcode.ErrInvalidParam.WithDetails(err.Error())) | ||
return | ||
} | ||
|
||
// 调用 UserService 的逻辑 | ||
user, err := h.UserService.Login(c.Request.Context(), loginRequest.Username, loginRequest.Password) | ||
if err != nil { | ||
app.Error(c, errcode.ErrUnauthorized.WithDetails(err.Error())) | ||
return | ||
} | ||
|
||
// 返回成功响应 | ||
app.Success(c, gin.H{"user": user}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package v1 | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/go-eagle/eagle-layout/internal/service" | ||
"github.com/go-eagle/eagle/pkg/app" | ||
"github.com/go-eagle/eagle/pkg/errcode" | ||
) | ||
|
||
// RegisterHandler 包含 UserService | ||
type RegisterHandler struct { | ||
UserService service.UserService | ||
} | ||
|
||
// NewRegisterHandler 创建一个新的 RegisterHandler | ||
func NewRegisterHandler(userService service.UserService) *RegisterHandler { | ||
return &RegisterHandler{UserService: userService} | ||
} | ||
|
||
// RegisterHandler 登录 | ||
// @Summary 用户名和密码登录 | ||
// @Description demo | ||
// @Tags user | ||
// @Accept json | ||
// @Produce json | ||
// @Router /v1/auth/Register [post] | ||
func (h *RegisterHandler) RegisterHandler(c *gin.Context) { | ||
// 从请求中提取参数(例如 JSON 或表单参数) | ||
var RegisterRequest struct { | ||
Username string `json:"username"` | ||
Password string `json:"password"` | ||
} | ||
|
||
if err := c.ShouldBindJSON(&RegisterRequest); err != nil { | ||
app.Error(c, errcode.ErrInvalidParam.WithDetails(err.Error())) | ||
return | ||
} | ||
|
||
// 调用 UserService 的逻辑 | ||
user, err := h.UserService.Register(c.Request.Context(), RegisterRequest.Username, RegisterRequest.Password) | ||
if err != nil { | ||
app.Error(c, errcode.ErrUnauthorized.WithDetails(err.Error())) | ||
return | ||
} | ||
|
||
// 返回成功响应 | ||
app.Success(c, gin.H{"user": user}) | ||
} |
Oops, something went wrong.