From becede4bfead952239b9cf7c8eb9fb96d01534ec Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Tue, 25 Jun 2024 09:14:56 +0200 Subject: refactor/format --- .golangci.yaml | 2 -- go.mod | 3 --- go.sum | 2 -- internal/builder/builder.go | 16 ++++++++++++++-- internal/builder/files.go | 40 ++++++++++++++++++++++++---------------- internal/builder/hasher.go | 1 + internal/builder/template.go | 27 ++------------------------- internal/log/log.go | 2 ++ internal/server/dev.go | 3 ++- internal/server/server.go | 4 ++-- internal/website/filemap.go | 5 +++-- internal/website/mux.go | 5 ++++- nix/gomod2nix.toml | 4 ---- 13 files changed, 54 insertions(+), 60 deletions(-) diff --git a/.golangci.yaml b/.golangci.yaml index e6c1096..1a9f243 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -10,7 +10,6 @@ linters: - grouper - lll - nilerr - - nilnil - nlreturn - noctx - nosprintfhostport @@ -20,4 +19,3 @@ linters: - revive - sloglint - unconvert - - wrapcheck diff --git a/go.mod b/go.mod index 11dddf5..ca792cb 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,6 @@ go 1.22.1 require ( github.com/BurntSushi/toml v1.3.2 github.com/PuerkitoBio/goquery v1.9.2 - github.com/a-h/htmlformat v0.0.0-20231108124658-5bd994fe268e github.com/a-h/templ v0.2.707 github.com/adrg/frontmatter v0.2.0 github.com/andybalholm/brotli v1.1.0 @@ -32,8 +31,6 @@ require ( golang.org/x/net v0.25.0 ) -replace github.com/a-h/htmlformat => github.com/alanpearce/htmlformat v0.0.0-20240425000139-1244374b2562 - require ( github.com/Code-Hex/dd v1.1.0 // indirect github.com/andybalholm/cascadia v1.3.2 // indirect diff --git a/go.sum b/go.sum index d3d8d1f..2e6a0ef 100644 --- a/go.sum +++ b/go.sum @@ -9,8 +9,6 @@ github.com/a-h/templ v0.2.707 h1:T1Gkd2ugbRglZ9rYw/VBchWOSZVKmetDbBkm4YubM7U= github.com/a-h/templ v0.2.707/go.mod h1:5cqsugkq9IerRNucNsI4DEamdHPsoGMQy99DzydLhM8= github.com/adrg/frontmatter v0.2.0 h1:/DgnNe82o03riBd1S+ZDjd43wAmC6W35q67NHeLkPd4= github.com/adrg/frontmatter v0.2.0/go.mod h1:93rQCj3z3ZlwyxxpQioRKC1wDLto4aXHrbqIsnH9wmE= -github.com/alanpearce/htmlformat v0.0.0-20240425000139-1244374b2562 h1:7LpBXZnmFk8+RwdFnAYB7rKZhBQrQ4poPLEhpwwbmSc= -github.com/alanpearce/htmlformat v0.0.0-20240425000139-1244374b2562/go.mod h1:FMIm5afKmEfarNbIXOaPHFY8X7fo+fRQB6I9MPG2nB0= github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M= github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss= diff --git a/internal/builder/builder.go b/internal/builder/builder.go index 933679d..d2d86a6 100644 --- a/internal/builder/builder.go +++ b/internal/builder/builder.go @@ -137,7 +137,12 @@ func build(outDir string, config config.Config) (*Result, error) { return nil, errors.WithMessage(err, "could not create directory for tags") } log.Debug("rendering tags list") - if err := renderToFile(templates.TagsPage(config, "tags", mapset.Sorted(tags), "/tags"), publicDir, "tags", "index.html"); err != nil { + if err := renderToFile( + templates.TagsPage(config, "tags", mapset.Sorted(tags), "/tags"), + publicDir, + "tags", + "index.html", + ); err != nil { return nil, err } sitemap.AddPath("/tags/", lastMod) @@ -154,7 +159,13 @@ func build(outDir string, config config.Config) (*Result, error) { } log.Debug("rendering tags page", "tag", tag) url := "/tags/" + tag - if err := renderToFile(templates.TagPage(config, tag, matchingPosts, url), publicDir, "tags", tag, "index.html"); err != nil { + if err := renderToFile( + templates.TagPage(config, tag, matchingPosts, url), + publicDir, + "tags", + tag, + "index.html", + ); err != nil { return nil, err } sitemap.AddPath(url, matchingPosts[0].Date) @@ -271,5 +282,6 @@ func BuildSite(ioConfig IOConfig) (*Result, error) { } loadCSS() + return build(ioConfig.Destination, *config) } diff --git a/internal/builder/files.go b/internal/builder/files.go index 20fedf9..a9046d7 100644 --- a/internal/builder/files.go +++ b/internal/builder/files.go @@ -32,6 +32,7 @@ func (mw *MultiWriteCloser) Close() error { lastErr = err } } + return lastErr } @@ -45,6 +46,7 @@ func openFileGz(filename string) (*gzip.Writer, error) { if err != nil { return nil, err } + return gzip.NewWriterLevel(f, gzipLevel) } @@ -54,6 +56,7 @@ func openFileBrotli(filename string) (*brotli.Writer, error) { if err != nil { return nil, err } + return brotli.NewWriterLevel(f, brotliLevel), nil } @@ -70,6 +73,7 @@ func multiOpenFile(filename string) (*MultiWriteCloser, error) { if err != nil { return nil, err } + return &MultiWriteCloser{ writers: []io.WriteCloser{r, gz, br}, multiWriter: io.MultiWriter(r, gz, br), @@ -79,34 +83,38 @@ func multiOpenFile(filename string) (*MultiWriteCloser, error) { func openFileAndVariants(filename string) (io.WriteCloser, error) { if compressFiles { return multiOpenFile(filename) - } else { - return openFileWrite(filename) } + + return openFileWrite(filename) } func copyRecursive(src, dst string) error { return filepath.WalkDir(src, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } rel, err := filepath.Rel(src, path) if err != nil { return err } if d.IsDir() { return mkdirp(dst, rel) - } else { - sf, err := os.Open(path) - if err != nil { - return err - } - defer sf.Close() - df, err := openFileAndVariants(filepath.Join(dst, rel)) - if err != nil { - return err - } - defer df.Close() - if _, err := io.Copy(df, sf); err != nil { - return err - } } + + sf, err := os.Open(path) + if err != nil { + return err + } + defer sf.Close() + df, err := openFileAndVariants(filepath.Join(dst, rel)) + if err != nil { + return err + } + defer df.Close() + if _, err := io.Copy(df, sf); err != nil { + return err + } + return nil }) } diff --git a/internal/builder/hasher.go b/internal/builder/hasher.go index dbc29f8..f0f9167 100644 --- a/internal/builder/hasher.go +++ b/internal/builder/hasher.go @@ -8,5 +8,6 @@ import ( func hash(s string) string { shasum := sha256.New() shasum.Write([]byte(s)) + return "sha256-" + base64.StdEncoding.EncodeToString(shasum.Sum(nil)) } diff --git a/internal/builder/template.go b/internal/builder/template.go index 5ec963a..9d81582 100644 --- a/internal/builder/template.go +++ b/internal/builder/template.go @@ -10,14 +10,11 @@ import ( "website/internal/atom" "website/internal/config" "website/internal/content" - "website/internal/log" "github.com/PuerkitoBio/goquery" - "github.com/a-h/htmlformat" "github.com/antchfx/xmlquery" "github.com/antchfx/xpath" "github.com/pkg/errors" - "golang.org/x/net/html" ) var ( @@ -84,6 +81,7 @@ func renderRobotsTXT(config config.Config) (io.Reader, error) { } w.Close() }() + return r, nil } @@ -209,27 +207,6 @@ func getHTMLStyleHash(filenames ...string) (string, error) { return "", err } html := doc.Find("head > style").Text() - return hash(html), nil -} - -func renderHTML(doc *goquery.Document) io.Reader { - r, w := io.Pipe() - - go func() { - _, err := w.Write([]byte("\n")) - if err != nil { - log.Error("error writing doctype", "error", err) - w.CloseWithError(err) - } - err = htmlformat.Nodes(w, []*html.Node{doc.Children().Get(0)}) - if err != nil { - log.Error("error rendering html", "error", err) - w.CloseWithError(err) - return - } - defer w.Close() - }() - - return r + return hash(html), nil } diff --git a/internal/log/log.go b/internal/log/log.go index f5b48b1..a0f5ba7 100644 --- a/internal/log/log.go +++ b/internal/log/log.go @@ -36,8 +36,10 @@ func Fatal(msg string, rest ...any) { func getLevelFromEnv() (zapcore.Level, error) { if str, found := os.LookupEnv("LOG_LEVEL"); found { l, err := zap.ParseAtomicLevel(str) + return l.Level(), err } + return zap.InfoLevel, nil } diff --git a/internal/server/dev.go b/internal/server/dev.go index 7898a09..0763065 100644 --- a/internal/server/dev.go +++ b/internal/server/dev.go @@ -90,7 +90,8 @@ func (watcher FileWatcher) Start(callback func(string)) { } } } - if event.Has(fsnotify.Rename) || event.Has(fsnotify.Write) { + if event.Has(fsnotify.Rename) || event.Has(fsnotify.Write) || + event.Has(fsnotify.Create) || event.Has(fsnotify.Chmod) { if timer == nil { timer = time.AfterFunc(checkSettleInterval, func() { callback(event.Name) diff --git a/internal/server/server.go b/internal/server/server.go index b9db417..60b20d2 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -173,9 +173,9 @@ func New(runtimeConfig *Config) (*Server, error) { func (s *Server) serve(tls bool) error { if tls { return s.serveTLS() - } else { - return s.serveTCP() } + + return s.serveTCP() } func (s *Server) Start() error { diff --git a/internal/website/filemap.go b/internal/website/filemap.go index 1ca242e..28dcd40 100644 --- a/internal/website/filemap.go +++ b/internal/website/filemap.go @@ -26,6 +26,7 @@ func (f *File) AvailableEncodings() []string { for enc := range f.alternatives { encs = append(encs, enc) } + return encs } @@ -67,9 +68,9 @@ func registerFile(urlpath string, fp string) error { if err != nil { if errors.Is(err, os.ErrNotExist) { continue - } else { - return err } + + return err } f.alternatives[enc] = fp + suffix } diff --git a/internal/website/mux.go b/internal/website/mux.go index df58ee5..2d1b971 100644 --- a/internal/website/mux.go +++ b/internal/website/mux.go @@ -55,7 +55,10 @@ func (fn WrappedWebHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if err := fn.handler(w, r); err != nil { if strings.Contains(r.Header.Get("Accept"), "text/html") { w.WriteHeader(err.Code) - templates.NotFound(*fn.config, r.URL.Path).Render(r.Context(), w) + err := templates.NotFound(*fn.config, r.URL.Path).Render(r.Context(), w) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } } else { http.Error(w, err.Message, err.Code) } diff --git a/nix/gomod2nix.toml b/nix/gomod2nix.toml index 6e60a37..8ee87d2 100644 --- a/nix/gomod2nix.toml +++ b/nix/gomod2nix.toml @@ -10,10 +10,6 @@ schema = 3 [mod."github.com/PuerkitoBio/goquery"] version = "v1.9.2" hash = "sha256-9T/XF7YzCDt494E40p46BHZVNGnN4Duqlk/HsdbYEnI=" - [mod."github.com/a-h/htmlformat"] - version = "v0.0.0-20240425000139-1244374b2562" - hash = "sha256-qvnbf/VCR2s2VmyPaQeHLkpA01MNy1g1U0l9B9maBcE=" - replaced = "github.com/alanpearce/htmlformat" [mod."github.com/a-h/templ"] version = "v0.2.707" hash = "sha256-UoM2qj8E7C4NBAMhS/2jrOw0Dj/gnsyZRL4NpRCWaMo=" -- cgit 1.4.1