Implement basic API scoping
This commit is contained in:
parent
064ae2f564
commit
9db53d39c4
21 changed files with 519 additions and 264 deletions
71
internal/dis/component/server/list/api.go
Normal file
71
internal/dis/component/server/list/api.go
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
package list
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth/api"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth/scopes"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/status"
|
||||
)
|
||||
|
||||
// API implements an API to list all instances
|
||||
type API struct {
|
||||
component.Base
|
||||
Dependencies struct {
|
||||
ListInstances *ListInstances
|
||||
Auth *auth.Auth
|
||||
}
|
||||
}
|
||||
|
||||
func (lia *API) Routes() component.Routes {
|
||||
return component.Routes{
|
||||
Prefix: "/api/v1/instances/directory",
|
||||
Exact: true,
|
||||
}
|
||||
}
|
||||
|
||||
// APISystem represents a system returned by the api
|
||||
type APISystem struct {
|
||||
Slug string
|
||||
URL string
|
||||
Tagline string
|
||||
|
||||
EntityCount int
|
||||
BundleCount int
|
||||
LastEdit time.Time
|
||||
}
|
||||
|
||||
func (a *API) HandleRoute(ctx context.Context, path string) (http.Handler, error) {
|
||||
return &api.Handler[[]APISystem]{
|
||||
Config: a.Config,
|
||||
Auth: a.Dependencies.Auth,
|
||||
|
||||
Methods: []string{"GET"},
|
||||
Scope: scopes.ScopeInstanceDirectory,
|
||||
|
||||
Handler: func(s string, r *http.Request) ([]APISystem, error) {
|
||||
var statuses []status.WissKI
|
||||
if a.Dependencies.ListInstances.ShouldShowList(r) {
|
||||
statuses = a.Dependencies.ListInstances.infos.Get(nil)
|
||||
}
|
||||
|
||||
if len(statuses) == 0 {
|
||||
return []APISystem{}, nil
|
||||
}
|
||||
|
||||
infos := make([]APISystem, len(statuses))
|
||||
for i, status := range statuses {
|
||||
infos[i].Slug = status.Slug
|
||||
infos[i].URL = status.URL
|
||||
infos[i].EntityCount = status.Statistics.Bundles.TotalCount()
|
||||
infos[i].BundleCount = status.Statistics.Bundles.TotalBundles
|
||||
infos[i].LastEdit = status.Statistics.Bundles.LastEdit().Time
|
||||
}
|
||||
return infos, nil
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
130
internal/dis/component/server/list/list.go
Normal file
130
internal/dis/component/server/list/list.go
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
package list
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/status"
|
||||
"github.com/tkw1536/pkglib/lazy"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
// ListInstances holds information about all instances
|
||||
type ListInstances struct {
|
||||
component.Base
|
||||
|
||||
names lazy.Lazy[map[string]struct{}] // instance names
|
||||
infos lazy.Lazy[[]status.WissKI] // list of home instances (updated via cron)
|
||||
|
||||
Dependencies struct {
|
||||
Auth *auth.Auth
|
||||
Instances *instances.Instances
|
||||
}
|
||||
}
|
||||
|
||||
func (li *ListInstances) Names() map[string]struct{} {
|
||||
return li.names.Get(nil)
|
||||
}
|
||||
|
||||
func (li *ListInstances) Infos() []status.WissKI {
|
||||
return li.infos.Get(nil)
|
||||
}
|
||||
|
||||
// ShouldShowList determines if a list should be shown for the given request
|
||||
func (li *ListInstances) ShouldShowList(r *http.Request) bool {
|
||||
allowPrivate := li.Config.Home.List.Private.Value
|
||||
allowPublic := li.Config.Home.List.Public.Value
|
||||
|
||||
if allowPrivate == allowPublic {
|
||||
return allowPrivate
|
||||
}
|
||||
|
||||
user, _ := li.Dependencies.Auth.UserOf(r)
|
||||
if user == nil {
|
||||
return allowPublic
|
||||
} else {
|
||||
return allowPrivate
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
_ component.Cronable = (*ListInstances)(nil)
|
||||
)
|
||||
|
||||
func (li *ListInstances) TaskName() string {
|
||||
return "instance list and status"
|
||||
}
|
||||
|
||||
func (li *ListInstances) Cron(ctx context.Context) (err error) {
|
||||
{
|
||||
names, e := li.getNames(ctx)
|
||||
if err == nil {
|
||||
li.names.Set(names)
|
||||
} else {
|
||||
err = e
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
infos, e := li.getInfos(ctx)
|
||||
if err == nil {
|
||||
li.infos.Set(infos)
|
||||
} else {
|
||||
err = e
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// getNames returns the names of the given instances
|
||||
func (li *ListInstances) getNames(ctx context.Context) (map[string]struct{}, error) {
|
||||
wissKIs, err := li.Dependencies.Instances.All(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
names := make(map[string]struct{}, len(wissKIs))
|
||||
for _, w := range wissKIs {
|
||||
names[w.Slug] = struct{}{}
|
||||
}
|
||||
return names, nil
|
||||
}
|
||||
|
||||
// getInfos returns the names of the given instances
|
||||
func (li *ListInstances) getInfos(ctx context.Context) ([]status.WissKI, error) {
|
||||
// find all the WissKIs
|
||||
wissKIs, err := li.Dependencies.Instances.All(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
infos := make([]status.WissKI, len(wissKIs))
|
||||
|
||||
// determine their infos
|
||||
var eg errgroup.Group
|
||||
for i, instance := range wissKIs {
|
||||
i := i
|
||||
wissKI := instance
|
||||
eg.Go(func() (err error) {
|
||||
infos[i], err = wissKI.Info().Information(ctx, false)
|
||||
return
|
||||
})
|
||||
}
|
||||
eg.Wait()
|
||||
|
||||
// filter them by those that are running and do not have prefixes excluded
|
||||
infosF := infos[:0]
|
||||
for _, info := range infos {
|
||||
if info.NoPrefixes || !info.Running {
|
||||
continue
|
||||
}
|
||||
infosF = append(infosF, info)
|
||||
}
|
||||
|
||||
// and return them
|
||||
return infos[:len(infosF):len(infosF)], nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue