Skip to content

Commit

Permalink
Add a new endpoint for listing slugs in a space (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
nono authored May 25, 2023
1 parent 753bfe5 commit 2d67b1d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
28 changes: 28 additions & 0 deletions registry/finders.go
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,34 @@ func GetAppsList(v *base.VirtualSpace, c *space.Space, opts *AppsListOptions) (i
return cursor, res, nil
}

func GetSlugs(c *space.Space) ([]string, error) {
db := c.AppsDB()
rows, err := db.AllDocs(context.Background(), map[string]interface{}{
"include_docs": true,
"limit": 10000,
})
if err != nil {
return nil, err
}
defer rows.Close()

slugs := make([]string, 0)
for rows.Next() {
if strings.HasPrefix(rows.ID(), "_design") {
continue
}

var app *App
if err := rows.ScanDoc(&app); err != nil {
return nil, err
}
slugs = append(slugs, app.Slug)
}

sort.Strings(slugs)
return slugs, nil
}

type appVersionEntry struct {
app *App
cachedVersions *AppVersions
Expand Down
30 changes: 30 additions & 0 deletions web/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,33 @@ func getAppsList(c echo.Context) error {

return writeJSON(c, j)
}

func getSlugsList(c echo.Context) error {
slugs, err := registry.GetSlugs(getSpace(c))
if err != nil {
return err
}
return writeJSON(c, slugs)
}

func filterGetSlugs(virtual base.VirtualSpace) echo.HandlerFunc {
if virtual.Filter == "select" {
return func(c echo.Context) error {
return writeJSON(c, virtual.Slugs)
}
}

return func(c echo.Context) error {
slugs, err := registry.GetSlugs(getSpace(c))
if err != nil {
return err
}
filtered := slugs[:0]
for _, slug := range slugs {
if virtual.AcceptApp(slug) {
filtered = append(filtered, slug)
}
}
return writeJSON(c, filtered)
}
}
3 changes: 3 additions & 0 deletions web/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ func Router() *echo.Echo {
g.POST("/:app", createVersion, jsonEndpoint, middleware.Gzip())

g.GET("", getAppsList, jsonEndpoint, middleware.Gzip())
g.GET("/slugs", getSlugsList, jsonEndpoint, middleware.Gzip())

g.HEAD("/pending", getPendingVersions, jsonEndpoint, middleware.Gzip())
g.GET("/pending", getPendingVersions, jsonEndpoint, middleware.Gzip())
Expand Down Expand Up @@ -425,6 +426,8 @@ func Router() *echo.Echo {

virtualGetAppsList := applyVirtualSpace(getAppsList, v, name)
g.GET("", virtualGetAppsList, jsonEndpoint, middleware.Gzip())
filteredGetSlugs := filterGetSlugs(v)
g.GET("/slugs", filteredGetSlugs, jsonEndpoint, middleware.Gzip())

filteredGetMaintenanceApps := filterGetMaintenanceApps(v)
g.GET("/maintenance", filteredGetMaintenanceApps, jsonEndpoint, middleware.Gzip())
Expand Down

0 comments on commit 2d67b1d

Please sign in to comment.