Implement basic API scoping
This commit is contained in:
parent
064ae2f564
commit
9db53d39c4
21 changed files with 519 additions and 264 deletions
|
|
@ -1,64 +0,0 @@
|
|||
package home
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/status"
|
||||
"github.com/tkw1536/pkglib/httpx"
|
||||
)
|
||||
|
||||
type API struct {
|
||||
component.Base
|
||||
|
||||
Dependencies struct {
|
||||
Home *Home
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
_ component.Routeable = (*API)(nil)
|
||||
)
|
||||
|
||||
func (api *API) Routes() component.Routes {
|
||||
return component.Routes{
|
||||
Prefix: "/api/v1/systems",
|
||||
Exact: true,
|
||||
Decorator: api.Config.HTTP.APIDecorator("GET"),
|
||||
}
|
||||
}
|
||||
|
||||
type APISystemInfo struct {
|
||||
Slug string
|
||||
URL string
|
||||
Tagline string
|
||||
|
||||
EntityCount int
|
||||
BundleCount int
|
||||
LastEdit time.Time
|
||||
}
|
||||
|
||||
func (api *API) HandleRoute(ctx context.Context, path string) (http.Handler, error) {
|
||||
return httpx.JSON(func(r *http.Request) ([]APISystemInfo, error) {
|
||||
var statuses []status.WissKI
|
||||
if api.Dependencies.Home.ShouldShowList(r) {
|
||||
statuses = api.Dependencies.Home.homeInstances.Get(nil)
|
||||
}
|
||||
|
||||
if len(statuses) == 0 {
|
||||
return []APISystemInfo{}, nil
|
||||
}
|
||||
|
||||
infos := make([]APISystemInfo, 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
|
||||
}
|
||||
|
|
@ -6,23 +6,16 @@ import (
|
|||
"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/dis/component/server/list"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templating"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/status"
|
||||
"github.com/tkw1536/pkglib/lazy"
|
||||
)
|
||||
|
||||
type Home struct {
|
||||
component.Base
|
||||
Dependencies struct {
|
||||
Templating *templating.Templating
|
||||
Instances *instances.Instances
|
||||
Auth *auth.Auth
|
||||
ListInstances *list.ListInstances
|
||||
Templating *templating.Templating
|
||||
}
|
||||
|
||||
instanceNames lazy.Lazy[map[string]struct{}] // instance names
|
||||
homeInstances lazy.Lazy[[]status.WissKI] // list of home instances (updated via cron)
|
||||
}
|
||||
|
||||
var (
|
||||
|
|
@ -61,21 +54,8 @@ func (home *Home) HandleRoute(ctx context.Context, route string) (http.Handler,
|
|||
}), nil
|
||||
}
|
||||
|
||||
func (home *Home) instanceMap(ctx context.Context) (map[string]struct{}, error) {
|
||||
wissKIs, err := home.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
|
||||
}
|
||||
|
||||
func (home *Home) serveWissKI(w http.ResponseWriter, slug string, r *http.Request) {
|
||||
if _, ok := home.instanceNames.Get(nil)[slug]; !ok {
|
||||
if _, ok := home.Dependencies.ListInstances.Names()[slug]; !ok {
|
||||
// Get(nil) guaranteed to work by precondition
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
fmt.Fprintf(w, "WissKI %q not found\n", slug)
|
||||
|
|
|
|||
|
|
@ -1,86 +0,0 @@
|
|||
package home
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/status"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
// loadInstances loads all the instances into the home route
|
||||
func (home *Home) loadInstances(ctx context.Context) ([]status.WissKI, error) {
|
||||
// find all the WissKIs
|
||||
wissKIs, err := home.Dependencies.Instances.All(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
instances := 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) {
|
||||
instances[i], err = wissKI.Info().Information(ctx, false)
|
||||
return
|
||||
})
|
||||
}
|
||||
eg.Wait()
|
||||
|
||||
// and return the new instances
|
||||
return instances, nil
|
||||
}
|
||||
|
||||
// UpdateInstanceList updates the instances list of the home struct
|
||||
type UpdateInstanceList struct {
|
||||
component.Base
|
||||
Dependencies struct {
|
||||
Home *Home
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
_ component.Cronable = (*UpdateInstanceList)(nil)
|
||||
)
|
||||
|
||||
func (*UpdateInstanceList) TaskName() string {
|
||||
return "instance list"
|
||||
}
|
||||
|
||||
func (ul *UpdateInstanceList) Cron(ctx context.Context) error {
|
||||
names, err := ul.Dependencies.Home.instanceMap(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ul.Dependencies.Home.instanceNames.Set(names)
|
||||
return nil
|
||||
}
|
||||
|
||||
type UpdateHome struct {
|
||||
component.Base
|
||||
Dependencies struct {
|
||||
Home *Home
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
_ component.Cronable = (*UpdateHome)(nil)
|
||||
)
|
||||
|
||||
func (ur *UpdateHome) TaskName() string {
|
||||
return "home instances fetch"
|
||||
}
|
||||
|
||||
func (ur *UpdateHome) Cron(ctx context.Context) error {
|
||||
instances, err := ur.Dependencies.Home.loadInstances(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ur.Dependencies.Home.homeInstances.Set(instances)
|
||||
return nil
|
||||
}
|
||||
|
|
@ -47,23 +47,6 @@ type publicContext struct {
|
|||
|
||||
const logoHTML = template.HTML(`<img src="/logo.svg" alt="WissKI Distillery Logo" class="biglogo">`)
|
||||
|
||||
// ShouldShowList determines if the given request should show a WissKI list
|
||||
func (home *Home) ShouldShowList(r *http.Request) bool {
|
||||
allowPrivate := home.Config.Home.List.Private.Value
|
||||
allowPublic := home.Config.Home.List.Public.Value
|
||||
|
||||
if allowPrivate == allowPublic {
|
||||
return allowPrivate
|
||||
}
|
||||
|
||||
user, _ := home.Dependencies.Auth.UserOf(r)
|
||||
if user == nil {
|
||||
return allowPublic
|
||||
} else {
|
||||
return allowPrivate
|
||||
}
|
||||
}
|
||||
|
||||
func (home *Home) publicHandler(ctx context.Context) http.Handler {
|
||||
title := home.Config.Home.Title
|
||||
|
||||
|
|
@ -89,7 +72,7 @@ func (home *Home) publicHandler(ctx context.Context) http.Handler {
|
|||
|
||||
// prepare about
|
||||
pc.aboutContext.Logo = logoHTML
|
||||
pc.aboutContext.Instances = home.homeInstances.Get(nil)
|
||||
pc.aboutContext.Instances = home.Dependencies.ListInstances.Infos()
|
||||
pc.aboutContext.SelfRedirect = home.Config.Home.SelfRedirect.String()
|
||||
|
||||
// render the about template
|
||||
|
|
@ -102,7 +85,7 @@ func (home *Home) publicHandler(ctx context.Context) http.Handler {
|
|||
pc.About = template.HTML(builder.String())
|
||||
|
||||
// check if we should show the list of WissKIs
|
||||
pc.ListEnabled = home.ShouldShowList(r)
|
||||
pc.ListEnabled = home.Dependencies.ListInstances.ShouldShowList(r)
|
||||
|
||||
// title of the list
|
||||
pc.ListTitle = home.Config.Home.List.Title
|
||||
|
|
|
|||
|
|
@ -6,22 +6,20 @@
|
|||
</div>
|
||||
|
||||
{{range .Instances}}
|
||||
{{ if and .Running (not .NoPrefixes) }}
|
||||
<div class="pure-u-1 pure-u-md-1-3">
|
||||
<h3>{{.Slug}}</h3>
|
||||
<p>
|
||||
<a href="{{.URL}}" target="_blank" rel="noopener noreferrer" class="wisskilink">{{.URL}}</a><br>
|
||||
<small>
|
||||
{{ .Statistics.Bundles.Summary }}
|
||||
<div class="pure-u-1 pure-u-md-1-3">
|
||||
<h3>{{.Slug}}</h3>
|
||||
<p>
|
||||
<a href="{{.URL}}" target="_blank" rel="noopener noreferrer" class="wisskilink">{{.URL}}</a><br>
|
||||
<small>
|
||||
{{ .Statistics.Bundles.Summary }}
|
||||
|
||||
{{ $edit := .Statistics.Bundles.LastEdit }}
|
||||
{{ if $edit.Valid }}
|
||||
<br />
|
||||
last edited {{ $edit.Time.Format "2006-01-02T15:04:05Z07:00" }}
|
||||
{{ end }}
|
||||
</small>
|
||||
</p>
|
||||
</div>
|
||||
{{ end }}
|
||||
{{ $edit := .Statistics.Bundles.LastEdit }}
|
||||
{{ if $edit.Valid }}
|
||||
<br />
|
||||
last edited {{ $edit.Time.Format "2006-01-02T15:04:05Z07:00" }}
|
||||
{{ end }}
|
||||
</small>
|
||||
</p>
|
||||
</div>
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
Loading…
Add table
Add a link
Reference in a new issue