From 0bef608297d7def2a18d054e7230fe0e0a3710a7 Mon Sep 17 00:00:00 2001 From: mo khan Date: Sat, 12 Apr 2025 18:11:05 -0600 Subject: refactor: extract mountable interface to allow controllers to mount themselves into the mux --- app/app.go | 27 ++++++++++++++ app/app_test.go | 32 ++++++++++++++++ app/controllers/health/controller.go | 4 ++ app/controllers/sparkles/controller.go | 5 +++ app/controllers/sparkles/controller_test.go | 57 +++++++++++++++++++++++++++++ app/init.go | 25 +++++++++++++ 6 files changed, 150 insertions(+) create mode 100644 app/app.go create mode 100644 app/app_test.go create mode 100644 app/controllers/sparkles/controller_test.go create mode 100644 app/init.go (limited to 'app') diff --git a/app/app.go b/app/app.go new file mode 100644 index 0000000..3edd5f3 --- /dev/null +++ b/app/app.go @@ -0,0 +1,27 @@ +package app + +import ( + "net/http" + + "github.com/xlgmokha/x/pkg/ioc" + "gitlab.com/mokhax/sparkled/app/controllers/health" + "gitlab.com/mokhax/sparkled/app/controllers/sparkles" + "gitlab.com/mokhax/sparkled/pkg/db" + "gitlab.com/mokhax/sparkled/pkg/web" +) + +func New() http.Handler { + mux := ioc.MustResolve[*http.ServeMux](ioc.Default) + + mountable := []web.Mountable{ + sparkles.New(ioc.MustResolve[db.Repository](ioc.Default)), + health.New(), + } + for _, m := range mountable { + m.MountTo(mux) + } + + mux.Handle("GET /", http.FileServer(http.Dir("public"))) + + return mux +} diff --git a/app/app_test.go b/app/app_test.go new file mode 100644 index 0000000..d54397e --- /dev/null +++ b/app/app_test.go @@ -0,0 +1,32 @@ +package app + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/assert" + "gitlab.com/mokhax/sparkled/pkg/test" +) + +func TestApp(t *testing.T) { + t.Run("New", func(t *testing.T) { + server := New() + + t.Run("GET /index.html", func(t *testing.T) { + t.Skip() + response := httptest.NewRecorder() + + server.ServeHTTP(response, test.Request("GET", "/")) + assert.Equal(t, http.StatusOK, response.Code) + assert.Contains(t, response.Body.String(), "SparkleLab") + }) + + t.Run("GET /health", func(t *testing.T) { + response := httptest.NewRecorder() + + server.ServeHTTP(response, test.Request("GET", "/health")) + assert.Equal(t, http.StatusOK, response.Code) + }) + }) +} diff --git a/app/controllers/health/controller.go b/app/controllers/health/controller.go index e241885..5bcff66 100644 --- a/app/controllers/health/controller.go +++ b/app/controllers/health/controller.go @@ -9,6 +9,10 @@ func New() *Controller { return &Controller{} } +func (c *Controller) MountTo(mux *http.ServeMux) { + mux.HandleFunc("GET /health", c.Index) +} + func (c *Controller) Index(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) } diff --git a/app/controllers/sparkles/controller.go b/app/controllers/sparkles/controller.go index 365184f..f00f40c 100644 --- a/app/controllers/sparkles/controller.go +++ b/app/controllers/sparkles/controller.go @@ -16,6 +16,11 @@ func New(db db.Repository) *Controller { return &Controller{db: db} } +func (c *Controller) MountTo(mux *http.ServeMux) { + mux.HandleFunc("GET /sparkles", c.Index) + mux.HandleFunc("POST /sparkles", c.Create) +} + func (c *Controller) Index(w http.ResponseWriter, r *http.Request) { serde.ToHTTP(w, r, c.db.All()) } diff --git a/app/controllers/sparkles/controller_test.go b/app/controllers/sparkles/controller_test.go new file mode 100644 index 0000000..a351da1 --- /dev/null +++ b/app/controllers/sparkles/controller_test.go @@ -0,0 +1,57 @@ +package sparkles + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/xlgmokha/x/pkg/serde" + "gitlab.com/mokhax/sparkled/pkg/db" + "gitlab.com/mokhax/sparkled/pkg/domain" + "gitlab.com/mokhax/sparkled/pkg/test" +) + +func TestSparkles(t *testing.T) { + t.Run("GET /sparkles", func(t *testing.T) { + sparkle, _ := domain.NewSparkle("@tanuki for helping me") + store := db.NewRepository() + store.Save(sparkle) + + controller := New(store) + + t.Run("returns JSON", func(t *testing.T) { + request := test.Request("GET", "/sparkles", + test.WithAcceptHeader(serde.JSON), + ) + response := httptest.NewRecorder() + + controller.Index(response, request) + + assert.Equal(t, http.StatusOK, response.Code) + items, err := serde.FromJSON[[]*domain.Sparkle](response.Body) + require.NoError(t, err) + + assert.Equal(t, 1, len(items)) + assert.Equal(t, "@tanuki", items[0].Sparklee) + assert.Equal(t, "for helping me", items[0].Reason) + }) + }) + + t.Run("POST /sparkles", func(t *testing.T) { + t.Run("saves a new sparkle", func(t *testing.T) { + repository := db.NewRepository() + controller := New(repository) + + sparkle, _ := domain.NewSparkle("@tanuki for reviewing my MR!") + request := test.Request("POST", "/sparkles", test.WithContentType(sparkle, serde.JSON)) + response := httptest.NewRecorder() + + controller.Create(response, request) + + require.Equal(t, http.StatusCreated, response.Code) + assert.Equal(t, 1, len(repository.All())) + }) + }) +} diff --git a/app/init.go b/app/init.go new file mode 100644 index 0000000..e48713d --- /dev/null +++ b/app/init.go @@ -0,0 +1,25 @@ +package app + +import ( + "net/http" + + "github.com/xlgmokha/x/pkg/ioc" + "gitlab.com/mokhax/sparkled/app/controllers/health" + "gitlab.com/mokhax/sparkled/app/controllers/sparkles" + "gitlab.com/mokhax/sparkled/pkg/db" +) + +func init() { + ioc.RegisterSingleton[db.Repository](ioc.Default, func() db.Repository { + return db.NewRepository() + }) + ioc.RegisterSingleton[*http.ServeMux](ioc.Default, func() *http.ServeMux { + return http.NewServeMux() + }) + ioc.Register[*sparkles.Controller](ioc.Default, func() *sparkles.Controller { + return sparkles.New(ioc.MustResolve[db.Repository](ioc.Default)) + }) + ioc.Register[*health.Controller](ioc.Default, func() *health.Controller { + return health.New() + }) +} -- cgit v1.2.3