Skip to content

Commit 5c871f9

Browse files
committed
make max page size configurable initial code
1 parent fcce6b1 commit 5c871f9

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

api/api.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"net/http"
66
"os"
7+
"strconv"
78

89
"github.com/go-chi/chi/v5"
910
"github.com/go-chi/chi/v5/middleware"
@@ -55,14 +56,16 @@ type Options struct {
5556
Database database.Database
5657
Logger slog.Logger
5758
// Set to <0 to disable.
58-
RateLimit int
59-
Storage storage.Storage
59+
RateLimit int
60+
Storage storage.Storage
61+
MaxPageSize int
6062
}
6163

6264
type API struct {
63-
Database database.Database
64-
Handler http.Handler
65-
Logger slog.Logger
65+
Database database.Database
66+
Handler http.Handler
67+
Logger slog.Logger
68+
MaxPageSize int
6669
}
6770

6871
// New creates a new API server.
@@ -84,9 +87,10 @@ func New(options *Options) *API {
8487
)
8588

8689
api := &API{
87-
Database: options.Database,
88-
Handler: r,
89-
Logger: options.Logger,
90+
Database: options.Database,
91+
Handler: r,
92+
Logger: options.Logger,
93+
MaxPageSize: options.MaxPageSize,
9094
}
9195

9296
r.Get("/", func(rw http.ResponseWriter, r *http.Request) {
@@ -163,10 +167,10 @@ func (api *API) extensionQuery(rw http.ResponseWriter, r *http.Request) {
163167
})
164168
}
165169
for _, filter := range query.Filters {
166-
if filter.PageSize < 0 || filter.PageSize > 50 {
170+
if filter.PageSize < 0 || filter.PageSize > api.MaxPageSize {
167171
httpapi.Write(rw, http.StatusBadRequest, httpapi.ErrorResponse{
168172
Message: "Invalid page size",
169-
Detail: "Check that the page size is between zero and fifty",
173+
Detail: "Check that the page size is between 0 and " + strconv.Itoa(api.MaxPageSize),
170174
RequestID: httpmw.RequestID(r),
171175
})
172176
}

cli/server.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func server() *cobra.Command {
2828
extdir string
2929
repo string
3030
listcacheduration time.Duration
31+
maxpagesize int
3132
)
3233

3334
cmd := &cobra.Command{
@@ -85,9 +86,10 @@ func server() *cobra.Command {
8586

8687
// Start the API server.
8788
mapi := api.New(&api.Options{
88-
Database: database,
89-
Storage: store,
90-
Logger: logger,
89+
Database: database,
90+
Storage: store,
91+
Logger: logger,
92+
MaxPageSize: maxpagesize,
9193
})
9294
server := &http.Server{
9395
Handler: mapi.Handler,
@@ -136,6 +138,7 @@ func server() *cobra.Command {
136138
}
137139

138140
cmd.Flags().StringVar(&extdir, "extensions-dir", "", "The path to extensions.")
141+
cmd.Flags().IntVar(&maxpagesize, "max-page-size", 50, "The maximum number of pages to request")
139142
cmd.Flags().StringVar(&artifactory, "artifactory", "", "Artifactory server URL.")
140143
cmd.Flags().StringVar(&repo, "repo", "", "Artifactory repository.")
141144
cmd.Flags().StringVar(&address, "address", "127.0.0.1:3001", "The address on which to serve the marketplace API.")

storage/local.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type Local struct {
2727
}
2828

2929
type LocalOptions struct {
30-
// How long to cache the list of extensions with their manifests. Zero means
30+
// How long to cache the list of extensions with their manifests. Zero means
3131
// no cache.
3232
ListCacheDuration time.Duration
3333
ExtDir string

0 commit comments

Comments
 (0)