diff options
author | Alan Pearce | 2024-04-21 20:50:10 +0200 |
---|---|---|
committer | Alan Pearce | 2024-04-21 21:49:21 +0200 |
commit | a10c8466b4bf81f9ddd356a2da90e8e5e9b360a5 (patch) | |
tree | 63a4182a2a3d8e847609f42c5dbe497cda25379e | |
parent | bbda985b3aec721a7d972d76780ca981140f6f3c (diff) | |
download | website-a10c8466b4bf81f9ddd356a2da90e8e5e9b360a5.tar.lz website-a10c8466b4bf81f9ddd356a2da90e8e5e9b360a5.tar.zst website-a10c8466b4bf81f9ddd356a2da90e8e5e9b360a5.zip |
split output directories into website/{public,private}
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | cmd/build/build.go | 53 | ||||
-rw-r--r-- | cmd/server/server.go | 21 | ||||
-rw-r--r-- | nix/default.nix | 20 |
4 files changed, 53 insertions, 43 deletions
diff --git a/.gitignore b/.gitignore index 3a95bfb..bb1ec91 100644 --- a/.gitignore +++ b/.gitignore @@ -168,7 +168,7 @@ dist .yarn/install-state.gz .pnp.\* /.direnv/ -/public/ +/website /.compressstamp /.formatstamp /server 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{ diff --git a/nix/default.nix b/nix/default.nix index 32c546f..1ee61a2 100644 --- a/nix/default.nix +++ b/nix/default.nix @@ -2,7 +2,7 @@ let version = self.shortRev or self.dirtyShortRev or "unstable-${self.lastModified}"; dockerTag = self.rev or self.dirtyRev or "unstable-${self.lastModified}"; - mkDocker = type: { server, public }: + mkDocker = type: { server, website }: let PORT = 3000; in @@ -11,7 +11,7 @@ let tag = dockerTag; contents = [ (pkgs.writeTextDir "config.toml" (builtins.readFile ./../config.toml)) - public + website ]; config = { Cmd = [ "${server}/bin/server" ]; @@ -46,7 +46,7 @@ with pkgs; rec { modules = ./gomod2nix.toml; subPackages = [ "cmd/build" ]; }; - public = runCommandLocal "build" + website = runCommandLocal "build" { src = with lib.fileset; toSource { root = ./..; @@ -58,7 +58,7 @@ with pkgs; rec { ]; }; } '' - ${builder}/bin/build -s $src -d $out/public + ${builder}/bin/build -s $src -d $out/website ''; server = buildGoApplication { pname = "server"; @@ -76,18 +76,18 @@ with pkgs; rec { modules = ./gomod2nix.toml; ldflags = [ "-s" "-w" "-X" "main.Commit=${version}" ]; }; - docker-stream = mkDockerStream { inherit server public; }; + docker-stream = mkDockerStream { inherit server website; }; docker-stream-aarch64-linux = mkDockerStream { - inherit public; server = (self.packages.aarch64-linux.server); + inherit website; server = (self.packages.aarch64-linux.server); }; docker-stream-x86_64-linux = mkDockerStream { - inherit public; server = (self.packages.x86_64-linux.server); + inherit website; server = (self.packages.x86_64-linux.server); }; - docker-image = mkDockerImage { inherit server public; }; + docker-image = mkDockerImage { inherit server website; }; docker-image-aarch64-linux = mkDockerImage { - inherit public; server = (self.packages.aarch64-linux.server); + inherit website; server = (self.packages.aarch64-linux.server); }; docker-image-x86_64-linux = mkDockerImage { - inherit public; server = (self.packages.x86_64-linux.server); + inherit website; server = (self.packages.x86_64-linux.server); }; } |