Files
athens/pkg/download/version_zip.go
Marwan Sulaiman 76fb786324 downloadProtocol: support multi-proxy environments with DownloadFile (#1230)
* downloadProtocol: support multi-proxy environments with DownloadFile

* remove debugging lines

* update config tests

* download/mode: add tests for DownloadFile and friends

* add documentation to Download File
2019-06-08 00:30:07 -04:00

45 lines
1.1 KiB
Go

package download
import (
"io"
"net/http"
"github.com/gomods/athens/pkg/download/mode"
"github.com/gomods/athens/pkg/errors"
"github.com/gomods/athens/pkg/log"
)
// PathVersionZip URL.
const PathVersionZip = "/{module:.+}/@v/{version}.zip"
// ZipHandler implements GET baseURL/module/@v/version.zip
func ZipHandler(dp Protocol, lggr log.Entry, df *mode.DownloadFile) http.Handler {
const op errors.Op = "download.ZipHandler"
f := func(w http.ResponseWriter, r *http.Request) {
mod, ver, err := getModuleParams(r, op)
if err != nil {
lggr.SystemErr(err)
w.WriteHeader(errors.Kind(err))
return
}
zip, err := dp.Zip(r.Context(), mod, ver)
if err != nil {
lggr.SystemErr(err)
if errors.Kind(err) == errors.KindRedirect {
http.Redirect(w, r, getRedirectURL(df.URL(mod), r.URL.Path), errors.KindRedirect)
return
}
w.WriteHeader(errors.Kind(err))
return
}
defer zip.Close()
w.Header().Set("Content-Type", "application/zip")
_, err = io.Copy(w, zip)
if err != nil {
lggr.SystemErr(errors.E(op, errors.M(mod), errors.V(ver), err))
}
}
return http.HandlerFunc(f)
}