From 76460c541456f85a2d780cd78db8227628bb5d20 Mon Sep 17 00:00:00 2001 From: comp500 Date: Sun, 13 Jun 2021 19:15:33 +0100 Subject: [PATCH] Remove pack-root, fix serve command (fixes #39) index.toml is now the pack root - if you want to replicate what the pack-root option would have provided, just move your index.toml (and update the path in pack.toml) --- README.md | 3 +-- cmd/serve.go | 30 ++++++++++++++++++++---------- core/index.go | 6 +----- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 2f3c071..b72cf04 100644 --- a/README.md +++ b/README.md @@ -79,5 +79,4 @@ For use on servers, add the `-g` flag to disable the GUI and `-s server` to down - `mods-folder` The folder to save mod metadata files into, for the install commands (relative to the pack root) - `acceptable-game-versions` A list of additional Minecraft versions to accept when installing or updating mods - `no-internal-hashes` If this is set to true, packwiz will not generate hashes of local files, to prevent merge conflicts and inconsistent hashes when using git/etc. - - `packwiz refresh --build` can be used in this mode to generate internal hashes for distributing the pack with packwiz-installer - - `pack-root` A custom directory, containing the modpack files and optional .packwizignore file (defaults to the index file location) \ No newline at end of file + - `packwiz refresh --build` can be used in this mode to generate internal hashes for distributing the pack with packwiz-installer \ No newline at end of file diff --git a/cmd/serve.go b/cmd/serve.go index 2a463d8..d0b7658 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -5,6 +5,7 @@ import ( "io" "net/http" "os" + "path" "path/filepath" "strconv" "strings" @@ -40,17 +41,25 @@ var serveCmd = &cobra.Command{ os.Exit(1) } indexPath := filepath.Join(filepath.Dir(viper.GetString("pack-file")), filepath.FromSlash(pack.Index.File)) + indexDir := filepath.Dir(indexPath) http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { - path := strings.TrimPrefix(req.URL.Path, "/") + urlPath := strings.TrimPrefix(path.Clean("/"+strings.TrimPrefix(req.URL.Path, "/")), "/") + indexRelPath, err := filepath.Rel(indexDir, filepath.FromSlash(urlPath)) + if err != nil { + fmt.Println(err) + return + } + indexRelPathSlash := path.Clean(filepath.ToSlash(indexRelPath)) + var destPath string found := false - if path == pack.Index.File { + if urlPath == filepath.ToSlash(indexPath) { found = true - path = indexPath + destPath = indexPath // Must be done here, to ensure all paths gain the lock at some point refreshMutex.RLock() - } else if path == filepath.ToSlash(viper.GetString("pack-file")) { + } else if urlPath == filepath.ToSlash(viper.GetString("pack-file")) { found = true if viper.GetBool("serve.refresh") { // Get write lock, to do a refresh @@ -92,24 +101,25 @@ var serveCmd = &cobra.Command{ refreshMutex.Unlock() } refreshMutex.RLock() + destPath = viper.GetString("pack-file") } else { refreshMutex.RLock() // Only allow indexed files for _, v := range index.Files { - if path == v.File { + if indexRelPathSlash == v.File { found = true break } } if found { - path = filepath.Join(filepath.Dir(indexPath), filepath.FromSlash(path)) + destPath = filepath.FromSlash(urlPath) } } defer refreshMutex.RUnlock() if found { - f, err := os.Open(path) + f, err := os.Open(destPath) if err != nil { - fmt.Printf("Error reading file \"%s\": %s\n", path, err) + fmt.Printf("Error reading file \"%s\": %s\n", destPath, err) w.WriteHeader(404) _, _ = w.Write([]byte("File not found")) return @@ -120,13 +130,13 @@ var serveCmd = &cobra.Command{ err = err2 } if err != nil { - fmt.Printf("Error reading file \"%s\": %s\n", path, err) + fmt.Printf("Error reading file \"%s\": %s\n", destPath, err) w.WriteHeader(500) _, _ = w.Write([]byte("Failed to read file")) return } } else { - fmt.Printf("File not found: %s\n", path) + fmt.Printf("File not found: %s\n", destPath) w.WriteHeader(404) _, _ = w.Write([]byte("File not found")) return diff --git a/core/index.go b/core/index.go index 47cfd04..94dab60 100644 --- a/core/index.go +++ b/core/index.go @@ -166,11 +166,7 @@ func (in *Index) updateFile(path string) error { } func (in Index) GetPackRoot() string { - packRoot := viper.GetString("pack-root") - if len(packRoot) == 0 { - packRoot = filepath.Dir(in.indexFile) - } - return packRoot + return filepath.Dir(in.indexFile) } // Refresh updates the hashes of all the files in the index, and adds new files to the index