simplify output directory tree
Alan Pearce alan@alanpearce.eu
Fri, 28 Jun 2024 14:12:55 +0200
5 files changed, 24 insertions(+), 34 deletions(-)
M .gitignore → .gitignore
@@ -25,7 +25,7 @@ # Go workspace file go.work # End of https://www.toptal.com/developers/gitignore/api/go -/website/ +/public/ /.pre-commit-config.yaml /result
M internal/builder/builder.go → internal/builder/builder.go
@@ -22,7 +22,7 @@ ) 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 @@ log.Debug("output", "dir", outDir) 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 @@ lastMod = posts[0].Date } 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 @@ return nil, err } } - 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 @@ if slices.Contains(post.Taxonomies.Tags, tag) { 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 @@ ) 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 @@ feed, err := renderFeed(config.Title, config, posts, "feed") 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 @@ feedStyles, err := renderFeedStyles() 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 @@ content, err := content.RenderMarkdown(text) 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 @@ rob, err := renderRobotsTXT(config) 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 }
M internal/server/server.go → internal/server/server.go
@@ -32,7 +32,7 @@ serverHeader = fmt.Sprintf("website (%s)", ShortSHA) ) 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 @@ err = os.Chdir(vcsConfig.LocalPath) if err != nil { return nil, err } - runtimeConfig.Root = "website" + runtimeConfig.Root = "public" } config, err := cfg.GetConfig()
M internal/website/mux.go → internal/website/mux.go
@@ -3,7 +3,6 @@ import ( "encoding/json" "net/http" - "path" "strings" "website/internal/config" ihttp "website/internal/http" @@ -63,9 +62,8 @@ 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") }