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)
This commit is contained in:
comp500 2021-06-13 19:15:33 +01:00
parent 8cbe7d4c61
commit 76460c5414
3 changed files with 22 additions and 17 deletions

View File

@ -80,4 +80,3 @@ For use on servers, add the `-g` flag to disable the GUI and `-s server` to down
- `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)

View File

@ -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

View File

@ -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