diff --git a/handler.go b/handler.go index d12401c..2b3c17a 100644 --- a/handler.go +++ b/handler.go @@ -2,10 +2,10 @@ package main import ( "context" - "embed" "encoding/json" "errors" "fmt" + "io" "io/fs" "net" "net/http" @@ -18,9 +18,6 @@ import ( "golang.org/x/mod/semver" ) -//go:embed meta -var content embed.FS - // this is pretty janky, but I didn't want to import a routing library var ( latestP = regexp.MustCompile(`^/dl/(.+)/@latest$`) @@ -116,6 +113,7 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } + // $base/$module/@v/$version.info - get info about a specific version if matches := infoP.FindStringSubmatch(r.URL.Path); matches != nil { modpath := matches[1] modversion := matches[2] @@ -130,12 +128,13 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // return // } - // if matches := zipP.FindStringSubmatch(r.URL.Path); matches != nil { - // modpath := matches[1] - // modversion := matches[2] - // h.zipfile(modpath, modversion, w, r) - // return - // } + // $base/$module/@v/$version.zip - get the zip bundle of a package version + if matches := zipP.FindStringSubmatch(r.URL.Path); matches != nil { + modpath := matches[1] + modversion := matches[2] + h.zipfile(modpath, modversion, w, r) + return + } w.WriteHeader(http.StatusNotFound) w.Write([]byte("not found")) @@ -306,21 +305,30 @@ func (h handler) info(modpath, modversion string, w http.ResponseWriter, r *http }) } +func (h handler) openZip(modpath, version string) (io.ReadCloser, error) { + dirname, basename := filepath.Split(modpath) + absdir := filepath.Join(h.root, "modules", dirname) + fname := filepath.Join(absdir, fmt.Sprintf("%s@%s.zip", basename, version)) + return os.Open(fname) +} + // modfile serves the $base/$module/@v/$version.mod endpoint func (h handler) modfile(modpath, modversion string, w http.ResponseWriter, r *http.Request) { } // zipfile serves the $base/$module/@v/$version.zip endpoint func (h handler) zipfile(modpath, modversion string, w http.ResponseWriter, r *http.Request) { -} - -func serveFile(w http.ResponseWriter, path string) { - b, err := content.ReadFile(path) + zf, err := h.openZip(modpath, modversion) if err != nil { - w.WriteHeader(http.StatusInternalServerError) - fmt.Fprintf(w, err.Error()) + writeError(w, err) + return + } + defer zf.Close() + + w.Header().Add("Content-Type", "application/zip") + if _, err := io.Copy(w, zf); err != nil { + log_error.Printf("error writing zip for module %s version %s: %v", modpath, modversion, err) } - w.Write(b) } type versionInfo struct { diff --git a/meta/fart/modfile b/meta/fart/modfile deleted file mode 100644 index 7249dfa..0000000 --- a/meta/fart/modfile +++ /dev/null @@ -1,3 +0,0 @@ -module orel.li/fart - -go 1.17 diff --git a/meta/fart/root.html b/meta/fart/root.html deleted file mode 100644 index b719026..0000000 --- a/meta/fart/root.html +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - diff --git a/meta/fart/version-list b/meta/fart/version-list deleted file mode 100644 index c0a8dc7..0000000 --- a/meta/fart/version-list +++ /dev/null @@ -1,3 +0,0 @@ -0.0.1 -0.0.2 -0.0.3