Rename info -> admin

This commit is contained in:
Tom Wiesing 2023-01-04 12:53:02 +01:00
parent 785130dc36
commit 11f7749c1d
No known key found for this signature in database
10 changed files with 55 additions and 55 deletions

View file

@ -1,4 +1,4 @@
package info
package admin
import (
"context"
@ -15,7 +15,7 @@ import (
"github.com/FAU-CDI/wisski-distillery/pkg/lazy"
)
type Info struct {
type Admin struct {
component.Base
Dependencies struct {
Fetchers []component.DistilleryFetcher
@ -31,13 +31,13 @@ type Info struct {
}
var (
_ component.DistilleryFetcher = (*Info)(nil)
_ component.Routeable = (*Info)(nil)
_ component.DistilleryFetcher = (*Admin)(nil)
_ component.Routeable = (*Admin)(nil)
)
func (*Info) Routes() []string { return []string{"/admin/"} }
func (*Admin) Routes() []string { return []string{"/admin/"} }
func (info *Info) HandleRoute(ctx context.Context, route string) (handler http.Handler, err error) {
func (admin *Admin) HandleRoute(ctx context.Context, route string) (handler http.Handler, err error) {
router := httprouter.New()
@ -45,9 +45,9 @@ func (info *Info) HandleRoute(ctx context.Context, route string) (handler http.H
socket := &httpx.WebSocket{
Context: ctx,
Fallback: router,
Handler: info.serveSocket,
Handler: admin.serveSocket,
}
handler = info.Dependencies.Auth.Protect(socket, auth.Admin)
handler = admin.Dependencies.Auth.Protect(socket, auth.Admin)
}
// handle everything
@ -57,25 +57,25 @@ func (info *Info) HandleRoute(ctx context.Context, route string) (handler http.H
// add a handler for the index page
router.Handler(http.MethodGet, route+"index", httpx.HTMLHandler[indexContext]{
Handler: info.index,
Handler: admin.index,
Template: indexTemplate,
})
// add a handler for the component page
router.Handler(http.MethodGet, route+"components", httpx.HTMLHandler[componentContext]{
Handler: info.components,
Handler: admin.components,
Template: componentsTemplate,
})
// add a handler for the component page
router.Handler(http.MethodGet, route+"ingredients/:slug", httpx.HTMLHandler[ingredientsContext]{
Handler: info.ingredients,
Handler: admin.ingredients,
Template: ingredientsTemplate,
})
// add a handler for the instance page
router.Handler(http.MethodGet, route+"instance/:slug", httpx.HTMLHandler[instanceContext]{
Handler: info.instance,
Handler: admin.instance,
Template: instanceTemplate,
})
@ -86,7 +86,7 @@ func (info *Info) HandleRoute(ctx context.Context, route string) (handler http.H
}
// get the instance
instance, err := info.Dependencies.Instances.WissKI(r.Context(), r.PostFormValue("slug"))
instance, err := admin.Dependencies.Instances.WissKI(r.Context(), r.PostFormValue("slug"))
if err != nil {
return "", 0, httpx.ErrNotFound
}

View file

@ -1,4 +1,4 @@
package info
package admin
import (
"net/http"
@ -27,8 +27,8 @@ type componentContext struct {
Analytics lazy.PoolAnalytics
}
func (info *Info) components(r *http.Request) (cp componentContext, err error) {
cp.Analytics = *info.Analytics
func (admin *Admin) components(r *http.Request) (cp componentContext, err error) {
cp.Analytics = *admin.Analytics
cp.Time = time.Now().UTC()
return
@ -48,11 +48,11 @@ type ingredientsContext struct {
Analytics *lazy.PoolAnalytics
}
func (info *Info) ingredients(r *http.Request) (cp ingredientsContext, err error) {
func (admin *Admin) ingredients(r *http.Request) (cp ingredientsContext, err error) {
cp.Time = time.Now().UTC()
// find the instance itself!
instance, err := info.Dependencies.Instances.WissKI(r.Context(), mux.Vars(r)["slug"])
instance, err := admin.Dependencies.Instances.WissKI(r.Context(), mux.Vars(r)["slug"])
if err == instances.ErrWissKINotFound {
return cp, httpx.ErrNotFound
}

View file

@ -1,4 +1,4 @@
package info
package admin
import (
"context"
@ -20,24 +20,14 @@ var indexTemplate = static.AssetsControlIndex.MustParseShared(
indexTemplateStr,
)
type indexContext struct {
status.Distillery
Instances []status.WissKI
}
func (info *Info) index(r *http.Request) (idx indexContext, err error) {
idx.Distillery, idx.Instances, err = info.Status(r.Context(), true)
return
}
// Status produces a new observation of the distillery, and a new information of all instances
// The information on all instances is passed the given quick flag.
func (info *Info) Status(ctx context.Context, QuickInformation bool) (target status.Distillery, information []status.WissKI, err error) {
func (admin *Admin) Status(ctx context.Context, QuickInformation bool) (target status.Distillery, information []status.WissKI, err error) {
var group errgroup.Group
group.Go(func() error {
// list all the instances
all, err := info.Dependencies.Instances.All(ctx)
all, err := admin.Dependencies.Instances.All(ctx)
if err != nil {
return err
}
@ -63,7 +53,7 @@ func (info *Info) Status(ctx context.Context, QuickInformation bool) (target sta
flags := component.FetcherFlags{
Context: ctx,
}
for _, o := range info.Dependencies.Fetchers {
for _, o := range admin.Dependencies.Fetchers {
o := o
group.Go(func() error {
return o.Fetch(flags, &target)
@ -88,8 +78,18 @@ func (info *Info) Status(ctx context.Context, QuickInformation bool) (target sta
return
}
func (nfo *Info) Fetch(flags component.FetcherFlags, target *status.Distillery) error {
type indexContext struct {
status.Distillery
Instances []status.WissKI
}
func (admin *Admin) index(r *http.Request) (idx indexContext, err error) {
idx.Distillery, idx.Instances, err = admin.Status(r.Context(), true)
return
}
func (admin *Admin) Fetch(flags component.FetcherFlags, target *status.Distillery) error {
target.Time = time.Now().UTC()
target.Config = nfo.Config
target.Config = admin.Config
return nil
}

View file

@ -1,4 +1,4 @@
package info
package admin
import (
_ "embed"
@ -27,9 +27,9 @@ type instanceContext struct {
Info status.WissKI
}
func (info *Info) instance(r *http.Request) (is instanceContext, err error) {
func (admin *Admin) instance(r *http.Request) (is instanceContext, err error) {
// find the instance itself!
instance, err := info.Dependencies.Instances.WissKI(r.Context(), mux.Vars(r)["slug"])
instance, err := admin.Dependencies.Instances.WissKI(r.Context(), mux.Vars(r)["slug"])
if err == instances.ErrWissKINotFound {
return is, httpx.ErrNotFound
}

View file

@ -1,4 +1,4 @@
package info
package admin
import (
"context"
@ -16,13 +16,13 @@ import (
type InstanceAction struct {
NumParams int
HandleInteractive func(ctx context.Context, info *Info, instance *wisski.WissKI, out io.Writer, params ...string) error
HandleResult func(ctx context.Context, info *Info, instance *wisski.WissKI, params ...string) (value any, err error)
HandleInteractive func(ctx context.Context, info *Admin, instance *wisski.WissKI, out io.Writer, params ...string) error
HandleResult func(ctx context.Context, info *Admin, instance *wisski.WissKI, params ...string) (value any, err error)
}
var socketInstanceActions = map[string]InstanceAction{
"snapshot": {
HandleInteractive: func(ctx context.Context, info *Info, instance *wisski.WissKI, out io.Writer, params ...string) error {
HandleInteractive: func(ctx context.Context, info *Admin, instance *wisski.WissKI, out io.Writer, params ...string) error {
return info.Dependencies.Exporter.MakeExport(
ctx,
out,
@ -36,23 +36,23 @@ var socketInstanceActions = map[string]InstanceAction{
},
},
"rebuild": {
HandleInteractive: func(ctx context.Context, _ *Info, instance *wisski.WissKI, out io.Writer, params ...string) error {
HandleInteractive: func(ctx context.Context, _ *Admin, instance *wisski.WissKI, out io.Writer, params ...string) error {
return instance.Barrel().Build(ctx, out, true)
},
},
"update": {
HandleInteractive: func(ctx context.Context, _ *Info, instance *wisski.WissKI, out io.Writer, params ...string) error {
HandleInteractive: func(ctx context.Context, _ *Admin, instance *wisski.WissKI, out io.Writer, params ...string) error {
return instance.Drush().Update(ctx, out)
},
},
"cron": {
HandleInteractive: func(ctx context.Context, _ *Info, instance *wisski.WissKI, str io.Writer, params ...string) error {
HandleInteractive: func(ctx context.Context, _ *Admin, instance *wisski.WissKI, str io.Writer, params ...string) error {
return instance.Drush().Cron(ctx, str)
},
},
}
func (info *Info) serveSocket(conn httpx.WebSocketConnection) {
func (admin *Admin) serveSocket(conn httpx.WebSocketConnection) {
// read the next message to act on
message, ok := <-conn.Read()
if !ok {
@ -61,14 +61,14 @@ func (info *Info) serveSocket(conn httpx.WebSocketConnection) {
// perform an action if it exists!
if action, ok := socketInstanceActions[string(message.Bytes)]; ok {
info.handleInstanceAction(conn, action)
admin.handleInstanceAction(conn, action)
return
}
}
var instanceParamsTimeout = time.Second
func (info *Info) handleInstanceAction(conn httpx.WebSocketConnection, action InstanceAction) {
func (admin *Admin) handleInstanceAction(conn httpx.WebSocketConnection, action InstanceAction) {
// read the slug
slug, ok := <-conn.Read()
@ -78,7 +78,7 @@ func (info *Info) handleInstanceAction(conn httpx.WebSocketConnection, action In
}
// resolve the instance
instance, err := info.Dependencies.Instances.WissKI(conn.Context(), string(slug.Bytes))
instance, err := admin.Dependencies.Instances.WissKI(conn.Context(), string(slug.Bytes))
if err != nil {
<-conn.WriteText("Instance not found")
return
@ -111,7 +111,7 @@ func (info *Info) handleInstanceAction(conn httpx.WebSocketConnection, action In
// handle the interactive action
if action.HandleInteractive != nil {
err := action.HandleInteractive(conn.Context(), info, instance, writer, params...)
err := action.HandleInteractive(conn.Context(), admin, instance, writer, params...)
if err != nil {
fmt.Fprintln(writer, err)
return
@ -121,7 +121,7 @@ func (info *Info) handleInstanceAction(conn httpx.WebSocketConnection, action In
// handle the result computation
if action.HandleResult != nil {
result, err := action.HandleResult(conn.Context(), info, instance, params...)
result, err := action.HandleResult(conn.Context(), admin, instance, params...)
if err != nil {
fmt.Fprintln(writer, "false")
return