Files
athens/cmd/proxy/actions/index.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

62 lines
1.5 KiB
Go

package actions
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
"github.com/gomods/athens/pkg/errors"
"github.com/gomods/athens/pkg/index"
"github.com/gomods/athens/pkg/log"
"github.com/sirupsen/logrus"
)
// indexHandler implements GET baseURL/index.
func indexHandler(index index.Indexer) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
list, err := getIndexLines(r, index)
if err != nil {
log.EntryFromContext(ctx).SystemErr(err)
http.Error(w, err.Error(), errors.Kind(err))
return
}
enc := json.NewEncoder(w)
for _, meta := range list {
if err = enc.Encode(meta); err != nil {
log.EntryFromContext(ctx).SystemErr(err)
fmt.Fprintln(w, err)
return
}
}
}
}
func getIndexLines(r *http.Request, index index.Indexer) ([]*index.Line, error) {
const op errors.Op = "actions.IndexHandler"
var (
err error
limit = 2000
since time.Time
)
if limitStr := r.FormValue("limit"); limitStr != "" {
limit, err = strconv.Atoi(limitStr)
if err != nil || limit <= 0 {
return nil, errors.E(op, err, errors.KindBadRequest, logrus.InfoLevel)
}
}
if sinceStr := r.FormValue("since"); sinceStr != "" {
since, err = time.Parse(time.RFC3339, sinceStr)
if err != nil {
return nil, errors.E(op, err, errors.KindBadRequest, logrus.InfoLevel)
}
}
list, err := index.Lines(r.Context(), since, limit)
if err != nil {
return nil, errors.E(op, err)
}
return list, nil
}