diff options
author | Alan Pearce | 2024-06-25 09:14:56 +0200 |
---|---|---|
committer | Alan Pearce | 2024-06-25 09:14:56 +0200 |
commit | becede4bfead952239b9cf7c8eb9fb96d01534ec (patch) | |
tree | 20cf7d880487d89bc7d1d2e09912409e1a8f4489 /internal | |
parent | 4f5ca39f582d83ded08a6e1cbf1a195141623b1c (diff) | |
download | website-becede4bfead952239b9cf7c8eb9fb96d01534ec.tar.lz website-becede4bfead952239b9cf7c8eb9fb96d01534ec.tar.zst website-becede4bfead952239b9cf7c8eb9fb96d01534ec.zip |
refactor/format
Diffstat (limited to 'internal')
-rw-r--r-- | internal/builder/builder.go | 16 | ||||
-rw-r--r-- | internal/builder/files.go | 40 | ||||
-rw-r--r-- | internal/builder/hasher.go | 1 | ||||
-rw-r--r-- | internal/builder/template.go | 27 | ||||
-rw-r--r-- | internal/log/log.go | 2 | ||||
-rw-r--r-- | internal/server/dev.go | 3 | ||||
-rw-r--r-- | internal/server/server.go | 4 | ||||
-rw-r--r-- | internal/website/filemap.go | 5 | ||||
-rw-r--r-- | internal/website/mux.go | 5 |
9 files changed, 54 insertions, 49 deletions
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("<!doctype html>\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) } |