aboutsummaryrefslogtreecommitdiff
path: root/pkg/handler
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/handler')
-rw-r--r--pkg/handler/about/handler.go (renamed from pkg/handler/about.go)18
-rw-r--r--pkg/handler/config/handler.go (renamed from pkg/handler/status.go)29
-rw-r--r--pkg/handler/git/handler.go (renamed from pkg/handler/git.go)20
-rw-r--r--pkg/handler/router.go38
-rw-r--r--pkg/handler/static/handler.go (renamed from pkg/handler/static.go)2
5 files changed, 81 insertions, 26 deletions
diff --git a/pkg/handler/about.go b/pkg/handler/about/handler.go
index 3ab2de8..a2caa4e 100644
--- a/pkg/handler/about.go
+++ b/pkg/handler/about/handler.go
@@ -1,4 +1,4 @@
-package handler
+package about
import (
"io"
@@ -13,12 +13,18 @@ import (
"git.gabrielgio.me/cerrado/templates"
)
-type AboutHandler struct {
- readmePath string
-}
+type (
+ AboutHandler struct {
+ readmePath string
+ }
+
+ configurationRepository interface {
+ GetRootReadme() string
+ }
+)
-func NewAboutHandler(readmePath string) *AboutHandler {
- return &AboutHandler{readmePath}
+func NewAboutHandler(configRepo configurationRepository) *AboutHandler {
+ return &AboutHandler{configRepo.GetRootReadme()}
}
func (g *AboutHandler) About(w http.ResponseWriter, _ *http.Request) {
diff --git a/pkg/handler/status.go b/pkg/handler/config/handler.go
index 9baac2c..c278e35 100644
--- a/pkg/handler/status.go
+++ b/pkg/handler/config/handler.go
@@ -1,11 +1,10 @@
-package handler
+package config
import (
"bytes"
"encoding/json"
"log/slog"
"net/http"
- "os"
"github.com/alecthomas/chroma/v2/formatters/html"
"github.com/alecthomas/chroma/v2/lexers"
@@ -15,21 +14,25 @@ import (
"git.gabrielgio.me/cerrado/templates"
)
-func ConfigFile(configPath string) func(http.ResponseWriter, *http.Request) {
+type (
+ configurationRepository interface {
+ GetRootReadme() string
+ List() []*config.GitRepositoryConfiguration
+ }
+)
+
+func ConfigFile(configRepo configurationRepository) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, _ *http.Request) {
- f, err := os.Open(configPath)
- if err != nil {
- slog.Error("Error openning config file", "error", err, "path", configPath)
- return
- }
- c, err := config.Parse(f)
- if err != nil {
- slog.Error("Error parsing config", "error", err, "path", configPath)
- return
+ config := struct {
+ RootReadme string
+ Repositories []*config.GitRepositoryConfiguration
+ }{
+ RootReadme: configRepo.GetRootReadme(),
+ Repositories: configRepo.List(),
}
- b, err := json.MarshalIndent(c, "", " ")
+ b, err := json.MarshalIndent(config, "", " ")
if err != nil {
slog.Error("Error parsing json", "error", err)
return
diff --git a/pkg/handler/git.go b/pkg/handler/git/handler.go
index 1ed2c49..236ac41 100644
--- a/pkg/handler/git.go
+++ b/pkg/handler/git/handler.go
@@ -1,4 +1,4 @@
-package handler
+package git
import (
"log/slog"
@@ -8,12 +8,20 @@ import (
"git.gabrielgio.me/cerrado/templates"
)
-type GitHandler struct {
- gitService *service.GitService
-}
+type (
+ GitHandler struct {
+ gitService gitService
+ }
+
+ gitService interface {
+ ListRepositories() ([]*service.Repository, error)
+ }
+)
-func NewGitHandler(gitService *service.GitService) *GitHandler {
- return &GitHandler{gitService}
+func NewGitHandler(gitService gitService) *GitHandler {
+ return &GitHandler{
+ gitService: gitService,
+ }
}
func (g *GitHandler) List(w http.ResponseWriter, _ *http.Request) {
diff --git a/pkg/handler/router.go b/pkg/handler/router.go
new file mode 100644
index 0000000..a8c9c6f
--- /dev/null
+++ b/pkg/handler/router.go
@@ -0,0 +1,38 @@
+package handler
+
+import (
+ "net/http"
+
+ serverconfig "git.gabrielgio.me/cerrado/pkg/config"
+ "git.gabrielgio.me/cerrado/pkg/handler/about"
+ "git.gabrielgio.me/cerrado/pkg/handler/config"
+ "git.gabrielgio.me/cerrado/pkg/handler/git"
+ "git.gabrielgio.me/cerrado/pkg/handler/static"
+ "git.gabrielgio.me/cerrado/pkg/service"
+)
+
+// Mount handler gets the requires service and repository to build the handlers
+// This functons wraps the whole handler package and wraps it into one part so
+// its sub package don't leak in other places.
+func MountHandler(
+ gitService *service.GitService,
+ configRepo *serverconfig.ConfigurationRepository,
+) (http.Handler, error) {
+ var (
+ gitHandler = git.NewGitHandler(gitService)
+ aboutHandler = about.NewAboutHandler(configRepo)
+ configHander = config.ConfigFile(configRepo)
+ )
+
+ staticHandler, err := static.NewStaticHander("/static/")
+ if err != nil {
+ return nil, err
+ }
+
+ mux := http.NewServeMux()
+ mux.Handle("/static/", staticHandler)
+ mux.HandleFunc("/config", configHander)
+ mux.HandleFunc("/about", aboutHandler.About)
+ mux.HandleFunc("/", gitHandler.List)
+ return mux, nil
+}
diff --git a/pkg/handler/static.go b/pkg/handler/static/handler.go
index 9f312f4..6a826cc 100644
--- a/pkg/handler/static.go
+++ b/pkg/handler/static/handler.go
@@ -1,4 +1,4 @@
-package handler
+package static
import (
"io/fs"