From ff92df3a87bbaa26d87e0360780fdc099c422c70 Mon Sep 17 00:00:00 2001 From: Tom Wiesing Date: Fri, 10 Nov 2023 18:31:35 +0100 Subject: [PATCH] Move snapshots to separate page --- internal/dis/component/server/admin/admin.go | 6 ++ .../component/server/admin/html/instance.html | 35 ---------- .../server/admin/html/instance_snapshots.html | 36 ++++++++++ .../dis/component/server/admin/instance.go | 2 +- .../server/admin/instance_snapshots.go | 66 +++++++++++++++++++ 5 files changed, 109 insertions(+), 36 deletions(-) create mode 100644 internal/dis/component/server/admin/html/instance_snapshots.html create mode 100644 internal/dis/component/server/admin/instance_snapshots.go diff --git a/internal/dis/component/server/admin/admin.go b/internal/dis/component/server/admin/admin.go index ad00a47..128db04 100644 --- a/internal/dis/component/server/admin/admin.go +++ b/internal/dis/component/server/admin/admin.go @@ -74,6 +74,7 @@ var ( menuRebuild = component.DummyMenuItem() menuGrants = component.DummyMenuItem() menuPurge = component.DummyMenuItem() + menuSnapshots = component.DummyMenuItem() ) func (admin *Admin) HandleRoute(ctx context.Context, route string) (handler http.Handler, err error) { @@ -139,6 +140,11 @@ func (admin *Admin) HandleRoute(ctx context.Context, route string) (handler http router.Handler(http.MethodGet, route+"instance/:slug/purge", purge) } + { + snapshots := admin.instanceSnapshots(ctx) + router.Handler(http.MethodGet, route+"instance/:slug/snapshots", snapshots) + } + // add a router for the login page router.Handler(http.MethodPost, route+"login", admin.loginHandler(ctx)) diff --git a/internal/dis/component/server/admin/html/instance.html b/internal/dis/component/server/admin/html/instance.html index 6b06c62..1efa196 100644 --- a/internal/dis/component/server/admin/html/instance.html +++ b/internal/dis/component/server/admin/html/instance.html @@ -9,7 +9,6 @@ WissKI Data Statistics SSH Keys - Snapshots @@ -514,37 +513,3 @@ - -
-

Snapshots

-

- -

-
- -
- - - - - - - - - - {{ range .Info.Snapshots }} - - - - - - {{ end}} - -
PathCreatedPacked
- {{ .Path }} - - {{ .Created.Format "2006-01-02T15:04:05Z07:00" }} - - {{ .Packed }} -
-
\ No newline at end of file diff --git a/internal/dis/component/server/admin/html/instance_snapshots.html b/internal/dis/component/server/admin/html/instance_snapshots.html new file mode 100644 index 0000000..9dfb72c --- /dev/null +++ b/internal/dis/component/server/admin/html/instance_snapshots.html @@ -0,0 +1,36 @@ +
+

+ Taking a snapshot makes a backup of the entire instance, including triplestore and graph database. + Currently snapshot can only be accessed on the server filesystem. +

+

+ +

+
+ +
+ + + + + + + + + + {{ range .Snapshots }} + + + + + + {{ end}} + +
PathCreatedPacked
+ {{ .Path }} + + {{ .Created.Format "2006-01-02T15:04:05Z07:00" }} + + {{ .Packed }} +
+
\ No newline at end of file diff --git a/internal/dis/component/server/admin/instance.go b/internal/dis/component/server/admin/instance.go index 0b961e0..aaea17c 100644 --- a/internal/dis/component/server/admin/instance.go +++ b/internal/dis/component/server/admin/instance.go @@ -79,6 +79,7 @@ func (admin *Admin) instanceTabs(slug string, active string) templating.FlagFunc {Title: "Overview", Path: template.URL("/admin/instance/" + slug), Active: active == "overview"}, {Title: "Rebuild", Path: template.URL("/admin/instance/" + slug + "/rebuild"), Active: active == "rebuild"}, {Title: "Users & Grants", Path: template.URL("/admin/instance/" + slug + "/users"), Active: active == "users"}, + {Title: "Snapshots", Path: template.URL("/admin/instance/" + slug + "/snapshots"), Active: active == "snapshots"}, {Title: "Purge", Path: template.URL("/admin/instance/" + slug + "/purge"), Active: active == "purge"}, // TODO: These still need to be migrated to their own tabs @@ -90,7 +91,6 @@ func (admin *Admin) instanceTabs(slug string, active string) templating.FlagFunc {Title: "Users & Grants", Path: template.URL("/instance/" + slug + "/users"), Active: active == "users"}, {Title: "Stats", Path: template.URL("/instance/" + slug + "/stats"), Active: active == "stats"}, {Title: "SSH", Path: template.URL("/instance/" + slug + "/ssh"), Active: active == "ssh"}, - {Title: "Snapshots", Path: template.URL("/instance/" + slug + "/snapshots"), Active: active == "snapshots"}, */ } return flags diff --git a/internal/dis/component/server/admin/instance_snapshots.go b/internal/dis/component/server/admin/instance_snapshots.go new file mode 100644 index 0000000..b266644 --- /dev/null +++ b/internal/dis/component/server/admin/instance_snapshots.go @@ -0,0 +1,66 @@ +package admin + +import ( + "context" + _ "embed" + "html/template" + "net/http" + + "github.com/FAU-CDI/wisski-distillery/internal/dis/component" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templating" + "github.com/FAU-CDI/wisski-distillery/internal/models" + "github.com/FAU-CDI/wisski-distillery/internal/wisski" + "github.com/tkw1536/pkglib/httpx" + + "github.com/julienschmidt/httprouter" +) + +//go:embed "html/instance_snapshots.html" +var instanceSnapshotsHTML []byte +var instanceSnapshotsTemplate = templating.Parse[instanceSnapshotsContext]( + "instance_snapshots.html", instanceSnapshotsHTML, nil, + + templating.Assets(assets.AssetsAdmin), +) + +type instanceSnapshotsContext struct { + templating.RuntimeFlags + + Instance *wisski.WissKI + Snapshots []models.Export +} + +func (admin *Admin) instanceSnapshots(ctx context.Context) http.Handler { + tpl := instanceSnapshotsTemplate.Prepare( + admin.dependencies.Templating, + templating.Crumbs( + menuAdmin, + menuInstances, + menuInstance, + menuSnapshots, + ), + ) + + return tpl.HTMLHandlerWithFlags(func(r *http.Request) (ctx instanceSnapshotsContext, funcs []templating.FlagFunc, err error) { + slug := httprouter.ParamsFromContext(r.Context()).ByName("slug") + + // setup the context with just the instance + ctx.Instance, err = admin.dependencies.Instances.WissKI(r.Context(), slug) + if err != nil { + return ctx, nil, httpx.ErrNotFound + } + + ctx.Snapshots, err = ctx.Instance.Snapshots(r.Context()) + if err != nil { + return ctx, nil, httpx.ErrInternalServerError + } + + return ctx, []templating.FlagFunc{ + templating.ReplaceCrumb(menuInstance, component.MenuItem{Title: "Instance", Path: template.URL("/admin/instance/" + ctx.Instance.Slug)}), + templating.ReplaceCrumb(menuSnapshots, component.MenuItem{Title: "Snapshots", Path: template.URL("/admin/instance/" + ctx.Instance.Slug + "/snapshots")}), + templating.Title(ctx.Instance.Slug + " - Snapshots"), + admin.instanceTabs(slug, "snapshots"), + }, nil + }) +}