From a10c8466b4bf81f9ddd356a2da90e8e5e9b360a5 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Sun, 21 Apr 2024 20:50:10 +0200 Subject: split output directories into website/{public,private} --- cmd/build/build.go | 53 +++++++++++++++++++++++++++++++--------------------- cmd/server/server.go | 21 ++++++++++----------- 2 files changed, 42 insertions(+), 32 deletions(-) (limited to 'cmd') diff --git a/cmd/build/build.go b/cmd/build/build.go index ea611aa..121d5aa 100644 --- a/cmd/build/build.go +++ b/cmd/build/build.go @@ -408,17 +408,35 @@ func build(outDir string) error { if err != nil { return err } - if err := mkdirp(outDir, "post"); err != nil { + + privateDir := path.Join(outDir, "private") + if err := mkdirp(privateDir); err != nil { + return errors.Errorf("could not create private directory: %w", err) + } + publicDir := path.Join(outDir, "public") + if err := mkdirp(publicDir); err != nil { + return errors.Errorf("could not create public directory: %w", err) + } + + err = cp.Copy("static", publicDir, cp.Options{ + PreserveTimes: true, + PermissionControl: cp.AddPermission(0755), + }) + if err != nil { + log.Panic(errors.Errorf("could not copy static files: %v", err)) + } + + if err := mkdirp(publicDir, "post"); err != nil { return errors.WithMessage(err, "could not create post output directory") } slog.Debug("reading posts") - posts, tags, err := readPosts("content", "post", outDir) + posts, tags, err := readPosts("content", "post", publicDir) if err != nil { return err } for _, post := range posts { - if err := mkdirp(outDir, "post", post.Basename); err != nil { + if err := mkdirp(publicDir, "post", post.Basename); err != nil { return errors.WithMessage(err, "could not create directory for post") } slog.Debug("rendering post", "post", post.Basename) @@ -431,7 +449,7 @@ func build(outDir string) error { } } - if err := mkdirp(outDir, "tags"); err != nil { + if err := mkdirp(publicDir, "tags"); err != nil { return errors.WithMessage(err, "could not create directory for tags") } slog.Debug("rendering tags list") @@ -439,7 +457,7 @@ func build(outDir string) error { if err != nil { return errors.WithMessage(err, "could not render tags") } - if err := outputToFile(output, outDir, "tags", "index.html"); err != nil { + if err := outputToFile(output, publicDir, "tags", "index.html"); err != nil { return err } @@ -450,7 +468,7 @@ func build(outDir string) error { matchingPosts = append(matchingPosts, post) } } - if err := mkdirp(outDir, "tags", tag); err != nil { + if err := mkdirp(publicDir, "tags", tag); err != nil { return errors.WithMessage(err, "could not create directory") } slog.Debug("rendering tags page", "tag", tag) @@ -458,7 +476,7 @@ func build(outDir string) error { if err != nil { return errors.WithMessage(err, "could not render tag page") } - if err := outputToFile(output, outDir, "tags", tag, "index.html"); err != nil { + if err := outputToFile(output, publicDir, "tags", tag, "index.html"); err != nil { return err } @@ -467,7 +485,7 @@ func build(outDir string) error { if err != nil { return errors.WithMessage(err, "could not render tag feed page") } - if err := outputToFile(output, outDir, "tags", tag, "atom.xml"); err != nil { + if err := outputToFile(output, publicDir, "tags", tag, "atom.xml"); err != nil { return err } } @@ -477,7 +495,7 @@ func build(outDir string) error { if err != nil { return errors.WithMessage(err, "could not render list page") } - if err := outputToFile(listPage, outDir, "post", "index.html"); err != nil { + if err := outputToFile(listPage, publicDir, "post", "index.html"); err != nil { return err } @@ -486,7 +504,7 @@ func build(outDir string) error { if err != nil { return errors.WithMessage(err, "could not render feed") } - if err := outputToFile(feed, outDir, "atom.xml"); err != nil { + if err := outputToFile(feed, publicDir, "atom.xml"); err != nil { return err } @@ -495,7 +513,7 @@ func build(outDir string) error { if err != nil { return errors.WithMessage(err, "could not render feed styles") } - if err := outputToFile(feedStyles, outDir, "feed-styles.xsl"); err != nil { + if err := outputToFile(feedStyles, publicDir, "feed-styles.xsl"); err != nil { return err } @@ -504,7 +522,7 @@ func build(outDir string) error { if err != nil { return errors.WithMessage(err, "could not render homepage") } - if err := outputToFile(homePage, outDir, "index.html"); err != nil { + if err := outputToFile(homePage, publicDir, "index.html"); err != nil { return err } @@ -513,7 +531,7 @@ func build(outDir string) error { if err != nil { return errors.WithMessage(err, "could not render 404 page") } - if err := outputToFile(notFound, outDir, "404.html"); err != nil { + if err := outputToFile(notFound, privateDir, "404.html"); err != nil { return err } @@ -522,7 +540,7 @@ func build(outDir string) error { type IOConfig struct { Source string `conf:"default:.,short:s"` - Destination string `conf:"default:public,short:d"` + Destination string `conf:"default:website,short:d"` } func main() { @@ -544,13 +562,6 @@ func main() { if err != nil { log.Panic(errors.Errorf("could not remove public directory: %v", err)) } - err = cp.Copy(path.Join(ioConfig.Source, "static"), ioConfig.Destination, cp.Options{ - PreserveTimes: true, - PermissionControl: cp.AddPermission(0755), - }) - if err != nil { - log.Panic(errors.Errorf("could not copy static files: %v", err)) - } if err := build(ioConfig.Destination); err != nil { switch cause := errors.Cause(err).(type) { diff --git a/cmd/server/server.go b/cmd/server/server.go index 935cc1e..cc1821a 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -126,23 +126,22 @@ func main() { website.Use(recover.New(recover.Config{})) - files := http.Dir("public") - notFoundHandler := func(c *fiber.Ctx) error { + prefix := "website/public" + publicFiles := http.Dir(prefix) + website.Use("/", filesystem.New(filesystem.Config{ + Root: publicFiles, + ContentTypeCharset: "utf-8", + MaxAge: int((24 * time.Hour).Seconds()), + })) + website.Use(func(c *fiber.Ctx) error { c.Status(fiber.StatusNotFound).Type("html", "utf-8") - content, err := files.Open("404.html") + content, err := os.Open("website/private/404.html") if err != nil { c.Type("txt") return c.SendString("404 Not Found") } return c.SendStream(content) - } - website.Get("/404.html", notFoundHandler) - website.Use("/", filesystem.New(filesystem.Config{ - Root: files, - ContentTypeCharset: "utf-8", - MaxAge: int((24 * time.Hour).Seconds()), - })) - website.Use(notFoundHandler) + }) hosts[runtimeConfig.BaseURL.Host] = &Host{website} toplevel := fiber.New(fiber.Config{ -- cgit 1.4.1