/
api.go
71 lines (60 loc) · 1.74 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
}