Files
athens/pkg/download/latest.go
Nicholas Wiersma d932d50232 chore: lint code with golangci-lint (#1828)
* feat: add golangci-lint linting

* chore: fix linter issues

* feat: add linting into the workflow

* docs: update lint docs

* fix: cr suggestions

* fix: remove old formatting and vetting scripts

* fix: add docker make target

* fix: action go caching

* fix: depreciated actions checkout version

* fix: cr suggestion

* fix: cr suggestions

---------

Co-authored-by: Manu Gupta <manugupt1@gmail.com>
2023-02-24 20:39:17 -08:00

42 lines
1.0 KiB
Go

package download
import (
"encoding/json"
"net/http"
"github.com/gomods/athens/pkg/download/mode"
"github.com/gomods/athens/pkg/errors"
"github.com/gomods/athens/pkg/log"
"github.com/gomods/athens/pkg/paths"
)
// PathLatest URL.
const PathLatest = "/{module:.+}/@latest"
// LatestHandler implements GET baseURL/module/@latest.
func LatestHandler(dp Protocol, lggr log.Entry, df *mode.DownloadFile) http.Handler {
const op errors.Op = "download.LatestHandler"
f := func(w http.ResponseWriter, r *http.Request) {
mod, err := paths.GetModule(r)
if err != nil {
lggr.SystemErr(errors.E(op, err))
w.WriteHeader(http.StatusInternalServerError)
return
}
info, err := dp.Latest(r.Context(), mod)
if err != nil {
severityLevel := errors.Expect(err, errors.KindNotFound)
err = errors.E(op, err, severityLevel)
lggr.SystemErr(err)
w.WriteHeader(errors.Kind(err))
return
}
if err = json.NewEncoder(w).Encode(info); err != nil {
lggr.SystemErr(errors.E(op, err))
}
}
return http.HandlerFunc(f)
}