Skip to content

Commit e6ad677

Browse files
authored
make max page size configurable initial code (#42)
* make max page size configurable initial code * extract constant for max page size default, double to 100 * update default page size to 200 + tweak error message * remember to stage changes * update changelog
1 parent fcce6b1 commit e6ad677

File tree

5 files changed

+42
-20
lines changed

5 files changed

+42
-20
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
### Changed
11+
12+
- Default max page size is now 200.
13+
14+
### Added
15+
16+
- Added a flag --max-page-size to server subcommand to set the max page size.
17+
1018
## [2.1.0](https://github.com/coder/code-marketplace/releases/tag/v2.1.0) - 2023-12-21
1119

1220
### Changed

api/api.go

+21-11
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"
@@ -15,6 +16,8 @@ import (
1516
"github.com/coder/code-marketplace/storage"
1617
)
1718

19+
const MaxPageSizeDefault int = 200
20+
1821
// QueryRequest implements an untyped object. It is the data sent to the API to
1922
// query for extensions.
2023
// https://github.com/microsoft/vscode/blob/a69f95fdf3dc27511517eef5ff62b21c7a418015/src/vs/platform/extensionManagement/common/extensionGalleryService.ts#L338-L342
@@ -55,14 +58,16 @@ type Options struct {
5558
Database database.Database
5659
Logger slog.Logger
5760
// Set to <0 to disable.
58-
RateLimit int
59-
Storage storage.Storage
61+
RateLimit int
62+
Storage storage.Storage
63+
MaxPageSize int
6064
}
6165

6266
type API struct {
63-
Database database.Database
64-
Handler http.Handler
65-
Logger slog.Logger
67+
Database database.Database
68+
Handler http.Handler
69+
Logger slog.Logger
70+
MaxPageSize int
6671
}
6772

6873
// New creates a new API server.
@@ -71,6 +76,10 @@ func New(options *Options) *API {
7176
options.RateLimit = 512
7277
}
7378

79+
if options.MaxPageSize == 0 {
80+
options.MaxPageSize = MaxPageSizeDefault
81+
}
82+
7483
r := chi.NewRouter()
7584

7685
r.Use(
@@ -84,9 +93,10 @@ func New(options *Options) *API {
8493
)
8594

8695
api := &API{
87-
Database: options.Database,
88-
Handler: r,
89-
Logger: options.Logger,
96+
Database: options.Database,
97+
Handler: r,
98+
Logger: options.Logger,
99+
MaxPageSize: options.MaxPageSize,
90100
}
91101

92102
r.Get("/", func(rw http.ResponseWriter, r *http.Request) {
@@ -163,10 +173,10 @@ func (api *API) extensionQuery(rw http.ResponseWriter, r *http.Request) {
163173
})
164174
}
165175
for _, filter := range query.Filters {
166-
if filter.PageSize < 0 || filter.PageSize > 50 {
176+
if filter.PageSize < 0 || filter.PageSize > api.MaxPageSize {
167177
httpapi.Write(rw, http.StatusBadRequest, httpapi.ErrorResponse{
168-
Message: "Invalid page size",
169-
Detail: "Check that the page size is between zero and fifty",
178+
Message: "The page size must be between 0 and " + strconv.Itoa(api.MaxPageSize),
179+
Detail: "Contact an administrator to increase the page size",
170180
RequestID: httpmw.RequestID(r),
171181
})
172182
}

api/api_test.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ func TestAPI(t *testing.T) {
119119
}},
120120
},
121121
Response: &httpapi.ErrorResponse{
122-
Message: "Invalid page size",
123-
Detail: "Check that the page size is between zero and fifty",
122+
Message: "The page size must be between 0 and 200",
123+
Detail: "Contact an administrator to increase the page size",
124124
},
125125
},
126126
{
@@ -255,9 +255,10 @@ func TestAPI(t *testing.T) {
255255

256256
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug)
257257
apiServer := api.New(&api.Options{
258-
Database: testutil.NewMockDB(exts),
259-
Storage: testutil.NewMockStorage(),
260-
Logger: logger,
258+
Database: testutil.NewMockDB(exts),
259+
Storage: testutil.NewMockStorage(),
260+
Logger: logger,
261+
MaxPageSize: api.MaxPageSizeDefault,
261262
})
262263

263264
server := httptest.NewServer(apiServer.Handler)

cli/server.go

+6-3
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", api.MaxPageSizeDefault, "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

+1-1
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)