can serve the mod file endpoint now

master
Jordan Orelli 3 years ago
parent f1ee16b319
commit 1a04bde6ef

@ -1,6 +1,7 @@
package main package main
import ( import (
"archive/zip"
"context" "context"
"encoding/json" "encoding/json"
"errors" "errors"
@ -121,12 +122,13 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
// if matches := modP.FindStringSubmatch(r.URL.Path); matches != nil { // $base/$module/@v/$version.mod - get go.mod file for a specific version
// modpath := matches[1] if matches := modP.FindStringSubmatch(r.URL.Path); matches != nil {
// modversion := matches[2] modpath := matches[1]
// h.modfile(modpath, modversion, w, r) modversion := matches[2]
// return h.modfile(modpath, modversion, w, r)
// } return
}
// $base/$module/@v/$version.zip - get the zip bundle of a package version // $base/$module/@v/$version.zip - get the zip bundle of a package version
if matches := zipP.FindStringSubmatch(r.URL.Path); matches != nil { if matches := zipP.FindStringSubmatch(r.URL.Path); matches != nil {
@ -305,15 +307,34 @@ func (h handler) info(modpath, modversion string, w http.ResponseWriter, r *http
}) })
} }
func (h handler) openZip(modpath, version string) (io.ReadCloser, error) { func (h handler) zipPath(modpath, version string) string {
dirname, basename := filepath.Split(modpath) dirname, basename := filepath.Split(modpath)
absdir := filepath.Join(h.root, "modules", dirname) absdir := filepath.Join(h.root, "modules", dirname)
fname := filepath.Join(absdir, fmt.Sprintf("%s@%s.zip", basename, version)) return filepath.Join(absdir, fmt.Sprintf("%s@%s.zip", basename, version))
return os.Open(fname) }
func (h handler) openZip(modpath, version string) (io.ReadCloser, error) {
return os.Open(h.zipPath(modpath, version))
} }
// modfile serves the $base/$module/@v/$version.mod endpoint // modfile serves the $base/$module/@v/$version.mod endpoint
func (h handler) modfile(modpath, modversion string, w http.ResponseWriter, r *http.Request) { func (h handler) modfile(modpath, modversion string, w http.ResponseWriter, r *http.Request) {
rc, err := zip.OpenReader(h.zipPath(modpath, modversion))
if err != nil {
writeError(w, err)
return
}
defer rc.Close()
mfname := fmt.Sprintf("%s@%s/go.mod", modpath, modversion)
f, err := rc.Open(mfname)
if err != nil {
writeError(w, err)
return
}
defer f.Close()
io.Copy(w, f)
} }
// zipfile serves the $base/$module/@v/$version.zip endpoint // zipfile serves the $base/$module/@v/$version.zip endpoint

Loading…
Cancel
Save