diff options
| author | Xe Iaso <me@xeiaso.net> | 2024-01-28 13:56:33 -0500 |
|---|---|---|
| committer | Xe Iaso <me@xeiaso.net> | 2024-01-28 13:56:33 -0500 |
| commit | 9db22cf1af83757e7388a3c035f5c638b9fba22b (patch) | |
| tree | 3b26ede8eb30ff4bf0099867aa2abca3ca941740 /internal/lume | |
| parent | 2f5df3bf784fc77abd1336301bdb38d3bd318387 (diff) | |
| download | xesite-Xe/super-gzip.tar.xz xesite-Xe/super-gzip.zip | |
internal/lume: start support for serving gzip streams directlyXe/super-gzip
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'internal/lume')
| -rw-r--r-- | internal/lume/lume.go | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/internal/lume/lume.go b/internal/lume/lume.go index 47fd497..cded99d 100644 --- a/internal/lume/lume.go +++ b/internal/lume/lume.go @@ -11,6 +11,8 @@ import ( "io/fs" "log" "log/slog" + "net/http" + "net/http/httptest" "os" "os/exec" "path/filepath" @@ -279,6 +281,51 @@ func (f *FS) Update(ctx context.Context) error { return nil } +func (f *FS) crystallizeHeaders(destDir string) error { + fsys := os.DirFS(destDir) + h := http.FileServer(http.FS(fsys)) + + result := map[string]http.Header{} + + if err := fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error { + if err != nil { + return fmt.Errorf("lume: can't walk dir when crystallizing headers: %w", err) + } + + if d.IsDir() { + return nil + } + + req, err := http.NewRequest("GET", "http://localhost/"+path, nil) + if err != nil { + return fmt.Errorf("lume: can't create request when crystallizing headers: %w", err) + } + rw := httptest.NewRecorder() + + h.ServeHTTP(rw, req) + + resp := rw.Result() + + result[path] = resp.Header + + return nil + }); err != nil { + return err + } + + fout, err := os.Create(filepath.Join(destDir, ".xesite-headers.json")) + if err != nil { + return err + } + defer fout.Close() + + if err := json.NewEncoder(fout).Encode(result); err != nil { + return err + } + + return nil +} + func (f *FS) build(ctx context.Context, siteCommit string) error { builds.Add(1) destDir := filepath.Join(f.repoDir, f.opt.StaticSiteDir, "_site") @@ -308,6 +355,10 @@ func (f *FS) build(ctx context.Context, siteCommit string) error { lastBuildTime.Set(dur.Milliseconds()) slog.Info("built site", "dir", destDir, "time", dur.String()) + if err := f.crystallizeHeaders(destDir); err != nil { + return fmt.Errorf("lume: can't crystallize headers: %w", err) + } + zipLoc := filepath.Join(f.opt.DataDir, "site.zip") if err := ZipFolder(filepath.Join(cmd.Dir, "_site"), zipLoc); err != nil { |
