From b7ff80b7be532f4bb64c1daf8cef3462f9938362 Mon Sep 17 00:00:00 2001 From: mo khan Date: Tue, 15 Apr 2025 17:54:43 -0600 Subject: feat: connect a blank dashboard controller --- app/app.go | 6 ++++-- app/controllers/dashboard/controller.go | 18 +++++++++++++++++ app/controllers/dashboard/controller_test.go | 29 ++++++++++++++++++++++++++++ app/init.go | 4 ++++ 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 app/controllers/dashboard/controller.go create mode 100644 app/controllers/dashboard/controller_test.go diff --git a/app/app.go b/app/app.go index ad33d97..6fd3e7f 100644 --- a/app/app.go +++ b/app/app.go @@ -4,6 +4,7 @@ import ( "net/http" "github.com/xlgmokha/x/pkg/ioc" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/controllers/dashboard" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/controllers/health" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/controllers/sessions" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/controllers/sparkles" @@ -14,9 +15,10 @@ func New() http.Handler { mux := ioc.MustResolve[*http.ServeMux](ioc.Default) mountable := []web.Mountable{ - ioc.MustResolve[*sparkles.Controller](ioc.Default), - ioc.MustResolve[*sessions.Controller](ioc.Default), + ioc.MustResolve[*dashboard.Controller](ioc.Default), ioc.MustResolve[*health.Controller](ioc.Default), + ioc.MustResolve[*sessions.Controller](ioc.Default), + ioc.MustResolve[*sparkles.Controller](ioc.Default), } for _, m := range mountable { m.MountTo(mux) diff --git a/app/controllers/dashboard/controller.go b/app/controllers/dashboard/controller.go new file mode 100644 index 0000000..060dbfa --- /dev/null +++ b/app/controllers/dashboard/controller.go @@ -0,0 +1,18 @@ +package dashboard + +import "net/http" + +type Controller struct { +} + +func New() *Controller { + return &Controller{} +} + +func (c *Controller) MountTo(mux *http.ServeMux) { + mux.HandleFunc("GET /dashboard", c.Show) +} + +func (c *Controller) Show(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, "/", http.StatusFound) +} diff --git a/app/controllers/dashboard/controller_test.go b/app/controllers/dashboard/controller_test.go new file mode 100644 index 0000000..0bfb3d6 --- /dev/null +++ b/app/controllers/dashboard/controller_test.go @@ -0,0 +1,29 @@ +package dashboard + +import ( + "net/http" + "testing" + + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/test" + "gotest.tools/v3/assert" +) + +func TestController(t *testing.T) { + mux := http.NewServeMux() + controller := New() + controller.MountTo(mux) + + t.Run("GET /dashboard", func(t *testing.T) { + t.Run("when unauthenticated", func(t *testing.T) { + t.Run("redirects to the home page", func(t *testing.T) { + r, w := test.RequestResponse("GET", "/dashboard") + + mux.ServeHTTP(w, r) + + assert.Equal(t, http.StatusFound, w.Code) + location := w.HeaderMap.Get("Location") + assert.Equal(t, "/", location) + }) + }) + }) +} diff --git a/app/init.go b/app/init.go index f35c2d6..8ef0602 100644 --- a/app/init.go +++ b/app/init.go @@ -8,6 +8,7 @@ import ( "github.com/xlgmokha/x/pkg/env" "github.com/xlgmokha/x/pkg/ioc" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/controllers/dashboard" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/controllers/health" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/controllers/sessions" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/controllers/sparkles" @@ -34,6 +35,9 @@ func init() { ioc.RegisterSingleton[*http.ServeMux](ioc.Default, func() *http.ServeMux { return http.NewServeMux() }) + ioc.Register[*dashboard.Controller](ioc.Default, func() *dashboard.Controller { + return dashboard.New() + }) ioc.Register[*sparkles.Controller](ioc.Default, func() *sparkles.Controller { return sparkles.New(ioc.MustResolve[db.Repository](ioc.Default)) }) -- cgit v1.2.3