From 2854b695a0df016ef1a9c67303c15911b0ed4b72 Mon Sep 17 00:00:00 2001 From: mo khan Date: Mon, 21 Apr 2025 12:00:16 -0600 Subject: refactor: provide the root dir as a parameter --- app/app.go | 6 ++++-- app/app_test.go | 6 ++++-- cmd/sparkled/main.go | 4 +++- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/app/app.go b/app/app.go index 51979a4..8ccb71b 100644 --- a/app/app.go +++ b/app/app.go @@ -2,6 +2,7 @@ package app import ( "net/http" + "path/filepath" "github.com/rs/zerolog" "github.com/xlgmokha/x/pkg/ioc" @@ -15,7 +16,7 @@ import ( "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/web/middleware" ) -func New() http.Handler { +func New(rootDir string) http.Handler { mux := ioc.MustResolve[*http.ServeMux](ioc.Default) mountable := []web.Mountable{ @@ -28,7 +29,8 @@ func New() http.Handler { m.MountTo(mux) } - mux.Handle("GET /", http.FileServer(http.Dir("public"))) + dir := http.Dir(filepath.Join(rootDir, "public")) + mux.Handle("GET /", http.FileServer(dir)) logger := ioc.MustResolve[*zerolog.Logger](ioc.Default) oidc := ioc.MustResolve[*oidc.OpenID](ioc.Default) diff --git a/app/app_test.go b/app/app_test.go index 27f5f9c..bf706bc 100644 --- a/app/app_test.go +++ b/app/app_test.go @@ -3,18 +3,20 @@ package app import ( "net/http" "net/http/httptest" + "os" "testing" "github.com/stretchr/testify/assert" + "github.com/xlgmokha/x/pkg/x" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/test" ) func TestApp(t *testing.T) { t.Run("New", func(t *testing.T) { - server := New() + wd := x.Must(os.Getwd()) + server := New(wd + "/..") t.Run("GET /index.html", func(t *testing.T) { - t.Skip() response := httptest.NewRecorder() server.ServeHTTP(response, test.Request("GET", "/")) diff --git a/cmd/sparkled/main.go b/cmd/sparkled/main.go index 5eec970..7bec1b7 100644 --- a/cmd/sparkled/main.go +++ b/cmd/sparkled/main.go @@ -3,8 +3,10 @@ package main import ( "log" "net/http" + "os" "github.com/xlgmokha/x/pkg/env" + "github.com/xlgmokha/x/pkg/x" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app" ) @@ -14,6 +16,6 @@ func main() { log.Fatal(http.ListenAndServe( bindAddr, - app.New(), + app.New(x.Must(os.Getwd())), )) } -- cgit v1.2.3