From c14f44e81c99cb10eaa75126c9acca3f359acfaa Mon Sep 17 00:00:00 2001 From: "Gustavo \"Guz\" L de Mello" Date: Mon, 16 Jun 2025 07:08:52 -0300 Subject: [PATCH] refactor(user,router): move arguments to struct cfg --- router/router.go | 8 ++++++++ router/user.go | 47 +++++++++++++++++++++++++++++++++-------------- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/router/router.go b/router/router.go index 07b2f3d..1f4bd28 100644 --- a/router/router.go +++ b/router/router.go @@ -96,6 +96,14 @@ func (router *router) setup() http.Handler { r.Use(exception.PanicMiddleware()) r.Use(exception.Middleware()) + userController := newUserController(userControllerCfg{ + UserService: router.userService, + TokenService: router.tokenService, + LoginPath: "/login/", + RedirectPath: "/", + Templates: router.templates, + Assert: router.assert, + }) r.Handle("/assets/", http.StripPrefix("/assets/", http.FileServerFS(router.assets))) diff --git a/router/user.go b/router/user.go index d33c900..818d32e 100644 --- a/router/user.go +++ b/router/user.go @@ -12,28 +12,47 @@ import ( ) type userController struct { - assert tinyssert.Assertions - templates templates.ITemplate - service *service.UserService + userSvc *service.User + tokenSvc *service.Token - loginPath string + loginPath string + redirectPath string + templates templates.ITemplate + + assert tinyssert.Assertions } -func newUserController( - service *service.UserService, - templates templates.ITemplate, - assert tinyssert.Assertions, -) userController { +func newUserController(cfg userControllerCfg) userController { + cfg.Assert.NotNil(cfg.UserService) + cfg.Assert.NotNil(cfg.TokenService) + cfg.Assert.NotZero(cfg.LoginPath) + cfg.Assert.NotZero(cfg.RedirectPath) + cfg.Assert.NotNil(cfg.Templates) + return userController{ - assert: assert, - templates: templates, - service: service, + userSvc: cfg.UserService, + tokenSvc: cfg.TokenService, + loginPath: cfg.LoginPath, + redirectPath: cfg.RedirectPath, + templates: cfg.Templates, + assert: cfg.Assert, } } +type userControllerCfg struct { + UserService *service.User + TokenService *service.Token + + LoginPath string + RedirectPath string + Templates templates.ITemplate + + Assert tinyssert.Assertions +} + func (ctrl userController) login(w http.ResponseWriter, r *http.Request) { - ctrl.assert.NotNil(ctrl.templates) - ctrl.assert.NotNil(ctrl.service) + ctrl.assert.NotNil(ctrl.templates) // TODO?: Remove these types of assertions, since golang will panic anyway + ctrl.assert.NotNil(ctrl.userSvc) // when the methods of these functions are called if r.Method == http.MethodGet { err := ctrl.templates.ExecuteTemplate(w, "login", nil)