From f5fd5ef8edc832f86c4cbb752ba4d27959753d19 Mon Sep 17 00:00:00 2001 From: mo khan Date: Thu, 8 May 2025 11:07:55 -0600 Subject: refactor: move html render to data transfer object --- app/controllers/sessions/controller.go | 9 +-------- app/controllers/sessions/dto.go | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 8 deletions(-) (limited to 'app/controllers') diff --git a/app/controllers/sessions/controller.go b/app/controllers/sessions/controller.go index d5b2ba6..afc44f7 100644 --- a/app/controllers/sessions/controller.go +++ b/app/controllers/sessions/controller.go @@ -7,7 +7,6 @@ import ( "github.com/xlgmokha/x/pkg/env" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/cfg" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/middleware" - "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/views" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/oidc" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/pls" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/web" @@ -102,13 +101,7 @@ func (c *Controller) Destroy(w http.ResponseWriter, r *http.Request) { func (c *Controller) redirectTo(w http.ResponseWriter, r *http.Request, location string) { if env.Fetch("APP_ENV", "development") == "production" { - w.WriteHeader(http.StatusOK) - w.Header().Add("Content-Type", "text/html") - - if err := views.Render(w, "sessions/redirect", &RedirectDTO{URL: location}); err != nil { - pls.LogError(r.Context(), err) - w.WriteHeader(http.StatusInternalServerError) - } + (&RedirectDTO{URL: location}).ServeHTTP(w, r) } else { http.Redirect(w, r, location, http.StatusFound) } diff --git a/app/controllers/sessions/dto.go b/app/controllers/sessions/dto.go index 4bb959f..a3ce6ba 100644 --- a/app/controllers/sessions/dto.go +++ b/app/controllers/sessions/dto.go @@ -1,5 +1,19 @@ package sessions +import ( + "net/http" + + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/views" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/pls" +) + type RedirectDTO struct { URL string } + +func (d *RedirectDTO) ServeHTTP(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + w.Header().Add("Content-Type", "text/html") + + pls.LogError(r.Context(), views.Render(w, "sessions/redirect", d)) +} -- cgit v1.2.3