about summary refs log tree commit diff stats
path: root/cmd/server/filemap.go
diff options
context:
space:
mode:
authorAlan Pearce2024-04-24 13:36:57 +0200
committerAlan Pearce2024-04-24 13:36:57 +0200
commit27f92894b50ffc2058c1b2f0db4d78d47a48c843 (patch)
tree6d832b323939c23a8c58a358507417cc2002a633 /cmd/server/filemap.go
parent40ab775c0a63b9fc4ff84c6ae59ab00650b07ee3 (diff)
downloadwebsite-27f92894b50ffc2058c1b2f0db4d78d47a48c843.tar.lz
website-27f92894b50ffc2058c1b2f0db4d78d47a48c843.tar.zst
website-27f92894b50ffc2058c1b2f0db4d78d47a48c843.zip
split code into separate files
Diffstat (limited to 'cmd/server/filemap.go')
-rw-r--r--cmd/server/filemap.go77
1 files changed, 77 insertions, 0 deletions
diff --git a/cmd/server/filemap.go b/cmd/server/filemap.go
new file mode 100644
index 0000000..5f7e1bb
--- /dev/null
+++ b/cmd/server/filemap.go
@@ -0,0 +1,77 @@
+package main
+
+import (
+	"fmt"
+	"hash/fnv"
+	"io"
+	"io/fs"
+	"log"
+	"log/slog"
+	"os"
+	"path/filepath"
+	"strings"
+
+	"github.com/pkg/errors"
+)
+
+type File struct {
+	filename string
+	etag     string
+}
+
+var files = map[string]File{}
+
+func hashFile(filename string) (string, error) {
+	f, err := os.Open(filename)
+	if err != nil {
+		return "", err
+	}
+	defer f.Close()
+	hash := fnv.New64a()
+	if _, err := io.Copy(hash, f); err != nil {
+		return "", err
+	}
+	return fmt.Sprintf(`W/"%x"`, hash.Sum(nil)), nil
+}
+
+func registerFile(urlpath string, filepath string) error {
+	if files[urlpath] != (File{}) {
+		log.Printf("registerFile called with duplicate file, urlPath: %s", urlpath)
+		return nil
+	}
+	hash, err := hashFile(filepath)
+	if err != nil {
+		return err
+	}
+	files[urlpath] = File{
+		filename: filepath,
+		etag:     hash,
+	}
+	return nil
+}
+
+func registerContentFiles(root string) error {
+	err := filepath.WalkDir(root, func(filePath string, f fs.DirEntry, err error) error {
+		if err != nil {
+			return errors.WithMessagef(err, "failed to access path %s", filePath)
+		}
+		relPath, err := filepath.Rel(root, filePath)
+		if err != nil {
+			return errors.WithMessagef(err, "failed to make path relative, path: %s", filePath)
+		}
+		urlPath, _ := strings.CutSuffix(relPath, "index.html")
+		if !f.IsDir() {
+			slog.Debug("registering file", "urlpath", "/"+urlPath)
+			return registerFile("/"+urlPath, filePath)
+		}
+		return nil
+	})
+	if err != nil {
+		return err
+	}
+	return nil
+}
+
+func GetFile(urlPath string) File {
+	return files[urlPath]
+}