diff options
author | Alan Pearce | 2024-05-22 16:48:57 +0200 |
---|---|---|
committer | Alan Pearce | 2024-05-22 16:48:57 +0200 |
commit | aef028263229d8acda28b8e657413f7e9c187833 (patch) | |
tree | e465629cbf9b5fc5e148f0f8bc1c8be97f1e03c7 /internal/server/filemap.go | |
parent | 1d9d4fc9812f9eb15e78398340053afb32a3c55a (diff) | |
download | website-aef028263229d8acda28b8e657413f7e9c187833.tar.lz website-aef028263229d8acda28b8e657413f7e9c187833.tar.zst website-aef028263229d8acda28b8e657413f7e9c187833.zip |
refactor: split server and mux
Diffstat (limited to 'internal/server/filemap.go')
-rw-r--r-- | internal/server/filemap.go | 77 |
1 files changed, 0 insertions, 77 deletions
diff --git a/internal/server/filemap.go b/internal/server/filemap.go deleted file mode 100644 index 6130e65..0000000 --- a/internal/server/filemap.go +++ /dev/null @@ -1,77 +0,0 @@ -package server - -import ( - "fmt" - "hash/fnv" - "io" - "io/fs" - "os" - "path/filepath" - "strings" - - "website/internal/log" - - "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.Info("registerFile called with duplicate file", "url_path", 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() { - log.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] -} |