From 0fbf10f9d7bc4991084a2405ba7c3816b3a49e9e Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Fri, 28 Jun 2024 14:12:55 +0200 Subject: simplify output directory tree --- .gitignore | 2 +- internal/builder/builder.go | 42 +++++++++++++++++------------------------- internal/server/server.go | 4 ++-- internal/website/mux.go | 6 ++---- justfile | 4 ++-- 5 files changed, 24 insertions(+), 34 deletions(-) diff --git a/.gitignore b/.gitignore index 2a3a860..130d516 100644 --- a/.gitignore +++ b/.gitignore @@ -25,7 +25,7 @@ vendor/ go.work # End of https://www.toptal.com/developers/gitignore/api/go -/website/ +/public/ /.pre-commit-config.yaml /result diff --git a/internal/builder/builder.go b/internal/builder/builder.go index 177fa2f..0bf6cb2 100644 --- a/internal/builder/builder.go +++ b/internal/builder/builder.go @@ -22,7 +22,7 @@ import ( type IOConfig struct { Source string `conf:"default:.,short:s,flag:src"` - Destination string `conf:"default:website,short:d,flag:dest"` + Destination string `conf:"default:public,short:d,flag:dest"` Development bool `conf:"default:false,flag:dev"` } @@ -91,25 +91,17 @@ func build(outDir string, config *config.Config) (*Result, error) { r := &Result{ Hashes: make([]string, 0), } - privateDir := path.Join(outDir, "private") - if err := mkdirp(privateDir); err != nil { - return nil, errors.WithMessage(err, "could not create private directory") - } - publicDir := path.Join(outDir, "public") - if err := mkdirp(publicDir); err != nil { - return nil, errors.WithMessage(err, "could not create public directory") - } - err := copyRecursive("static", publicDir) + err := copyRecursive("static", outDir) if err != nil { return nil, errors.WithMessage(err, "could not copy static files") } - if err := mkdirp(publicDir, "post"); err != nil { + if err := mkdirp(outDir, "post"); err != nil { return nil, errors.WithMessage(err, "could not create post output directory") } log.Debug("reading posts") - posts, tags, err := content.ReadPosts("content", "post", publicDir) + posts, tags, err := content.ReadPosts("content", "post", outDir) if err != nil { return nil, err } @@ -121,7 +113,7 @@ func build(outDir string, config *config.Config) (*Result, error) { } for _, post := range posts { - if err := mkdirp(publicDir, "post", post.Basename); err != nil { + if err := mkdirp(outDir, "post", post.Basename); err != nil { return nil, errors.WithMessage(err, "could not create directory for post") } log.Debug("rendering post", "post", post.Basename) @@ -131,13 +123,13 @@ func build(outDir string, config *config.Config) (*Result, error) { } } - if err := mkdirp(publicDir, "tags"); err != nil { + if err := mkdirp(outDir, "tags"); err != nil { 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, + outDir, "tags", "index.html", ); err != nil { @@ -152,14 +144,14 @@ func build(outDir string, config *config.Config) (*Result, error) { matchingPosts = append(matchingPosts, post) } } - if err := mkdirp(publicDir, "tags", tag); err != nil { + if err := mkdirp(outDir, "tags", tag); err != nil { return nil, errors.WithMessage(err, "could not create directory") } log.Debug("rendering tags page", "tag", tag) url := "/tags/" + tag if err := renderToFile( templates.TagPage(config, tag, matchingPosts, url), - publicDir, + outDir, "tags", tag, "index.html", @@ -178,13 +170,13 @@ func build(outDir string, config *config.Config) (*Result, error) { if err != nil { return nil, errors.WithMessage(err, "could not render tag feed page") } - if err := outputToFile(feed, publicDir, "tags", tag, "atom.xml"); err != nil { + if err := outputToFile(feed, outDir, "tags", tag, "atom.xml"); err != nil { return nil, err } } log.Debug("rendering list page") - if err := renderToFile(templates.ListPage(config, posts, "/post"), publicDir, "post", "index.html"); err != nil { + if err := renderToFile(templates.ListPage(config, posts, "/post"), outDir, "post", "index.html"); err != nil { return nil, err } sitemap.AddPath("/post/", lastMod) @@ -194,7 +186,7 @@ func build(outDir string, config *config.Config) (*Result, error) { if err != nil { return nil, errors.WithMessage(err, "could not render feed") } - if err := outputToFile(feed, publicDir, "atom.xml"); err != nil { + if err := outputToFile(feed, outDir, "atom.xml"); err != nil { return nil, err } @@ -203,7 +195,7 @@ func build(outDir string, config *config.Config) (*Result, error) { if err != nil { return nil, errors.WithMessage(err, "could not render feed styles") } - if err := outputToFile(feedStyles, publicDir, "feed-styles.xsl"); err != nil { + if err := outputToFile(feedStyles, outDir, "feed-styles.xsl"); err != nil { return nil, err } _, err = feedStyles.Seek(0, 0) @@ -225,18 +217,18 @@ func build(outDir string, config *config.Config) (*Result, error) { if err != nil { return nil, err } - if err := renderToFile(templates.Homepage(config, posts, content), publicDir, "index.html"); err != nil { + if err := renderToFile(templates.Homepage(config, posts, content), outDir, "index.html"); err != nil { return nil, err } // it would be nice to set LastMod here, but using the latest post // date would be wrong as the homepage has its own content file // without a date, which could be newer sitemap.AddPath("/", time.Time{}) - h, _ = getHTMLStyleHash(publicDir, "index.html") + h, _ = getHTMLStyleHash(outDir, "index.html") r.Hashes = append(r.Hashes, h) log.Debug("rendering sitemap") - if err := writerToFile(sitemap, publicDir, "sitemap.xml"); err != nil { + if err := writerToFile(sitemap, outDir, "sitemap.xml"); err != nil { return nil, err } @@ -245,7 +237,7 @@ func build(outDir string, config *config.Config) (*Result, error) { if err != nil { return nil, err } - if err := outputToFile(rob, publicDir, "robots.txt"); err != nil { + if err := outputToFile(rob, outDir, "robots.txt"); err != nil { return nil, err } diff --git a/internal/server/server.go b/internal/server/server.go index 469485f..aa979f4 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -32,7 +32,7 @@ var ( ) type Config struct { - Root string `conf:"default:website"` + Root string `conf:"default:public"` Redirect bool `conf:"default:true"` ListenAddress string `conf:"default:localhost"` Port int `conf:"default:8080,short:p"` @@ -132,7 +132,7 @@ func New(runtimeConfig *Config) (*Server, error) { if err != nil { return nil, err } - runtimeConfig.Root = "website" + runtimeConfig.Root = "public" } config, err := cfg.GetConfig() diff --git a/internal/website/mux.go b/internal/website/mux.go index c125561..265dd20 100644 --- a/internal/website/mux.go +++ b/internal/website/mux.go @@ -3,7 +3,6 @@ package website import ( "encoding/json" "net/http" - "path" "strings" "website/internal/config" ihttp "website/internal/http" @@ -63,9 +62,8 @@ func (fn WrappedWebHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func NewMux(cfg *config.Config, root string) (mux *http.ServeMux, err error) { mux = &http.ServeMux{} - prefix := path.Join(root, "public") - log.Debug("registering content files", "prefix", prefix) - err = registerContentFiles(prefix) + log.Debug("registering content files", "root", root) + err = registerContentFiles(root) if err != nil { return nil, errors.WithMessagef(err, "registering content files") } diff --git a/justfile b/justfile index 6584e01..e632e5a 100755 --- a/justfile +++ b/justfile @@ -7,13 +7,13 @@ default: @just --list --justfile {{ justfile() }} --unsorted clean: - rm -fr website + rm -fr public check-licenses: go-licenses check ./... check-links: - hyperlink website/public --sources content + hyperlink public --sources content update-all: npins update -- cgit 1.4.1