forked from gomods/athens
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatest.go
42 lines (36 loc) · 1.08 KB
/
latest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
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)
}