From 6079b1d963f34ada5c4b25363f2319901e283936 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Sat, 8 Jun 2024 00:01:44 +0200 Subject: feat: Add error handling --- pkg/handler/about/handler.go | 10 +++---- pkg/handler/config/handler.go | 16 +++++------- pkg/handler/git/handler.go | 61 +++++++++++++++++++------------------------ pkg/handler/router.go | 36 ++++++++++++------------- pkg/handler/static/handler.go | 6 +++-- 5 files changed, 59 insertions(+), 70 deletions(-) (limited to 'pkg/handler') diff --git a/pkg/handler/about/handler.go b/pkg/handler/about/handler.go index 1acde60..ac3d314 100644 --- a/pkg/handler/about/handler.go +++ b/pkg/handler/about/handler.go @@ -2,7 +2,6 @@ package about import ( "io" - "log/slog" "net/http" "os" @@ -27,17 +26,15 @@ func NewAboutHandler(configRepo configurationRepository) *AboutHandler { return &AboutHandler{configRepo.GetRootReadme()} } -func (g *AboutHandler) About(w http.ResponseWriter, _ *http.Request) { +func (g *AboutHandler) About(w http.ResponseWriter, _ *http.Request) error { f, err := os.Open(g.readmePath) if err != nil { - slog.Error("Error loading readme file", "error", err) - return + return err } bs, err := io.ReadAll(f) if err != nil { - slog.Error("Error reading readme file bytes", "error", err) - return + return err } extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock @@ -54,4 +51,5 @@ func (g *AboutHandler) About(w http.ResponseWriter, _ *http.Request) { Body: bs, } templates.WritePageTemplate(w, gitList) + return nil } diff --git a/pkg/handler/config/handler.go b/pkg/handler/config/handler.go index 30f4283..c43b54d 100644 --- a/pkg/handler/config/handler.go +++ b/pkg/handler/config/handler.go @@ -3,7 +3,6 @@ package config import ( "bytes" "encoding/json" - "log/slog" "net/http" "github.com/alecthomas/chroma/v2/formatters/html" @@ -11,6 +10,7 @@ import ( "github.com/alecthomas/chroma/v2/styles" "git.gabrielgio.me/cerrado/pkg/config" + "git.gabrielgio.me/cerrado/pkg/ext" "git.gabrielgio.me/cerrado/templates" ) @@ -21,8 +21,8 @@ type ( } ) -func ConfigFile(configRepo configurationRepository) func(http.ResponseWriter, *http.Request) { - return func(w http.ResponseWriter, _ *http.Request) { +func ConfigFile(configRepo configurationRepository) ext.ErrorRequestHandler { + return func(w http.ResponseWriter, _ *http.Request) error { config := struct { RootReadme string @@ -34,8 +34,7 @@ func ConfigFile(configRepo configurationRepository) func(http.ResponseWriter, *h b, err := json.MarshalIndent(config, "", " ") if err != nil { - slog.Error("Error parsing json", "error", err) - return + return err } lexer := lexers.Get("json") @@ -45,15 +44,13 @@ func ConfigFile(configRepo configurationRepository) func(http.ResponseWriter, *h ) iterator, err := lexer.Tokenise(nil, string(b)) if err != nil { - slog.Error("Error tokenise", "error", err) - return + return err } var code bytes.Buffer err = formatter.Format(&code, style, iterator) if err != nil { - slog.Error("Error format", "error", err) - return + return err } hello := &templates.ConfigPage{ @@ -61,5 +58,6 @@ func ConfigFile(configRepo configurationRepository) func(http.ResponseWriter, *h } templates.WritePageTemplate(w, hello) + return nil } } diff --git a/pkg/handler/git/handler.go b/pkg/handler/git/handler.go index 7bdf372..d952fef 100644 --- a/pkg/handler/git/handler.go +++ b/pkg/handler/git/handler.go @@ -3,7 +3,6 @@ package git import ( "bytes" "io" - "log/slog" "net/http" "os" "path/filepath" @@ -50,23 +49,20 @@ func NewGitHandler(gitService gitService, confRepo configurationRepository) *Git } } -func (g *GitHandler) List(w http.ResponseWriter, _ *http.Request) { +func (g *GitHandler) List(w http.ResponseWriter, _ *http.Request) error { repos, err := g.gitService.ListRepositories() if err != nil { - slog.Error("Error listing repo", "error", err) - return + return err } f, err := os.Open(g.readmePath) if err != nil { - slog.Error("Error loading readme file", "error", err) - return + return err } bs, err := io.ReadAll(f) if err != nil { - slog.Error("Error reading readme file bytes", "error", err) - return + return err } extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock @@ -84,15 +80,15 @@ func (g *GitHandler) List(w http.ResponseWriter, _ *http.Request) { About: bs, } templates.WritePageTemplate(w, gitList) + return nil } -func (g *GitHandler) Summary(w http.ResponseWriter, r *http.Request) { +func (g *GitHandler) Summary(w http.ResponseWriter, r *http.Request) error { ext.SetHTML(w) name := r.PathValue("name") ref, err := g.gitService.GetHead(name) if err != nil { - slog.Error("Error loading head", "error", err) - return + return err } gitList := &templates.GitItemPage{ @@ -101,15 +97,15 @@ func (g *GitHandler) Summary(w http.ResponseWriter, r *http.Request) { GitItemBase: &templates.GitItemSummaryPage{}, } templates.WritePageTemplate(w, gitList) + return nil } -func (g *GitHandler) About(w http.ResponseWriter, r *http.Request) { +func (g *GitHandler) About(w http.ResponseWriter, r *http.Request) error { ext.SetHTML(w) name := r.PathValue("name") ref, err := g.gitService.GetHead(name) if err != nil { - slog.Error("Error loading head", "error", err) - return + return err } gitList := &templates.GitItemPage{ Name: name, @@ -117,28 +113,26 @@ func (g *GitHandler) About(w http.ResponseWriter, r *http.Request) { GitItemBase: &templates.GitItemAboutPage{}, } templates.WritePageTemplate(w, gitList) + return nil } -func (g *GitHandler) Refs(w http.ResponseWriter, r *http.Request) { +func (g *GitHandler) Refs(w http.ResponseWriter, r *http.Request) error { ext.SetHTML(w) name := r.PathValue("name") tags, err := g.gitService.ListTags(name) if err != nil { - slog.Error("Error loading tags", "error", err) - return + return err } branches, err := g.gitService.ListBranches(name) if err != nil { - slog.Error("Error loading branches", "error", err) - return + return err } ref, err := g.gitService.GetHead(name) if err != nil { - slog.Error("Error loading head", "error", err) - return + return err } gitList := &templates.GitItemPage{ @@ -150,9 +144,10 @@ func (g *GitHandler) Refs(w http.ResponseWriter, r *http.Request) { }, } templates.WritePageTemplate(w, gitList) + return nil } -func (g *GitHandler) Tree(w http.ResponseWriter, r *http.Request) { +func (g *GitHandler) Tree(w http.ResponseWriter, r *http.Request) error { ext.SetHTML(w) name := r.PathValue("name") ref := r.PathValue("ref") @@ -160,8 +155,7 @@ func (g *GitHandler) Tree(w http.ResponseWriter, r *http.Request) { tree, err := g.gitService.GetTree(name, ref, rest) if err != nil { - slog.Error("Error loading tree", "error", err) - return + return err } gitList := &templates.GitItemPage{ @@ -175,9 +169,10 @@ func (g *GitHandler) Tree(w http.ResponseWriter, r *http.Request) { }, } templates.WritePageTemplate(w, gitList) + return nil } -func (g *GitHandler) Blob(w http.ResponseWriter, r *http.Request) { +func (g *GitHandler) Blob(w http.ResponseWriter, r *http.Request) error { ext.SetHTML(w) name := r.PathValue("name") ref := r.PathValue("ref") @@ -185,8 +180,7 @@ func (g *GitHandler) Blob(w http.ResponseWriter, r *http.Request) { file, err := g.gitService.GetFileContent(name, ref, rest) if err != nil { - slog.Error("Error loading blob", "error", err) - return + return err } filename := filepath.Base(rest) @@ -197,15 +191,13 @@ func (g *GitHandler) Blob(w http.ResponseWriter, r *http.Request) { ) iterator, err := lexer.Tokenise(nil, file) if err != nil { - slog.Error("Error tokenise", "error", err) - return + return err } var code bytes.Buffer err = formatter.Format(&code, style, iterator) if err != nil { - slog.Error("Error format", "error", err) - return + return err } gitList := &templates.GitItemPage{ @@ -217,17 +209,17 @@ func (g *GitHandler) Blob(w http.ResponseWriter, r *http.Request) { }, } templates.WritePageTemplate(w, gitList) + return nil } -func (g *GitHandler) Log(w http.ResponseWriter, r *http.Request) { +func (g *GitHandler) Log(w http.ResponseWriter, r *http.Request) error { ext.SetHTML(w) name := r.PathValue("name") ref := r.PathValue("ref") commits, err := g.gitService.ListCommits(name, ref) if err != nil { - slog.Error("Error loading commits", "error", err) - return + return err } gitList := &templates.GitItemPage{ @@ -238,6 +230,7 @@ func (g *GitHandler) Log(w http.ResponseWriter, r *http.Request) { }, } templates.WritePageTemplate(w, gitList) + return nil } func GetLexers(filename string) chroma.Lexer { diff --git a/pkg/handler/router.go b/pkg/handler/router.go index bf13ad5..3da812f 100644 --- a/pkg/handler/router.go +++ b/pkg/handler/router.go @@ -20,9 +20,9 @@ func MountHandler( configRepo *serverconfig.ConfigurationRepository, ) (http.Handler, error) { var ( - gitHandler = git.NewGitHandler(gitService, configRepo) - aboutHandler = about.NewAboutHandler(configRepo) - configHander = config.ConfigFile(configRepo) + gitHandler = git.NewGitHandler(gitService, configRepo) + aboutHandler = about.NewAboutHandler(configRepo) + configHandler = config.ConfigFile(configRepo) ) staticHandler, err := static.ServeStaticHandler() @@ -30,21 +30,19 @@ func MountHandler( return nil, err } - mux := http.NewServeMux() + mux := ext.NewRouter() + mux.AddMiddleware(ext.Compress) + mux.AddMiddleware(ext.Log) - mux.HandleFunc("/static/{file}", m(staticHandler)) - mux.HandleFunc("/{name}/about/{$}", m(gitHandler.About)) - mux.HandleFunc("/{name}", m(gitHandler.Summary)) - mux.HandleFunc("/{name}/refs/{$}", m(gitHandler.Refs)) - mux.HandleFunc("/{name}/tree/{ref}/{rest...}", m(gitHandler.Tree)) - mux.HandleFunc("/{name}/blob/{ref}/{rest...}", m(gitHandler.Blob)) - mux.HandleFunc("/{name}/log/{ref}", m(gitHandler.Log)) - mux.HandleFunc("/config", m(configHander)) - mux.HandleFunc("/about", m(aboutHandler.About)) - mux.HandleFunc("/", m(gitHandler.List)) - return mux, nil -} - -func m(next func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) { - return ext.Compress(next) + mux.HandleFunc("/static/{file}", staticHandler) + mux.HandleFunc("/{name}/about/{$}", gitHandler.About) + mux.HandleFunc("/{name}", gitHandler.Summary) + mux.HandleFunc("/{name}/refs/{$}", gitHandler.Refs) + mux.HandleFunc("/{name}/tree/{ref}/{rest...}", gitHandler.Tree) + mux.HandleFunc("/{name}/blob/{ref}/{rest...}", gitHandler.Blob) + mux.HandleFunc("/{name}/log/{ref}", gitHandler.Log) + mux.HandleFunc("/config", configHandler) + mux.HandleFunc("/about", aboutHandler.About) + mux.HandleFunc("/", gitHandler.List) + return mux.Handler(), nil } diff --git a/pkg/handler/static/handler.go b/pkg/handler/static/handler.go index 5155068..0973d75 100644 --- a/pkg/handler/static/handler.go +++ b/pkg/handler/static/handler.go @@ -10,19 +10,21 @@ import ( "git.gabrielgio.me/cerrado/static" ) -func ServeStaticHandler() (func(w http.ResponseWriter, r *http.Request), error) { +func ServeStaticHandler() (ext.ErrorRequestHandler, error) { staticFs, err := fs.Sub(static.Static, ".") if err != nil { return nil, err } - return func(w http.ResponseWriter, r *http.Request) { + return func(w http.ResponseWriter, r *http.Request) error { var ( f = r.PathValue("file") e = filepath.Ext(f) m = mime.TypeByExtension(e) ) ext.SetMIME(w, m) + w.Header().Add("Cache-Control", "immutable") http.ServeFileFS(w, r, staticFs, f) + return nil }, nil } -- cgit v1.2.3