From 6ede99d7c68549f51f4ecc457aa07204f4ed68cd Mon Sep 17 00:00:00 2001 From: Tom Wiesing Date: Thu, 19 Jan 2023 13:22:48 +0100 Subject: [PATCH] Refactor server and templates package --- .tool-versions | 2 +- Makefile | 6 +- cmd/license.go | 4 +- cmd/system_update.go | 2 +- internal/cli/cli_notices.go | 4 +- internal/dis/component/auth/auth.go | 4 +- internal/dis/component/auth/panel/panel.go | 22 +++--- internal/dis/component/auth/panel/password.go | 8 +-- internal/dis/component/auth/panel/ssh.go | 16 ++--- internal/dis/component/auth/panel/totp.go | 16 ++--- internal/dis/component/auth/panel/user.go | 12 ++-- internal/dis/component/auth/session.go | 18 ++--- internal/dis/component/control/control.go | 71 ------------------- .../component/control/static/assets_dist.go | 26 ------- .../component/control/static/custom/assets.go | 22 ------ internal/dis/component/resolver/resolver.go | 16 ++--- .../{control => server}/admin/admin.go | 4 +- .../{control => server}/admin/components.go | 18 ++--- .../{control => server}/admin/grants.go | 18 ++--- .../admin/html/components.html | 0 .../admin/html/grants.html | 0 .../{control => server}/admin/html/index.html | 0 .../admin/html/ingredients.html | 0 .../admin/html/instance.html | 0 .../admin/html/user_create.html | 0 .../{control => server}/admin/html/users.html | 0 .../{control => server}/admin/index.go | 12 ++-- .../{control => server}/admin/instance.go | 12 ++-- .../{control => server}/admin/socket.go | 0 .../{control => server}/admin/users.go | 16 ++--- .../static => server/assets}/assets.go | 4 +- .../assets}/assets_disclaimer.txt | 0 .../component/server/assets/assets_dist.go | 29 ++++++++ .../static => server/assets}/build.mjs | 7 +- .../assets}/dist/Admin.1a380f6f.js | 0 .../assets}/dist/Admin.6d2ae968.css | 0 .../assets}/dist/Admin.6d59e220.css | 0 .../assets}/dist/Admin.cb58d290.js | 0 .../assets}/dist/Default.38d394c2.js | 0 .../assets}/dist/Default.db26a303.css | 0 .../assets}/dist/Default.f9675eae.css | 0 .../assets}/dist/User.68febbf8.css | 0 .../assets}/dist/User.840de3b4.css | 0 .../assets}/dist/User.b2f9a57c.js | 0 .../assets}/dist/User.e0367d79.js | 0 .../static => server/assets}/package.json | 0 .../assets}/src/base/index.css | 0 .../assets}/src/base/index.ts | 0 .../assets}/src/entry/Admin/index.css | 0 .../assets}/src/entry/Admin/index.ts | 0 .../assets}/src/entry/Default/index.css | 0 .../assets}/src/entry/Default/index.ts | 0 .../assets}/src/entry/User/index.css | 0 .../assets}/src/entry/User/index.ts | 0 .../assets}/src/lib/autolink/index.css | 0 .../assets}/src/lib/autolink/index.ts | 0 .../assets}/src/lib/copy/index.css | 0 .../assets}/src/lib/copy/index.ts | 0 .../assets}/src/lib/highlight/index.ts | 0 .../assets}/src/lib/remote/index.css | 0 .../assets}/src/lib/remote/index.ts | 0 .../assets}/src/lib/remote/socket.ts | 0 .../static => server/assets}/static.go | 4 +- .../static => server/assets}/templates.go | 9 +-- .../assets}/templates/_anal.html | 0 .../assets}/templates/_base.html | 2 +- .../assets}/templates/_form.html | 0 .../static => server/assets}/tsconfig.json | 0 .../static => server/assets}/yarn.lock | 0 .../component/{control => server}/cookies.go | 2 +- .../{control => server}/cron/cron.go | 0 .../{control => server}/home/home.go | 6 +- .../{control => server}/home/instances.go | 0 .../{control => server}/home/public.go | 10 +-- .../{control => server}/home/public.html | 0 .../{control => server}/home/redirect.go | 0 .../{control => server}/legal/legal.go | 22 +++--- .../{control => server}/legal/legal.html | 0 .../news/NEWS/2022-09-08-go.md | 0 .../news/NEWS/2022-09-09-admin.md | 0 .../news/NEWS/2022-10-05-resolver.md | 0 .../news/NEWS/2022-10-12-http3.md | 0 .../news/NEWS/2022-11-12-ssh.md | 0 .../news/NEWS/2022-11-16-statistics.md | 0 .../news/NEWS/2022-11-23-login.md | 0 .../news/NEWS/2022-11-25-password.md | 0 .../news/NEWS/2023-01-07-management.md | 0 .../news/NEWS/2023-01-09-news.md | 0 .../news/NEWS/2023-01-15-ssh.md | 0 .../news/NEWS/2023-01-16-remove.md | 0 .../{control => server}/news/news.go | 12 ++-- .../{control => server}/news/news.html | 0 .../control.env => server/server.env} | 0 .../component/{control => server}/server.go | 32 ++++++--- .../control => server/server}/Dockerfile | 0 .../server}/docker-compose.yml | 0 internal/dis/component/server/stack.go | 55 ++++++++++++++ .../dis/component/server/templates/assets.go | 22 ++++++ .../custom => server/templates}/context.go | 11 ++- .../custom => server/templates}/footer.html | 0 .../custom => server/templates}/menu.go | 48 +++++++------ .../static/custom => server/templates}/new.go | 16 ++--- .../custom => server/templates}/template.go | 20 +++--- .../templates/templating.go} | 10 +-- internal/dis/distillery.go | 30 ++++---- 105 files changed, 341 insertions(+), 339 deletions(-) delete mode 100644 internal/dis/component/control/control.go delete mode 100644 internal/dis/component/control/static/assets_dist.go delete mode 100644 internal/dis/component/control/static/custom/assets.go rename internal/dis/component/{control => server}/admin/admin.go (97%) rename internal/dis/component/{control => server}/admin/components.go (72%) rename internal/dis/component/{control => server}/admin/grants.go (82%) rename internal/dis/component/{control => server}/admin/html/components.html (100%) rename internal/dis/component/{control => server}/admin/html/grants.html (100%) rename internal/dis/component/{control => server}/admin/html/index.html (100%) rename internal/dis/component/{control => server}/admin/html/ingredients.html (100%) rename internal/dis/component/{control => server}/admin/html/instance.html (100%) rename internal/dis/component/{control => server}/admin/html/user_create.html (100%) rename internal/dis/component/{control => server}/admin/html/users.html (100%) rename internal/dis/component/{control => server}/admin/index.go (82%) rename internal/dis/component/{control => server}/admin/instance.go (77%) rename internal/dis/component/{control => server}/admin/socket.go (100%) rename internal/dis/component/{control => server}/admin/users.go (92%) rename internal/dis/component/{control/static => server/assets}/assets.go (96%) rename internal/dis/component/{control/static => server/assets}/assets_disclaimer.txt (100%) create mode 100644 internal/dis/component/server/assets/assets_dist.go rename internal/dis/component/{control/static => server/assets}/build.mjs (96%) rename internal/dis/component/{control/static => server/assets}/dist/Admin.1a380f6f.js (100%) rename internal/dis/component/{control/static => server/assets}/dist/Admin.6d2ae968.css (100%) rename internal/dis/component/{control/static => server/assets}/dist/Admin.6d59e220.css (100%) rename internal/dis/component/{control/static => server/assets}/dist/Admin.cb58d290.js (100%) rename internal/dis/component/{control/static => server/assets}/dist/Default.38d394c2.js (100%) rename internal/dis/component/{control/static => server/assets}/dist/Default.db26a303.css (100%) rename internal/dis/component/{control/static => server/assets}/dist/Default.f9675eae.css (100%) rename internal/dis/component/{control/static => server/assets}/dist/User.68febbf8.css (100%) rename internal/dis/component/{control/static => server/assets}/dist/User.840de3b4.css (100%) rename internal/dis/component/{control/static => server/assets}/dist/User.b2f9a57c.js (100%) rename internal/dis/component/{control/static => server/assets}/dist/User.e0367d79.js (100%) rename internal/dis/component/{control/static => server/assets}/package.json (100%) rename internal/dis/component/{control/static => server/assets}/src/base/index.css (100%) rename internal/dis/component/{control/static => server/assets}/src/base/index.ts (100%) rename internal/dis/component/{control/static => server/assets}/src/entry/Admin/index.css (100%) rename internal/dis/component/{control/static => server/assets}/src/entry/Admin/index.ts (100%) rename internal/dis/component/{control/static => server/assets}/src/entry/Default/index.css (100%) rename internal/dis/component/{control/static => server/assets}/src/entry/Default/index.ts (100%) rename internal/dis/component/{control/static => server/assets}/src/entry/User/index.css (100%) rename internal/dis/component/{control/static => server/assets}/src/entry/User/index.ts (100%) rename internal/dis/component/{control/static => server/assets}/src/lib/autolink/index.css (100%) rename internal/dis/component/{control/static => server/assets}/src/lib/autolink/index.ts (100%) rename internal/dis/component/{control/static => server/assets}/src/lib/copy/index.css (100%) rename internal/dis/component/{control/static => server/assets}/src/lib/copy/index.ts (100%) rename internal/dis/component/{control/static => server/assets}/src/lib/highlight/index.ts (100%) rename internal/dis/component/{control/static => server/assets}/src/lib/remote/index.css (100%) rename internal/dis/component/{control/static => server/assets}/src/lib/remote/index.ts (100%) rename internal/dis/component/{control/static => server/assets}/src/lib/remote/socket.ts (100%) rename internal/dis/component/{control/static => server/assets}/static.go (94%) rename internal/dis/component/{control/static => server/assets}/templates.go (75%) rename internal/dis/component/{control/static => server/assets}/templates/_anal.html (100%) rename internal/dis/component/{control/static => server/assets}/templates/_base.html (96%) rename internal/dis/component/{control/static => server/assets}/templates/_form.html (100%) rename internal/dis/component/{control/static => server/assets}/tsconfig.json (100%) rename internal/dis/component/{control/static => server/assets}/yarn.lock (100%) rename internal/dis/component/{control => server}/cookies.go (96%) rename internal/dis/component/{control => server}/cron/cron.go (100%) rename internal/dis/component/{control => server}/home/home.go (93%) rename internal/dis/component/{control => server}/home/instances.go (100%) rename internal/dis/component/{control => server}/home/public.go (69%) rename internal/dis/component/{control => server}/home/public.html (100%) rename internal/dis/component/{control => server}/home/redirect.go (100%) rename internal/dis/component/{control => server}/legal/legal.go (59%) rename internal/dis/component/{control => server}/legal/legal.html (100%) rename internal/dis/component/{control => server}/news/NEWS/2022-09-08-go.md (100%) rename internal/dis/component/{control => server}/news/NEWS/2022-09-09-admin.md (100%) rename internal/dis/component/{control => server}/news/NEWS/2022-10-05-resolver.md (100%) rename internal/dis/component/{control => server}/news/NEWS/2022-10-12-http3.md (100%) rename internal/dis/component/{control => server}/news/NEWS/2022-11-12-ssh.md (100%) rename internal/dis/component/{control => server}/news/NEWS/2022-11-16-statistics.md (100%) rename internal/dis/component/{control => server}/news/NEWS/2022-11-23-login.md (100%) rename internal/dis/component/{control => server}/news/NEWS/2022-11-25-password.md (100%) rename internal/dis/component/{control => server}/news/NEWS/2023-01-07-management.md (100%) rename internal/dis/component/{control => server}/news/NEWS/2023-01-09-news.md (100%) rename internal/dis/component/{control => server}/news/NEWS/2023-01-15-ssh.md (100%) rename internal/dis/component/{control => server}/news/NEWS/2023-01-16-remove.md (100%) rename internal/dis/component/{control => server}/news/news.go (86%) rename internal/dis/component/{control => server}/news/news.html (100%) rename internal/dis/component/{control/control.env => server/server.env} (100%) rename internal/dis/component/{control => server}/server.go (78%) rename internal/dis/component/{control/control => server/server}/Dockerfile (100%) rename internal/dis/component/{control/control => server/server}/docker-compose.yml (100%) create mode 100644 internal/dis/component/server/stack.go create mode 100644 internal/dis/component/server/templates/assets.go rename internal/dis/component/{control/static/custom => server/templates}/context.go (88%) rename internal/dis/component/{control/static/custom => server/templates}/footer.html (100%) rename internal/dis/component/{control/static/custom => server/templates}/menu.go (56%) rename internal/dis/component/{control/static/custom => server/templates}/new.go (92%) rename internal/dis/component/{control/static/custom => server/templates}/template.go (69%) rename internal/dis/component/{control/static/custom/custom.go => server/templates/templating.go} (60%) diff --git a/.tool-versions b/.tool-versions index fe2801d..333af93 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1 +1 @@ -golang 1.19.2 +golang 1.19.5 \ No newline at end of file diff --git a/Makefile b/Makefile index acbe82e..cf67556 100644 --- a/Makefile +++ b/Makefile @@ -6,10 +6,10 @@ wdcli: go generate ./internal/dis/component/control/static/ go build -o ./wdcli ./cmd/wdcli -deps: internal/dis/component/control/static/node_modules +deps: internal/dis/component/server/assets/node_modules -internal/dis/component/control/static/node_modules: - cd internal/dis/component/control/static/ && yarn install +internal/dis/component/server/assets/node_modules: + cd internal/dis/component/server/assets/ && yarn install clean: rm wdcli \ No newline at end of file diff --git a/cmd/license.go b/cmd/license.go index b3eb567..7224caf 100644 --- a/cmd/license.go +++ b/cmd/license.go @@ -3,7 +3,7 @@ package cmd import ( wisski_distillery "github.com/FAU-CDI/wisski-distillery" "github.com/FAU-CDI/wisski-distillery/internal/cli" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets" ) // License is the 'wdcli license' command. @@ -28,7 +28,7 @@ func (license) AfterParse() error { } func (license) Run(context wisski_distillery.Context) error { - context.Printf(stringLicenseInfo, wisski_distillery.License, cli.LegalNotices, static.AssetsDisclaimer) + context.Printf(stringLicenseInfo, wisski_distillery.License, cli.LegalNotices, assets.Disclaimer) return nil } diff --git a/cmd/system_update.go b/cmd/system_update.go index e11819f..5e0af71 100644 --- a/cmd/system_update.go +++ b/cmd/system_update.go @@ -72,7 +72,7 @@ func (si systemupdate) Run(context wisski_distillery.Context) error { dis.Instances().Path(), dis.Exporter().StagingPath(), dis.Exporter().ArchivePath(), - dis.Custom().CustomAssetsPath(), + dis.Templating().CustomAssetsPath(), } { context.Println(d) if err := dis.Still.Environment.MkdirAll(d, environment.DefaultDirPerm); err != nil { diff --git a/internal/cli/cli_notices.go b/internal/cli/cli_notices.go index b6c986e..8d855b1 100755 --- a/internal/cli/cli_notices.go +++ b/internal/cli/cli_notices.go @@ -1,7 +1,7 @@ package cli // =========================================================================================================== -// This file was generated automatically at 16-01-2023 17:01:43 using gogenlicense. +// This file was generated automatically at 19-01-2023 12:17:46 using gogenlicense. // Do not edit manually, as changes may be overwritten. // =========================================================================================================== @@ -2417,7 +2417,7 @@ package cli // # Generation // // This variable and the associated documentation have been automatically generated using the 'gogenlicense' tool. -// It was last updated at 16-01-2023 17:01:43. +// It was last updated at 19-01-2023 12:17:46. var LegalNotices string func init() { diff --git a/internal/dis/component/auth/auth.go b/internal/dis/component/auth/auth.go index 7963883..d87bac5 100644 --- a/internal/dis/component/auth/auth.go +++ b/internal/dis/component/auth/auth.go @@ -5,7 +5,7 @@ import ( "net/http" "github.com/FAU-CDI/wisski-distillery/internal/dis/component" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static/custom" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates" "github.com/FAU-CDI/wisski-distillery/internal/dis/component/sql" "github.com/FAU-CDI/wisski-distillery/pkg/lazy" "github.com/gorilla/sessions" @@ -17,7 +17,7 @@ type Auth struct { Dependencies struct { SQL *sql.SQL UserDeleteHooks []component.UserDeleteHook - Custom *custom.Custom + Templating *templates.Templating } store lazy.Lazy[sessions.Store] diff --git a/internal/dis/component/auth/panel/panel.go b/internal/dis/component/auth/panel/panel.go index ea27728..5eddfb3 100644 --- a/internal/dis/component/auth/panel/panel.go +++ b/internal/dis/component/auth/panel/panel.go @@ -8,8 +8,8 @@ import ( "github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth" "github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth/next" "github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth/policy" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static/custom" "github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates" "github.com/FAU-CDI/wisski-distillery/internal/dis/component/ssh2/sshkeys" "github.com/FAU-CDI/wisski-distillery/internal/models" "github.com/FAU-CDI/wisski-distillery/pkg/httpx" @@ -19,12 +19,12 @@ import ( type UserPanel struct { component.Base Dependencies struct { - Auth *auth.Auth - Custom *custom.Custom - Policy *policy.Policy - Instances *instances.Instances - Next *next.Next - Keys *sshkeys.SSHKeys + Auth *auth.Auth + Templating *templates.Templating + Policy *policy.Policy + Instances *instances.Instances + Next *next.Next + Keys *sshkeys.SSHKeys } } @@ -106,14 +106,14 @@ func (panel *UserPanel) HandleRoute(ctx context.Context, route string) (http.Han } type userFormContext struct { - custom.BaseContext + templates.BaseContext httpx.FormContext User *models.User } -func (panel *UserPanel) UserFormContext2(tpl *custom.Template[userFormContext], last component.MenuItem, gaps ...custom.BaseContextGaps) func(ctx httpx.FormContext, r *http.Request) any { - var g custom.BaseContextGaps +func (panel *UserPanel) UserFormContext2(tpl *templates.Template[userFormContext], last component.MenuItem, gaps ...templates.BaseContextGaps) func(ctx httpx.FormContext, r *http.Request) any { + var g templates.BaseContextGaps if len(gaps) > 1 { panic("UserFormContext2: gaps must be of length 0 or 1") } @@ -125,7 +125,7 @@ func (panel *UserPanel) UserFormContext2(tpl *custom.Template[userFormContext], last, } - return custom.MappedHandler(tpl, func(ctx httpx.FormContext, r *http.Request) (userFormContext, custom.BaseContextGaps) { + return templates.MappedHandler(tpl, func(ctx httpx.FormContext, r *http.Request) (userFormContext, templates.BaseContextGaps) { uctx := userFormContext{FormContext: ctx} if user, err := panel.Dependencies.Auth.UserOf(r); err == nil { uctx.User = &user.User diff --git a/internal/dis/component/auth/panel/password.go b/internal/dis/component/auth/panel/password.go index 4376546..adf3e79 100644 --- a/internal/dis/component/auth/panel/password.go +++ b/internal/dis/component/auth/panel/password.go @@ -8,15 +8,15 @@ import ( _ "embed" "github.com/FAU-CDI/wisski-distillery/internal/dis/component" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static/custom" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets" + templating "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates" "github.com/FAU-CDI/wisski-distillery/pkg/httpx" "github.com/FAU-CDI/wisski-distillery/pkg/httpx/field" ) //go:embed "templates/password.html" var passwordHTML []byte -var passwordTemplate = custom.Parse[userFormContext]("password.html", passwordHTML, static.AssetsUser) +var passwordTemplate = templating.Parse[userFormContext]("password.html", passwordHTML, assets.AssetsUser) var ( errPasswordsNotIdentical = errors.New("passwords are not identical") @@ -28,7 +28,7 @@ var ( ) func (panel *UserPanel) routePassword(ctx context.Context) http.Handler { - tpl := passwordTemplate.Prepare(panel.Dependencies.Custom) + tpl := passwordTemplate.Prepare(panel.Dependencies.Templating) return &httpx.Form[struct{}]{ Fields: []field.Field{ diff --git a/internal/dis/component/auth/panel/ssh.go b/internal/dis/component/auth/panel/ssh.go index 62e8a15..e88c606 100644 --- a/internal/dis/component/auth/panel/ssh.go +++ b/internal/dis/component/auth/panel/ssh.go @@ -7,8 +7,8 @@ import ( "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/control/static" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static/custom" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates" "github.com/FAU-CDI/wisski-distillery/internal/models" "github.com/FAU-CDI/wisski-distillery/pkg/httpx" "github.com/FAU-CDI/wisski-distillery/pkg/httpx/field" @@ -22,10 +22,10 @@ import ( //go:embed "templates/ssh.html" var sshHTML []byte -var sshTemplate = custom.Parse[SSHTemplateContext]("ssh.html", sshHTML, static.AssetsUser) +var sshTemplate = templates.Parse[SSHTemplateContext]("ssh.html", sshHTML, assets.AssetsUser) type SSHTemplateContext struct { - custom.BaseContext + templates.BaseContext Keys []models.Keys @@ -37,7 +37,7 @@ type SSHTemplateContext struct { } func (panel *UserPanel) sshRoute(ctx context.Context) http.Handler { - tpl := sshTemplate.Prepare(panel.Dependencies.Custom, custom.BaseContextGaps{ + tpl := sshTemplate.Prepare(panel.Dependencies.Templating, templates.BaseContextGaps{ Crumbs: []component.MenuItem{ {Title: "User", Path: "/user/"}, {Title: "SSH Keys", Path: "/user/ssh/"}, @@ -114,7 +114,7 @@ func (panel *UserPanel) sshDeleteRoute(ctx context.Context) http.Handler { //go:embed "templates/ssh_add.html" var sshAddHTML []byte -var sshAddTemplate = custom.ParseForm("ssh_add.html", sshAddHTML, static.AssetsUser) +var sshAddTemplate = templates.ParseForm("ssh_add.html", sshAddHTML, assets.AssetsUser) type addKeyResult struct { User *auth.AuthUser @@ -123,7 +123,7 @@ type addKeyResult struct { } func (panel *UserPanel) sshAddRoute(ctx context.Context) http.Handler { - tpl := sshAddTemplate.Prepare(panel.Dependencies.Custom, custom.BaseContextGaps{ + tpl := sshAddTemplate.Prepare(panel.Dependencies.Templating, templates.BaseContextGaps{ Crumbs: []component.MenuItem{ {Title: "User", Path: "/user/"}, {Title: "SSH Keys", Path: "/user/ssh/"}, @@ -139,7 +139,7 @@ func (panel *UserPanel) sshAddRoute(ctx context.Context) http.Handler { FieldTemplate: field.PureCSSFieldTemplate, RenderTemplate: tpl.Template(), - RenderTemplateContext: custom.FormTemplateContext(tpl), + RenderTemplateContext: templates.FormTemplateContext(tpl), Validate: func(r *http.Request, values map[string]string) (ak addKeyResult, err error) { ak.User, err = panel.Dependencies.Auth.UserOf(r) diff --git a/internal/dis/component/auth/panel/totp.go b/internal/dis/component/auth/panel/totp.go index e59b99b..40c670b 100644 --- a/internal/dis/component/auth/panel/totp.go +++ b/internal/dis/component/auth/panel/totp.go @@ -7,8 +7,8 @@ import ( "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/control/static" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static/custom" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates" "github.com/FAU-CDI/wisski-distillery/pkg/httpx" "github.com/FAU-CDI/wisski-distillery/pkg/httpx/field" @@ -17,10 +17,10 @@ import ( //go:embed "templates/totp_enable.html" var totpEnableHTML []byte -var totpEnable = custom.Parse[userFormContext]("totp_enable.html", totpEnableHTML, static.AssetsUser) +var totpEnable = templates.Parse[userFormContext]("totp_enable.html", totpEnableHTML, assets.AssetsUser) func (panel *UserPanel) routeTOTPEnable(ctx context.Context) http.Handler { - tpl := totpEnable.Prepare(panel.Dependencies.Custom) + tpl := totpEnable.Prepare(panel.Dependencies.Templating) return &httpx.Form[struct{}]{ Fields: []field.Field{ @@ -69,7 +69,7 @@ func (panel *UserPanel) routeTOTPEnable(ctx context.Context) http.Handler { //go:embed "templates/totp_enroll.html" var totpEnrollHTML []byte -var totpEnrollTemplate = custom.Parse[totpEnrollContext]("totp_enroll.html", totpEnrollHTML, static.AssetsUser) +var totpEnrollTemplate = templates.Parse[totpEnrollContext]("totp_enroll.html", totpEnrollHTML, assets.AssetsUser) type totpEnrollContext struct { userFormContext @@ -80,7 +80,7 @@ type totpEnrollContext struct { } func (panel *UserPanel) routeTOTPEnroll(ctx context.Context) http.Handler { - tpl := totpEnrollTemplate.Prepare(panel.Dependencies.Custom, custom.BaseContextGaps{ + tpl := totpEnrollTemplate.Prepare(panel.Dependencies.Templating, templates.BaseContextGaps{ Crumbs: []component.MenuItem{ {Title: "User", Path: "/user/"}, {Title: "Enable TOTP", Path: "/user/totp/enable/"}, @@ -156,10 +156,10 @@ func (panel *UserPanel) routeTOTPEnroll(ctx context.Context) http.Handler { //go:embed "templates/totp_disable.html" var totpDisableHTML []byte -var totpDisableTemplate = custom.Parse[userFormContext]("totp_disable.html", totpDisableHTML, static.AssetsUser) +var totpDisableTemplate = templates.Parse[userFormContext]("totp_disable.html", totpDisableHTML, assets.AssetsUser) func (panel *UserPanel) routeTOTPDisable(ctx context.Context) http.Handler { - tpl := totpDisableTemplate.Prepare(panel.Dependencies.Custom) + tpl := totpDisableTemplate.Prepare(panel.Dependencies.Templating) return &httpx.Form[struct{}]{ Fields: []field.Field{ diff --git a/internal/dis/component/auth/panel/user.go b/internal/dis/component/auth/panel/user.go index 9cd2c12..d0650aa 100644 --- a/internal/dis/component/auth/panel/user.go +++ b/internal/dis/component/auth/panel/user.go @@ -9,17 +9,17 @@ import ( "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/control/static" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static/custom" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates" "github.com/FAU-CDI/wisski-distillery/internal/models" ) //go:embed "templates/user.html" var userHTML []byte -var userTemplate = custom.Parse[userContext]("user.html", userHTML, static.AssetsUser) +var userTemplate = templates.Parse[userContext]("user.html", userHTML, assets.AssetsUser) type userContext struct { - custom.BaseContext + templates.BaseContext *auth.AuthUser Grants []GrantWithURL @@ -31,7 +31,7 @@ type GrantWithURL struct { } func (panel *UserPanel) routeUser(ctx context.Context) http.Handler { - tpl := userTemplate.Prepare(panel.Dependencies.Custom, custom.BaseContextGaps{ + tpl := userTemplate.Prepare(panel.Dependencies.Templating, templates.BaseContextGaps{ Crumbs: []component.MenuItem{ {Title: "User", Path: "/user/"}, }, @@ -42,7 +42,7 @@ func (panel *UserPanel) routeUser(ctx context.Context) http.Handler { }, }) - return tpl.HTMLHandlerWithGaps(func(r *http.Request, gaps *custom.BaseContextGaps) (uc userContext, err error) { + return tpl.HTMLHandlerWithGaps(func(r *http.Request, gaps *templates.BaseContextGaps) (uc userContext, err error) { // find the user uc.AuthUser, err = panel.Dependencies.Auth.UserOf(r) if err != nil || uc.AuthUser == nil { diff --git a/internal/dis/component/auth/session.go b/internal/dis/component/auth/session.go index 6d5044a..46c7245 100644 --- a/internal/dis/component/auth/session.go +++ b/internal/dis/component/auth/session.go @@ -7,9 +7,9 @@ import ( "net/http" "github.com/FAU-CDI/wisski-distillery/internal/dis/component" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static/custom" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates" "github.com/FAU-CDI/wisski-distillery/pkg/httpx" "github.com/FAU-CDI/wisski-distillery/pkg/httpx/field" "github.com/gorilla/sessions" @@ -35,7 +35,7 @@ func (auth *Auth) UserOf(r *http.Request) (user *AuthUser, err error) { } // try to read the name from the session - name, ok := sess.Values[control.SessionUserKey] + name, ok := sess.Values[server.SessionUserKey] if !ok { return nil, nil } @@ -67,7 +67,7 @@ func (auth *Auth) UserOf(r *http.Request) (user *AuthUser, err error) { func (auth *Auth) session(r *http.Request) (*sessions.Session, error) { return auth.store.Get(func() sessions.Store { return sessions.NewCookieStore([]byte(auth.Config.SessionSecret)) - }).Get(r, control.SessionCookie) + }).Get(r, server.SessionCookie) } func (auth *Auth) Menu(r *http.Request) []component.MenuItem { @@ -101,7 +101,7 @@ func (auth *Auth) Login(w http.ResponseWriter, r *http.Request, user *AuthUser) if err != nil { return err } - sess.Values[control.SessionUserKey] = user.User.User + sess.Values[server.SessionUserKey] = user.User.User return sess.Save(r, w) } @@ -120,7 +120,7 @@ func (auth *Auth) Logout(w http.ResponseWriter, r *http.Request) error { //go:embed "login.html" var loginHTML []byte -var loginTemplate = custom.ParseForm("login.html", loginHTML, static.AssetsUser) +var loginTemplate = templates.ParseForm("login.html", loginHTML, assets.AssetsUser) var loginResponse = httpx.Response{ ContentType: "text/plain", @@ -131,7 +131,7 @@ var errLoginFailed = errors.New("Login failed") // authLogin implements a view to login a user func (auth *Auth) authLogin(ctx context.Context) http.Handler { - tpl := loginTemplate.Prepare(auth.Dependencies.Custom) + tpl := loginTemplate.Prepare(auth.Dependencies.Templating) return &httpx.Form[*AuthUser]{ Fields: []field.Field{ @@ -145,7 +145,7 @@ func (auth *Auth) authLogin(ctx context.Context) http.Handler { if context.Err != nil { context.Err = errLoginFailed } - tpl.Execute(w, r, custom.BaseFormContext{FormContext: context}, custom.BaseContextGaps{ + tpl.Execute(w, r, templates.BaseFormContext{FormContext: context}, templates.BaseContextGaps{ Crumbs: []component.MenuItem{ {Title: "Login", Path: template.URL(r.URL.RequestURI())}, }, diff --git a/internal/dis/component/control/control.go b/internal/dis/component/control/control.go deleted file mode 100644 index 697be39..0000000 --- a/internal/dis/component/control/control.go +++ /dev/null @@ -1,71 +0,0 @@ -package control - -import ( - "context" - "embed" - "io" - "path/filepath" - "syscall" - - "github.com/FAU-CDI/wisski-distillery/internal/bootstrap" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static/custom" - "github.com/FAU-CDI/wisski-distillery/pkg/environment" -) - -// Control represents the running control server. -type Control struct { - component.Base - Dependencies struct { - Routeables []component.Routeable - Cronables []component.Cronable - - Custom *custom.Custom - } -} - -var ( - _ component.Installable = (*Control)(nil) -) - -func (control Control) Path() string { - return filepath.Join(control.Still.Config.DeployRoot, "core", "dis") -} - -//go:embed all:control control.env -var resources embed.FS - -func (control *Control) Stack(env environment.Environment) component.StackWithResources { - return component.MakeStack(control, env, component.StackWithResources{ - Resources: resources, - ContextPath: "control", - EnvPath: "control.env", - - EnvContext: map[string]string{ - "DOCKER_NETWORK_NAME": control.Config.DockerNetworkName, - "HOST_RULE": control.Config.DefaultHostRule(), - "HTTPS_ENABLED": control.Config.HTTPSEnabledEnv(), - - "CONFIG_PATH": control.Config.ConfigPath, - "DEPLOY_ROOT": control.Config.DeployRoot, - - "SELF_OVERRIDES_FILE": control.Config.SelfOverridesFile, - "SELF_RESOLVER_BLOCK_FILE": control.Config.SelfResolverBlockFile, - - "CUSTOM_ASSETS_PATH": control.Dependencies.Custom.CustomAssetsPath(), - }, - - CopyContextFiles: []string{bootstrap.Executable}, - }) -} - -// Trigger triggers the active cron run to immediatly invoke cron. -func (control *Control) Trigger(ctx context.Context, env environment.Environment) error { - return control.Stack(env).Kill(ctx, io.Discard, "control", syscall.SIGHUP) -} - -func (control Control) Context(parent component.InstallationContext) component.InstallationContext { - return component.InstallationContext{ - bootstrap.Executable: control.Config.CurrentExecutable(control.Environment), // TODO: Does this make sense? - } -} diff --git a/internal/dis/component/control/static/assets_dist.go b/internal/dis/component/control/static/assets_dist.go deleted file mode 100644 index 215b4c3..0000000 --- a/internal/dis/component/control/static/assets_dist.go +++ /dev/null @@ -1,26 +0,0 @@ -package static - -import _ "embed" - -// This file was automatically generated. Do not edit. - -//go:embed "assets_disclaimer.txt" -var AssetsDisclaimer string - -// AssetsDefault contains assets for the 'Default' entrypoint. -var AssetsDefault = Assets{ - Scripts: ``, - Styles: ``, -} - -// AssetsUser contains assets for the 'User' entrypoint. -var AssetsUser = Assets{ - Scripts: ``, - Styles: ``, -} - -// AssetsAdmin contains assets for the 'Admin' entrypoint. -var AssetsAdmin = Assets{ - Scripts: ``, - Styles: ``, -} diff --git a/internal/dis/component/control/static/custom/assets.go b/internal/dis/component/control/static/custom/assets.go deleted file mode 100644 index c0ad5b4..0000000 --- a/internal/dis/component/control/static/custom/assets.go +++ /dev/null @@ -1,22 +0,0 @@ -package custom - -import ( - "path/filepath" - - "github.com/FAU-CDI/wisski-distillery/internal/dis/component" -) - -// CustomAssetsPath is the path custom assets are stored at -func (custom *Custom) CustomAssetsPath() string { - return filepath.Join(custom.Config.DeployRoot, "core", "assets") -} - -func (custom *Custom) CustomAssetPath(name string) string { - return filepath.Join(custom.CustomAssetsPath(), name) -} - -func (custom *Custom) BackupName() string { return "custom" } - -func (custom *Custom) Backup(context component.StagingContext) error { - return context.CopyDirectory("", custom.CustomAssetsPath()) -} diff --git a/internal/dis/component/resolver/resolver.go b/internal/dis/component/resolver/resolver.go index 7f3d282..9460e2f 100644 --- a/internal/dis/component/resolver/resolver.go +++ b/internal/dis/component/resolver/resolver.go @@ -11,9 +11,9 @@ import ( "github.com/FAU-CDI/wdresolve/resolvers" "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/control/static" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static/custom" "github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates" "github.com/FAU-CDI/wisski-distillery/pkg/lazy" "github.com/rs/zerolog" @@ -23,9 +23,9 @@ import ( type Resolver struct { component.Base Dependencies struct { - Instances *instances.Instances - Custom *custom.Custom - Auth *auth.Auth + Instances *instances.Instances + Templating *templates.Templating + Auth *auth.Auth } prefixes lazy.Lazy[map[string]string] // cached prefixes (from the server) @@ -50,15 +50,15 @@ func (resolver *Resolver) Routes() component.Routes { //go:embed "resolver.html" var resolverHTML []byte -var resolverTemplate = custom.Parse[resolverContext]("resolver.html", resolverHTML, static.AssetsDefault) +var resolverTemplate = templates.Parse[resolverContext]("resolver.html", resolverHTML, assets.AssetsDefault) type resolverContext struct { - custom.BaseContext + templates.BaseContext wdresolve.IndexContext } func (resolver *Resolver) HandleRoute(ctx context.Context, route string) (http.Handler, error) { - tpl := resolverTemplate.Prepare(resolver.Dependencies.Custom, custom.BaseContextGaps{ + tpl := resolverTemplate.Prepare(resolver.Dependencies.Templating, templates.BaseContextGaps{ Crumbs: []component.MenuItem{ {Title: "Resolver", Path: "/wisski/get/"}, }, diff --git a/internal/dis/component/control/admin/admin.go b/internal/dis/component/server/admin/admin.go similarity index 97% rename from internal/dis/component/control/admin/admin.go rename to internal/dis/component/server/admin/admin.go index 897132e..69f4633 100644 --- a/internal/dis/component/control/admin/admin.go +++ b/internal/dis/component/server/admin/admin.go @@ -7,10 +7,10 @@ import ( "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/policy" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static/custom" "github.com/FAU-CDI/wisski-distillery/internal/dis/component/exporter" "github.com/FAU-CDI/wisski-distillery/internal/dis/component/exporter/logger" "github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances/purger" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates" "github.com/julienschmidt/httprouter" "github.com/rs/zerolog" @@ -32,7 +32,7 @@ type Admin struct { Policy *policy.Policy - Custom *custom.Custom + Templating *templates.Templating Purger *purger.Purger } diff --git a/internal/dis/component/control/admin/components.go b/internal/dis/component/server/admin/components.go similarity index 72% rename from internal/dis/component/control/admin/components.go rename to internal/dis/component/server/admin/components.go index a901adf..9ce9809 100644 --- a/internal/dis/component/control/admin/components.go +++ b/internal/dis/component/server/admin/components.go @@ -8,9 +8,9 @@ import ( _ "embed" "github.com/FAU-CDI/wisski-distillery/internal/dis/component" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static/custom" "github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates" "github.com/FAU-CDI/wisski-distillery/internal/models" "github.com/FAU-CDI/wisski-distillery/pkg/httpx" "github.com/FAU-CDI/wisski-distillery/pkg/lazy" @@ -19,16 +19,16 @@ import ( //go:embed "html/components.html" var componentsHTML []byte -var componentsTemplate = custom.Parse[componentContext]("components.html", componentsHTML, static.AssetsAdmin) +var componentsTemplate = templates.Parse[componentContext]("components.html", componentsHTML, assets.AssetsAdmin) type componentContext struct { - custom.BaseContext + templates.BaseContext Analytics lazy.PoolAnalytics } func (admin *Admin) components(ctx context.Context) http.Handler { - tpl := componentsTemplate.Prepare(admin.Dependencies.Custom, custom.BaseContextGaps{ + tpl := componentsTemplate.Prepare(admin.Dependencies.Templating, templates.BaseContextGaps{ Crumbs: []component.MenuItem{ {Title: "Admin", Path: "/admin/"}, {Title: "Components", Path: "/admin/components/"}, @@ -43,17 +43,17 @@ func (admin *Admin) components(ctx context.Context) http.Handler { //go:embed "html/ingredients.html" var ingredientsHTML []byte -var ingredientsTemplate = custom.Parse[ingredientsContext]("ingredients.html", ingredientsHTML, static.AssetsAdmin) +var ingredientsTemplate = templates.Parse[ingredientsContext]("ingredients.html", ingredientsHTML, assets.AssetsAdmin) type ingredientsContext struct { - custom.BaseContext + templates.BaseContext Instance models.Instance Analytics *lazy.PoolAnalytics } func (admin *Admin) ingredients(ctx context.Context) http.Handler { - tpl := ingredientsTemplate.Prepare(admin.Dependencies.Custom, custom.BaseContextGaps{ + tpl := ingredientsTemplate.Prepare(admin.Dependencies.Templating, templates.BaseContextGaps{ Crumbs: []component.MenuItem{ {Title: "Admin", Path: "/admin/"}, {Title: "Instance", Path: "* to be updated *"}, @@ -61,7 +61,7 @@ func (admin *Admin) ingredients(ctx context.Context) http.Handler { }, }) - return tpl.HTMLHandlerWithGaps(func(r *http.Request, gaps *custom.BaseContextGaps) (ic ingredientsContext, err error) { + return tpl.HTMLHandlerWithGaps(func(r *http.Request, gaps *templates.BaseContextGaps) (ic ingredientsContext, err error) { slug := httprouter.ParamsFromContext(r.Context()).ByName("slug") gaps.Crumbs[1] = component.MenuItem{Title: "Instance", Path: template.URL("/admin/instance/" + slug)} diff --git a/internal/dis/component/control/admin/grants.go b/internal/dis/component/server/admin/grants.go similarity index 82% rename from internal/dis/component/control/admin/grants.go rename to internal/dis/component/server/admin/grants.go index d45d25a..d8a0e78 100644 --- a/internal/dis/component/control/admin/grants.go +++ b/internal/dis/component/server/admin/grants.go @@ -8,9 +8,9 @@ import ( "net/http" "github.com/FAU-CDI/wisski-distillery/internal/dis/component" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static/custom" "github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates" "github.com/FAU-CDI/wisski-distillery/internal/models" "github.com/FAU-CDI/wisski-distillery/internal/wisski" "github.com/FAU-CDI/wisski-distillery/pkg/httpx" @@ -22,10 +22,10 @@ import ( //go:embed "html/grants.html" var grantsHTML []byte -var grantsTemplate = custom.Parse[grantsContext]("grants.html", grantsHTML, static.AssetsAdmin) +var grantsTemplate = templates.Parse[grantsContext]("grants.html", grantsHTML, assets.AssetsAdmin) type grantsContext struct { - custom.BaseContext + templates.BaseContext Error string @@ -38,7 +38,7 @@ type grantsContext struct { } func (admin *Admin) grants(ctx context.Context) http.Handler { - tpl := grantsTemplate.Prepare(admin.Dependencies.Custom, custom.BaseContextGaps{ + tpl := grantsTemplate.Prepare(admin.Dependencies.Templating, templates.BaseContextGaps{ Crumbs: []component.MenuItem{ {Title: "Admin", Path: "/admin/"}, {Title: "Instance", Path: "*to be updated*"}, @@ -46,7 +46,7 @@ func (admin *Admin) grants(ctx context.Context) http.Handler { }, }) - return tpl.HTMLHandlerWithGaps(func(r *http.Request, gaps *custom.BaseContextGaps) (grantsContext, error) { + return tpl.HTMLHandlerWithGaps(func(r *http.Request, gaps *templates.BaseContextGaps) (grantsContext, error) { if r.Method == http.MethodGet { return admin.getGrants(r, gaps) } else { @@ -55,7 +55,7 @@ func (admin *Admin) grants(ctx context.Context) http.Handler { }) } -func (admin *Admin) getGrants(r *http.Request, gaps *custom.BaseContextGaps) (gc grantsContext, err error) { +func (admin *Admin) getGrants(r *http.Request, gaps *templates.BaseContextGaps) (gc grantsContext, err error) { slug := httprouter.ParamsFromContext(r.Context()).ByName("slug") if err := gc.use(r, gaps, slug, admin); err != nil { return gc, err @@ -68,7 +68,7 @@ func (admin *Admin) getGrants(r *http.Request, gaps *custom.BaseContextGaps) (gc return gc, nil } -func (admin *Admin) postGrants(r *http.Request, gaps *custom.BaseContextGaps) (gc grantsContext, err error) { +func (admin *Admin) postGrants(r *http.Request, gaps *templates.BaseContextGaps) (gc grantsContext, err error) { // parse the form if err := r.ParseForm(); err != nil { return gc, err @@ -115,7 +115,7 @@ func (admin *Admin) postGrants(r *http.Request, gaps *custom.BaseContextGaps) (g return gc, nil } -func (gc *grantsContext) use(r *http.Request, gaps *custom.BaseContextGaps, slug string, admin *Admin) (err error) { +func (gc *grantsContext) use(r *http.Request, gaps *templates.BaseContextGaps, slug string, admin *Admin) (err error) { gaps.Crumbs[1] = component.MenuItem{Title: "Instance", Path: template.URL("/admin/instance/" + slug)} gaps.Crumbs[2] = component.MenuItem{Title: "Grants", Path: template.URL("/admin/instance/" + slug + "/grants/")} diff --git a/internal/dis/component/control/admin/html/components.html b/internal/dis/component/server/admin/html/components.html similarity index 100% rename from internal/dis/component/control/admin/html/components.html rename to internal/dis/component/server/admin/html/components.html diff --git a/internal/dis/component/control/admin/html/grants.html b/internal/dis/component/server/admin/html/grants.html similarity index 100% rename from internal/dis/component/control/admin/html/grants.html rename to internal/dis/component/server/admin/html/grants.html diff --git a/internal/dis/component/control/admin/html/index.html b/internal/dis/component/server/admin/html/index.html similarity index 100% rename from internal/dis/component/control/admin/html/index.html rename to internal/dis/component/server/admin/html/index.html diff --git a/internal/dis/component/control/admin/html/ingredients.html b/internal/dis/component/server/admin/html/ingredients.html similarity index 100% rename from internal/dis/component/control/admin/html/ingredients.html rename to internal/dis/component/server/admin/html/ingredients.html diff --git a/internal/dis/component/control/admin/html/instance.html b/internal/dis/component/server/admin/html/instance.html similarity index 100% rename from internal/dis/component/control/admin/html/instance.html rename to internal/dis/component/server/admin/html/instance.html diff --git a/internal/dis/component/control/admin/html/user_create.html b/internal/dis/component/server/admin/html/user_create.html similarity index 100% rename from internal/dis/component/control/admin/html/user_create.html rename to internal/dis/component/server/admin/html/user_create.html diff --git a/internal/dis/component/control/admin/html/users.html b/internal/dis/component/server/admin/html/users.html similarity index 100% rename from internal/dis/component/control/admin/html/users.html rename to internal/dis/component/server/admin/html/users.html diff --git a/internal/dis/component/control/admin/index.go b/internal/dis/component/server/admin/index.go similarity index 82% rename from internal/dis/component/control/admin/index.go rename to internal/dis/component/server/admin/index.go index 4cde93d..34877ad 100644 --- a/internal/dis/component/control/admin/index.go +++ b/internal/dis/component/server/admin/index.go @@ -8,8 +8,8 @@ import ( _ "embed" "github.com/FAU-CDI/wisski-distillery/internal/dis/component" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static/custom" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates" "github.com/FAU-CDI/wisski-distillery/internal/status" "golang.org/x/sync/errgroup" ) @@ -80,17 +80,17 @@ func (admin *Admin) Fetch(flags component.FetcherFlags, target *status.Distiller //go:embed "html/index.html" var indexHTML []byte -var indexTemplate = custom.Parse[indexContext]("index.html", indexHTML, static.AssetsAdmin) +var indexTemplate = templates.Parse[indexContext]("index.html", indexHTML, assets.AssetsAdmin) type indexContext struct { - custom.BaseContext + templates.BaseContext status.Distillery Instances []status.WissKI } func (admin *Admin) index(ctx context.Context) http.Handler { - tpl := indexTemplate.Prepare(admin.Dependencies.Custom, custom.BaseContextGaps{ + tpl := indexTemplate.Prepare(admin.Dependencies.Templating, templates.BaseContextGaps{ Crumbs: []component.MenuItem{ {Title: "Admin", Path: "/admin/"}, }, @@ -100,7 +100,7 @@ func (admin *Admin) index(ctx context.Context) http.Handler { }, }) - return tpl.HTMLHandlerWithGaps(func(r *http.Request, gaps *custom.BaseContextGaps) (idx indexContext, err error) { + return tpl.HTMLHandlerWithGaps(func(r *http.Request, gaps *templates.BaseContextGaps) (idx indexContext, err error) { idx.Distillery, idx.Instances, err = admin.Status(r.Context(), true) return }) diff --git a/internal/dis/component/control/admin/instance.go b/internal/dis/component/server/admin/instance.go similarity index 77% rename from internal/dis/component/control/admin/instance.go rename to internal/dis/component/server/admin/instance.go index f3885b4..539fa03 100644 --- a/internal/dis/component/control/admin/instance.go +++ b/internal/dis/component/server/admin/instance.go @@ -7,9 +7,9 @@ import ( "net/http" "github.com/FAU-CDI/wisski-distillery/internal/dis/component" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static/custom" "github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates" "github.com/FAU-CDI/wisski-distillery/internal/models" "github.com/FAU-CDI/wisski-distillery/internal/status" "github.com/FAU-CDI/wisski-distillery/pkg/httpx" @@ -18,17 +18,17 @@ import ( //go:embed "html/instance.html" var instanceHTML []byte -var instanceTemplate = custom.Parse[instanceContext]("instance.html", instanceHTML, static.AssetsAdmin) +var instanceTemplate = templates.Parse[instanceContext]("instance.html", instanceHTML, assets.AssetsAdmin) type instanceContext struct { - custom.BaseContext + templates.BaseContext Instance models.Instance Info status.WissKI } func (admin *Admin) instance(ctx context.Context) http.Handler { - tpl := instanceTemplate.Prepare(admin.Dependencies.Custom, custom.BaseContextGaps{ + tpl := instanceTemplate.Prepare(admin.Dependencies.Templating, templates.BaseContextGaps{ Crumbs: []component.MenuItem{ {Title: "Admin", Path: "/admin/"}, {Title: "Instance", Path: "*to be replaced*"}, @@ -39,7 +39,7 @@ func (admin *Admin) instance(ctx context.Context) http.Handler { }, }) - return tpl.HTMLHandlerWithGaps(func(r *http.Request, gaps *custom.BaseContextGaps) (ic instanceContext, err error) { + return tpl.HTMLHandlerWithGaps(func(r *http.Request, gaps *templates.BaseContextGaps) (ic instanceContext, err error) { slug := httprouter.ParamsFromContext(r.Context()).ByName("slug") gaps.Crumbs[1] = component.MenuItem{Title: "Instance", Path: template.URL("/admin/instance/" + slug)} diff --git a/internal/dis/component/control/admin/socket.go b/internal/dis/component/server/admin/socket.go similarity index 100% rename from internal/dis/component/control/admin/socket.go rename to internal/dis/component/server/admin/socket.go diff --git a/internal/dis/component/control/admin/users.go b/internal/dis/component/server/admin/users.go similarity index 92% rename from internal/dis/component/control/admin/users.go rename to internal/dis/component/server/admin/users.go index 0a0ca59..be77ba1 100644 --- a/internal/dis/component/control/admin/users.go +++ b/internal/dis/component/server/admin/users.go @@ -10,8 +10,8 @@ import ( "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/control/static" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static/custom" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates" "github.com/FAU-CDI/wisski-distillery/pkg/httpx" "github.com/FAU-CDI/wisski-distillery/pkg/httpx/field" "github.com/rs/zerolog" @@ -19,17 +19,17 @@ import ( //go:embed "html/users.html" var usersHTML []byte -var usersTemplate = custom.Parse[usersContext]("user.html", usersHTML, static.AssetsAdmin) +var usersTemplate = templates.Parse[usersContext]("user.html", usersHTML, assets.AssetsAdmin) type usersContext struct { - custom.BaseContext + templates.BaseContext Error string Users []*auth.AuthUser } func (admin *Admin) users(ctx context.Context) http.Handler { - tpl := usersTemplate.Prepare(admin.Dependencies.Custom, custom.BaseContextGaps{ + tpl := usersTemplate.Prepare(admin.Dependencies.Templating, templates.BaseContextGaps{ Crumbs: []component.MenuItem{ {Title: "Admin", Path: "/admin/"}, {Title: "Users", Path: "/admin/users/"}, @@ -48,7 +48,7 @@ func (admin *Admin) users(ctx context.Context) http.Handler { //go:embed "html/user_create.html" var userCreateHTML []byte -var userCreateTemplate = custom.ParseForm("user_create.html", userCreateHTML, static.AssetsAdmin) +var userCreateTemplate = templates.ParseForm("user_create.html", userCreateHTML, assets.AssetsAdmin) var ( errCreateInvalidUsername = errors.New("invalid username") @@ -62,7 +62,7 @@ type createUserResult struct { } func (admin *Admin) createUser(ctx context.Context) http.Handler { - tpl := userCreateTemplate.Prepare(admin.Dependencies.Custom, custom.BaseContextGaps{ + tpl := userCreateTemplate.Prepare(admin.Dependencies.Templating, templates.BaseContextGaps{ Crumbs: []component.MenuItem{ {Title: "Admin", Path: "/admin/"}, {Title: "Users", Path: "/admin/users"}, @@ -79,7 +79,7 @@ func (admin *Admin) createUser(ctx context.Context) http.Handler { FieldTemplate: field.PureCSSFieldTemplate, RenderTemplate: tpl.Template(), - RenderTemplateContext: custom.FormTemplateContext(tpl), + RenderTemplateContext: templates.FormTemplateContext(tpl), Validate: func(r *http.Request, values map[string]string) (cu createUserResult, err error) { cu.User, cu.Passsword, cu.Admin = values["username"], values["password"], values["admin"] == field.CheckboxChecked diff --git a/internal/dis/component/control/static/assets.go b/internal/dis/component/server/assets/assets.go similarity index 96% rename from internal/dis/component/control/static/assets.go rename to internal/dis/component/server/assets/assets.go index de76041..5d28cba 100644 --- a/internal/dis/component/control/static/assets.go +++ b/internal/dis/component/server/assets/assets.go @@ -1,4 +1,4 @@ -package static +package assets import ( "html/template" @@ -13,7 +13,7 @@ import ( // - Delete any previously generated distribution directory. // - Bundle the entrypoint sources under 'src/entry/{{Name}}/index.{ts,css}' together with the base './src/base/index.{ts,css}' // - Store the output inside the 'dist' directory -// - Generate new constants of the form {{Name}} +// - Generate new constants of the form Assets{{Name}} // // Each asset group should be registered as a parameter to the 'go:generate' line. type Assets struct { diff --git a/internal/dis/component/control/static/assets_disclaimer.txt b/internal/dis/component/server/assets/assets_disclaimer.txt similarity index 100% rename from internal/dis/component/control/static/assets_disclaimer.txt rename to internal/dis/component/server/assets/assets_disclaimer.txt diff --git a/internal/dis/component/server/assets/assets_dist.go b/internal/dis/component/server/assets/assets_dist.go new file mode 100644 index 0000000..ec220ad --- /dev/null +++ b/internal/dis/component/server/assets/assets_dist.go @@ -0,0 +1,29 @@ +package assets + +import _ "embed" + +// This file was automatically generated. Do not edit. + +//go:embed "assets_disclaimer.txt" +var Disclaimer string + +// Public holds the path to the public route +const Public = "/this-is-fine/" + +// AssetsDefault contains assets for the 'Default' entrypoint. +var AssetsDefault = Assets{ + Scripts: ``, + Styles: ``, +} + +// AssetsUser contains assets for the 'User' entrypoint. +var AssetsUser = Assets{ + Scripts: ``, + Styles: ``, +} + +// AssetsAdmin contains assets for the 'Admin' entrypoint. +var AssetsAdmin = Assets{ + Scripts: ``, + Styles: ``, +} diff --git a/internal/dis/component/control/static/build.mjs b/internal/dis/component/server/assets/build.mjs similarity index 96% rename from internal/dis/component/control/static/build.mjs rename to internal/dis/component/server/assets/build.mjs index 249f265..9550037 100644 --- a/internal/dis/component/control/static/build.mjs +++ b/internal/dis/component/server/assets/build.mjs @@ -11,7 +11,7 @@ import { spawnSync } from 'child_process' const ENTRYPOINTS = process.argv.slice(2) const ENTRY_DIR = join('.', '.entry-cache') // directory to place entries into const DIST_DIR = join('.', 'dist') -const PUBLIC_DIR = '/static/' +const PUBLIC_DIR = '/this-is-fine/' const DEST_PACKAGE = process.env.GOPACKAGE ?? 'static' const DEST_DISCLAIMER = (() => { @@ -152,7 +152,10 @@ import _ "embed" // This file was automatically generated. Do not edit. //go:embed ${JSON.stringify(DEST_DISCLAIMER)} -var AssetsDisclaimer string +var Disclaimer string + +// Public holds the path to the public route +const Public = ${JSON.stringify(PUBLIC_DIR)} ${goAssets} `; diff --git a/internal/dis/component/control/static/dist/Admin.1a380f6f.js b/internal/dis/component/server/assets/dist/Admin.1a380f6f.js similarity index 100% rename from internal/dis/component/control/static/dist/Admin.1a380f6f.js rename to internal/dis/component/server/assets/dist/Admin.1a380f6f.js diff --git a/internal/dis/component/control/static/dist/Admin.6d2ae968.css b/internal/dis/component/server/assets/dist/Admin.6d2ae968.css similarity index 100% rename from internal/dis/component/control/static/dist/Admin.6d2ae968.css rename to internal/dis/component/server/assets/dist/Admin.6d2ae968.css diff --git a/internal/dis/component/control/static/dist/Admin.6d59e220.css b/internal/dis/component/server/assets/dist/Admin.6d59e220.css similarity index 100% rename from internal/dis/component/control/static/dist/Admin.6d59e220.css rename to internal/dis/component/server/assets/dist/Admin.6d59e220.css diff --git a/internal/dis/component/control/static/dist/Admin.cb58d290.js b/internal/dis/component/server/assets/dist/Admin.cb58d290.js similarity index 100% rename from internal/dis/component/control/static/dist/Admin.cb58d290.js rename to internal/dis/component/server/assets/dist/Admin.cb58d290.js diff --git a/internal/dis/component/control/static/dist/Default.38d394c2.js b/internal/dis/component/server/assets/dist/Default.38d394c2.js similarity index 100% rename from internal/dis/component/control/static/dist/Default.38d394c2.js rename to internal/dis/component/server/assets/dist/Default.38d394c2.js diff --git a/internal/dis/component/control/static/dist/Default.db26a303.css b/internal/dis/component/server/assets/dist/Default.db26a303.css similarity index 100% rename from internal/dis/component/control/static/dist/Default.db26a303.css rename to internal/dis/component/server/assets/dist/Default.db26a303.css diff --git a/internal/dis/component/control/static/dist/Default.f9675eae.css b/internal/dis/component/server/assets/dist/Default.f9675eae.css similarity index 100% rename from internal/dis/component/control/static/dist/Default.f9675eae.css rename to internal/dis/component/server/assets/dist/Default.f9675eae.css diff --git a/internal/dis/component/control/static/dist/User.68febbf8.css b/internal/dis/component/server/assets/dist/User.68febbf8.css similarity index 100% rename from internal/dis/component/control/static/dist/User.68febbf8.css rename to internal/dis/component/server/assets/dist/User.68febbf8.css diff --git a/internal/dis/component/control/static/dist/User.840de3b4.css b/internal/dis/component/server/assets/dist/User.840de3b4.css similarity index 100% rename from internal/dis/component/control/static/dist/User.840de3b4.css rename to internal/dis/component/server/assets/dist/User.840de3b4.css diff --git a/internal/dis/component/control/static/dist/User.b2f9a57c.js b/internal/dis/component/server/assets/dist/User.b2f9a57c.js similarity index 100% rename from internal/dis/component/control/static/dist/User.b2f9a57c.js rename to internal/dis/component/server/assets/dist/User.b2f9a57c.js diff --git a/internal/dis/component/control/static/dist/User.e0367d79.js b/internal/dis/component/server/assets/dist/User.e0367d79.js similarity index 100% rename from internal/dis/component/control/static/dist/User.e0367d79.js rename to internal/dis/component/server/assets/dist/User.e0367d79.js diff --git a/internal/dis/component/control/static/package.json b/internal/dis/component/server/assets/package.json similarity index 100% rename from internal/dis/component/control/static/package.json rename to internal/dis/component/server/assets/package.json diff --git a/internal/dis/component/control/static/src/base/index.css b/internal/dis/component/server/assets/src/base/index.css similarity index 100% rename from internal/dis/component/control/static/src/base/index.css rename to internal/dis/component/server/assets/src/base/index.css diff --git a/internal/dis/component/control/static/src/base/index.ts b/internal/dis/component/server/assets/src/base/index.ts similarity index 100% rename from internal/dis/component/control/static/src/base/index.ts rename to internal/dis/component/server/assets/src/base/index.ts diff --git a/internal/dis/component/control/static/src/entry/Admin/index.css b/internal/dis/component/server/assets/src/entry/Admin/index.css similarity index 100% rename from internal/dis/component/control/static/src/entry/Admin/index.css rename to internal/dis/component/server/assets/src/entry/Admin/index.css diff --git a/internal/dis/component/control/static/src/entry/Admin/index.ts b/internal/dis/component/server/assets/src/entry/Admin/index.ts similarity index 100% rename from internal/dis/component/control/static/src/entry/Admin/index.ts rename to internal/dis/component/server/assets/src/entry/Admin/index.ts diff --git a/internal/dis/component/control/static/src/entry/Default/index.css b/internal/dis/component/server/assets/src/entry/Default/index.css similarity index 100% rename from internal/dis/component/control/static/src/entry/Default/index.css rename to internal/dis/component/server/assets/src/entry/Default/index.css diff --git a/internal/dis/component/control/static/src/entry/Default/index.ts b/internal/dis/component/server/assets/src/entry/Default/index.ts similarity index 100% rename from internal/dis/component/control/static/src/entry/Default/index.ts rename to internal/dis/component/server/assets/src/entry/Default/index.ts diff --git a/internal/dis/component/control/static/src/entry/User/index.css b/internal/dis/component/server/assets/src/entry/User/index.css similarity index 100% rename from internal/dis/component/control/static/src/entry/User/index.css rename to internal/dis/component/server/assets/src/entry/User/index.css diff --git a/internal/dis/component/control/static/src/entry/User/index.ts b/internal/dis/component/server/assets/src/entry/User/index.ts similarity index 100% rename from internal/dis/component/control/static/src/entry/User/index.ts rename to internal/dis/component/server/assets/src/entry/User/index.ts diff --git a/internal/dis/component/control/static/src/lib/autolink/index.css b/internal/dis/component/server/assets/src/lib/autolink/index.css similarity index 100% rename from internal/dis/component/control/static/src/lib/autolink/index.css rename to internal/dis/component/server/assets/src/lib/autolink/index.css diff --git a/internal/dis/component/control/static/src/lib/autolink/index.ts b/internal/dis/component/server/assets/src/lib/autolink/index.ts similarity index 100% rename from internal/dis/component/control/static/src/lib/autolink/index.ts rename to internal/dis/component/server/assets/src/lib/autolink/index.ts diff --git a/internal/dis/component/control/static/src/lib/copy/index.css b/internal/dis/component/server/assets/src/lib/copy/index.css similarity index 100% rename from internal/dis/component/control/static/src/lib/copy/index.css rename to internal/dis/component/server/assets/src/lib/copy/index.css diff --git a/internal/dis/component/control/static/src/lib/copy/index.ts b/internal/dis/component/server/assets/src/lib/copy/index.ts similarity index 100% rename from internal/dis/component/control/static/src/lib/copy/index.ts rename to internal/dis/component/server/assets/src/lib/copy/index.ts diff --git a/internal/dis/component/control/static/src/lib/highlight/index.ts b/internal/dis/component/server/assets/src/lib/highlight/index.ts similarity index 100% rename from internal/dis/component/control/static/src/lib/highlight/index.ts rename to internal/dis/component/server/assets/src/lib/highlight/index.ts diff --git a/internal/dis/component/control/static/src/lib/remote/index.css b/internal/dis/component/server/assets/src/lib/remote/index.css similarity index 100% rename from internal/dis/component/control/static/src/lib/remote/index.css rename to internal/dis/component/server/assets/src/lib/remote/index.css diff --git a/internal/dis/component/control/static/src/lib/remote/index.ts b/internal/dis/component/server/assets/src/lib/remote/index.ts similarity index 100% rename from internal/dis/component/control/static/src/lib/remote/index.ts rename to internal/dis/component/server/assets/src/lib/remote/index.ts diff --git a/internal/dis/component/control/static/src/lib/remote/socket.ts b/internal/dis/component/server/assets/src/lib/remote/socket.ts similarity index 100% rename from internal/dis/component/control/static/src/lib/remote/socket.ts rename to internal/dis/component/server/assets/src/lib/remote/socket.ts diff --git a/internal/dis/component/control/static/static.go b/internal/dis/component/server/assets/static.go similarity index 94% rename from internal/dis/component/control/static/static.go rename to internal/dis/component/server/assets/static.go index 10f9014..83203f2 100644 --- a/internal/dis/component/control/static/static.go +++ b/internal/dis/component/server/assets/static.go @@ -1,5 +1,5 @@ // Package static implements serving of fully static resources -package static +package assets import ( "context" @@ -20,7 +20,7 @@ var ( func (*Static) Routes() component.Routes { return component.Routes{ - Prefix: "/static/", + Prefix: Public, CSRF: false, } diff --git a/internal/dis/component/control/static/templates.go b/internal/dis/component/server/assets/templates.go similarity index 75% rename from internal/dis/component/control/static/templates.go rename to internal/dis/component/server/assets/templates.go index a318487..718b026 100644 --- a/internal/dis/component/control/static/templates.go +++ b/internal/dis/component/server/assets/templates.go @@ -1,8 +1,7 @@ -package static +package assets import ( "embed" - "encoding/json" "html/template" ) @@ -17,12 +16,6 @@ var ( // It will be able to make use of shared templates as well as functions. func NewSharedTemplate(name string) *template.Template { new := template.New(name) - new.Funcs(template.FuncMap{ - "json": func(data any) (string, error) { - bytes, err := json.Marshal(data) - return string(bytes), err - }, - }) for _, template := range shared.Templates() { new.AddParseTree(template.Tree.Name, template.Tree.Copy()) } diff --git a/internal/dis/component/control/static/templates/_anal.html b/internal/dis/component/server/assets/templates/_anal.html similarity index 100% rename from internal/dis/component/control/static/templates/_anal.html rename to internal/dis/component/server/assets/templates/_anal.html diff --git a/internal/dis/component/control/static/templates/_base.html b/internal/dis/component/server/assets/templates/_base.html similarity index 96% rename from internal/dis/component/control/static/templates/_base.html rename to internal/dis/component/server/assets/templates/_base.html index d284c8d..3337947 100644 --- a/internal/dis/component/control/static/templates/_base.html +++ b/internal/dis/component/server/assets/templates/_base.html @@ -44,7 +44,7 @@ diff --git a/internal/dis/component/control/static/templates/_form.html b/internal/dis/component/server/assets/templates/_form.html similarity index 100% rename from internal/dis/component/control/static/templates/_form.html rename to internal/dis/component/server/assets/templates/_form.html diff --git a/internal/dis/component/control/static/tsconfig.json b/internal/dis/component/server/assets/tsconfig.json similarity index 100% rename from internal/dis/component/control/static/tsconfig.json rename to internal/dis/component/server/assets/tsconfig.json diff --git a/internal/dis/component/control/static/yarn.lock b/internal/dis/component/server/assets/yarn.lock similarity index 100% rename from internal/dis/component/control/static/yarn.lock rename to internal/dis/component/server/assets/yarn.lock diff --git a/internal/dis/component/control/cookies.go b/internal/dis/component/server/cookies.go similarity index 96% rename from internal/dis/component/control/cookies.go rename to internal/dis/component/server/cookies.go index 70d68a1..2675676 100644 --- a/internal/dis/component/control/cookies.go +++ b/internal/dis/component/server/cookies.go @@ -1,4 +1,4 @@ -package control +package server // CSRFCookie, CSRFCookieField, SessionCookie and SessionUserKey // hold the names of the cookies and fields used for specific cookies. diff --git a/internal/dis/component/control/cron/cron.go b/internal/dis/component/server/cron/cron.go similarity index 100% rename from internal/dis/component/control/cron/cron.go rename to internal/dis/component/server/cron/cron.go diff --git a/internal/dis/component/control/home/home.go b/internal/dis/component/server/home/home.go similarity index 93% rename from internal/dis/component/control/home/home.go rename to internal/dis/component/server/home/home.go index 99e411f..0b2f5bb 100644 --- a/internal/dis/component/control/home/home.go +++ b/internal/dis/component/server/home/home.go @@ -6,8 +6,8 @@ import ( "net/http" "github.com/FAU-CDI/wisski-distillery/internal/dis/component" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static/custom" "github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates" "github.com/FAU-CDI/wisski-distillery/internal/status" "github.com/FAU-CDI/wisski-distillery/pkg/lazy" ) @@ -15,8 +15,8 @@ import ( type Home struct { component.Base Dependencies struct { - Instances *instances.Instances - Custom *custom.Custom + Templating *templates.Templating + Instances *instances.Instances } instanceNames lazy.Lazy[map[string]struct{}] // instance names diff --git a/internal/dis/component/control/home/instances.go b/internal/dis/component/server/home/instances.go similarity index 100% rename from internal/dis/component/control/home/instances.go rename to internal/dis/component/server/home/instances.go diff --git a/internal/dis/component/control/home/public.go b/internal/dis/component/server/home/public.go similarity index 69% rename from internal/dis/component/control/home/public.go rename to internal/dis/component/server/home/public.go index 6f42d09..72ba878 100644 --- a/internal/dis/component/control/home/public.go +++ b/internal/dis/component/server/home/public.go @@ -7,25 +7,25 @@ import ( "strings" "github.com/FAU-CDI/wisski-distillery/internal/dis/component" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static/custom" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates" "github.com/FAU-CDI/wisski-distillery/internal/status" "github.com/FAU-CDI/wisski-distillery/pkg/httpx" ) //go:embed "public.html" var publicHTML []byte -var publicTemplate = custom.Parse[publicContext]("public.html", publicHTML, static.AssetsDefault) +var publicTemplate = templates.Parse[publicContext]("public.html", publicHTML, assets.AssetsDefault) type publicContext struct { - custom.BaseContext + templates.BaseContext Instances []status.WissKI SelfRedirect string } func (home *Home) publicHandler(ctx context.Context) http.Handler { - tpl := publicTemplate.Prepare(home.Dependencies.Custom, custom.BaseContextGaps{ + tpl := publicTemplate.Prepare(home.Dependencies.Templating, templates.BaseContextGaps{ Crumbs: []component.MenuItem{ {Title: "WissKI Distillery", Path: "/"}, }, diff --git a/internal/dis/component/control/home/public.html b/internal/dis/component/server/home/public.html similarity index 100% rename from internal/dis/component/control/home/public.html rename to internal/dis/component/server/home/public.html diff --git a/internal/dis/component/control/home/redirect.go b/internal/dis/component/server/home/redirect.go similarity index 100% rename from internal/dis/component/control/home/redirect.go rename to internal/dis/component/server/home/redirect.go diff --git a/internal/dis/component/control/legal/legal.go b/internal/dis/component/server/legal/legal.go similarity index 59% rename from internal/dis/component/control/legal/legal.go rename to internal/dis/component/server/legal/legal.go index c149822..302a60c 100644 --- a/internal/dis/component/control/legal/legal.go +++ b/internal/dis/component/server/legal/legal.go @@ -6,9 +6,9 @@ import ( "github.com/FAU-CDI/wisski-distillery/internal/cli" "github.com/FAU-CDI/wisski-distillery/internal/dis/component" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static/custom" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates" _ "embed" ) @@ -16,8 +16,8 @@ import ( type Legal struct { component.Base Dependencies struct { - Static *static.Static - Custom *custom.Custom + Static *assets.Static + Templating *templates.Templating } } @@ -27,10 +27,10 @@ var ( //go:embed "legal.html" var legalHTML []byte -var legalTemplate = custom.Parse[legalContext]("legal.html", legalHTML, static.AssetsDefault) +var legalTemplate = templates.Parse[legalContext]("legal.html", legalHTML, assets.AssetsDefault) type legalContext struct { - custom.BaseContext + templates.BaseContext LegalNotices string @@ -49,7 +49,7 @@ func (legal *Legal) Routes() component.Routes { } func (legal *Legal) HandleRoute(ctx context.Context, route string) (http.Handler, error) { - tpl := legalTemplate.Prepare(legal.Dependencies.Custom, custom.BaseContextGaps{ + tpl := legalTemplate.Prepare(legal.Dependencies.Templating, templates.BaseContextGaps{ Crumbs: []component.MenuItem{ {Title: "Legal", Path: "/legal/"}, }, @@ -58,9 +58,9 @@ func (legal *Legal) HandleRoute(ctx context.Context, route string) (http.Handler return tpl.HTMLHandler(func(r *http.Request) (lc legalContext, err error) { lc.LegalNotices = cli.LegalNotices - lc.CSRFCookie = control.CSRFCookie - lc.SessionCookie = control.SessionCookie - lc.AssetsDisclaimer = static.AssetsDisclaimer + lc.CSRFCookie = server.CSRFCookie + lc.SessionCookie = server.SessionCookie + lc.AssetsDisclaimer = assets.Disclaimer return }), nil diff --git a/internal/dis/component/control/legal/legal.html b/internal/dis/component/server/legal/legal.html similarity index 100% rename from internal/dis/component/control/legal/legal.html rename to internal/dis/component/server/legal/legal.html diff --git a/internal/dis/component/control/news/NEWS/2022-09-08-go.md b/internal/dis/component/server/news/NEWS/2022-09-08-go.md similarity index 100% rename from internal/dis/component/control/news/NEWS/2022-09-08-go.md rename to internal/dis/component/server/news/NEWS/2022-09-08-go.md diff --git a/internal/dis/component/control/news/NEWS/2022-09-09-admin.md b/internal/dis/component/server/news/NEWS/2022-09-09-admin.md similarity index 100% rename from internal/dis/component/control/news/NEWS/2022-09-09-admin.md rename to internal/dis/component/server/news/NEWS/2022-09-09-admin.md diff --git a/internal/dis/component/control/news/NEWS/2022-10-05-resolver.md b/internal/dis/component/server/news/NEWS/2022-10-05-resolver.md similarity index 100% rename from internal/dis/component/control/news/NEWS/2022-10-05-resolver.md rename to internal/dis/component/server/news/NEWS/2022-10-05-resolver.md diff --git a/internal/dis/component/control/news/NEWS/2022-10-12-http3.md b/internal/dis/component/server/news/NEWS/2022-10-12-http3.md similarity index 100% rename from internal/dis/component/control/news/NEWS/2022-10-12-http3.md rename to internal/dis/component/server/news/NEWS/2022-10-12-http3.md diff --git a/internal/dis/component/control/news/NEWS/2022-11-12-ssh.md b/internal/dis/component/server/news/NEWS/2022-11-12-ssh.md similarity index 100% rename from internal/dis/component/control/news/NEWS/2022-11-12-ssh.md rename to internal/dis/component/server/news/NEWS/2022-11-12-ssh.md diff --git a/internal/dis/component/control/news/NEWS/2022-11-16-statistics.md b/internal/dis/component/server/news/NEWS/2022-11-16-statistics.md similarity index 100% rename from internal/dis/component/control/news/NEWS/2022-11-16-statistics.md rename to internal/dis/component/server/news/NEWS/2022-11-16-statistics.md diff --git a/internal/dis/component/control/news/NEWS/2022-11-23-login.md b/internal/dis/component/server/news/NEWS/2022-11-23-login.md similarity index 100% rename from internal/dis/component/control/news/NEWS/2022-11-23-login.md rename to internal/dis/component/server/news/NEWS/2022-11-23-login.md diff --git a/internal/dis/component/control/news/NEWS/2022-11-25-password.md b/internal/dis/component/server/news/NEWS/2022-11-25-password.md similarity index 100% rename from internal/dis/component/control/news/NEWS/2022-11-25-password.md rename to internal/dis/component/server/news/NEWS/2022-11-25-password.md diff --git a/internal/dis/component/control/news/NEWS/2023-01-07-management.md b/internal/dis/component/server/news/NEWS/2023-01-07-management.md similarity index 100% rename from internal/dis/component/control/news/NEWS/2023-01-07-management.md rename to internal/dis/component/server/news/NEWS/2023-01-07-management.md diff --git a/internal/dis/component/control/news/NEWS/2023-01-09-news.md b/internal/dis/component/server/news/NEWS/2023-01-09-news.md similarity index 100% rename from internal/dis/component/control/news/NEWS/2023-01-09-news.md rename to internal/dis/component/server/news/NEWS/2023-01-09-news.md diff --git a/internal/dis/component/control/news/NEWS/2023-01-15-ssh.md b/internal/dis/component/server/news/NEWS/2023-01-15-ssh.md similarity index 100% rename from internal/dis/component/control/news/NEWS/2023-01-15-ssh.md rename to internal/dis/component/server/news/NEWS/2023-01-15-ssh.md diff --git a/internal/dis/component/control/news/NEWS/2023-01-16-remove.md b/internal/dis/component/server/news/NEWS/2023-01-16-remove.md similarity index 100% rename from internal/dis/component/control/news/NEWS/2023-01-16-remove.md rename to internal/dis/component/server/news/NEWS/2023-01-16-remove.md diff --git a/internal/dis/component/control/news/news.go b/internal/dis/component/server/news/news.go similarity index 86% rename from internal/dis/component/control/news/news.go rename to internal/dis/component/server/news/news.go index ff7b4f3..33c3db6 100644 --- a/internal/dis/component/control/news/news.go +++ b/internal/dis/component/server/news/news.go @@ -10,8 +10,8 @@ import ( "time" "github.com/FAU-CDI/wisski-distillery/internal/dis/component" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static/custom" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates" "github.com/rs/zerolog" "github.com/yuin/goldmark" gmmeta "github.com/yuin/goldmark-meta" @@ -22,7 +22,7 @@ import ( type News struct { component.Base Dependencies struct { - Custom *custom.Custom + Templating *templates.Templating } } @@ -113,16 +113,16 @@ func Items() ([]Item, error) { //go:embed "news.html" var newsHTML []byte -var newsTemplate = custom.Parse[newsContext]("news.html", newsHTML, static.AssetsDefault) +var newsTemplate = templates.Parse[newsContext]("news.html", newsHTML, assets.AssetsDefault) type newsContext struct { - custom.BaseContext + templates.BaseContext Items []Item } // HandleRoute returns the handler for the requested path func (news *News) HandleRoute(ctx context.Context, path string) (http.Handler, error) { - tpl := newsTemplate.Prepare(news.Dependencies.Custom, custom.BaseContextGaps{ + tpl := newsTemplate.Prepare(news.Dependencies.Templating, templates.BaseContextGaps{ Crumbs: []component.MenuItem{ {Title: "News", Path: "/news/"}, }, diff --git a/internal/dis/component/control/news/news.html b/internal/dis/component/server/news/news.html similarity index 100% rename from internal/dis/component/control/news/news.html rename to internal/dis/component/server/news/news.html diff --git a/internal/dis/component/control/control.env b/internal/dis/component/server/server.env similarity index 100% rename from internal/dis/component/control/control.env rename to internal/dis/component/server/server.env diff --git a/internal/dis/component/control/server.go b/internal/dis/component/server/server.go similarity index 78% rename from internal/dis/component/control/server.go rename to internal/dis/component/server/server.go index f97dac4..92e847b 100644 --- a/internal/dis/component/control/server.go +++ b/internal/dis/component/server/server.go @@ -1,4 +1,4 @@ -package control +package server import ( "context" @@ -7,6 +7,7 @@ import ( "net/http" "github.com/FAU-CDI/wisski-distillery/internal/dis/component" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates" "github.com/FAU-CDI/wisski-distillery/pkg/cancel" "github.com/FAU-CDI/wisski-distillery/pkg/httpx" "github.com/FAU-CDI/wisski-distillery/pkg/mux" @@ -14,16 +15,31 @@ import ( "github.com/rs/zerolog" ) +// Server represents the running control server. +type Server struct { + component.Base + Dependencies struct { + Routeables []component.Routeable + Cronables []component.Cronable + + Templating *templates.Templating + } +} + +var ( + _ component.Installable = (*Server)(nil) +) + // Server returns an http.Mux that implements the main server instance. // The server may spawn background tasks, but these should be terminated once context closes. // // Logging messages are directed to progress -func (control *Control) Server(ctx context.Context, progress io.Writer) (public http.Handler, internal http.Handler, err error) { +func (server *Server) Server(ctx context.Context, progress io.Writer) (public http.Handler, internal http.Handler, err error) { logger := zerolog.Ctx(ctx) var publicM, internalM mux.Mux[component.RouteContext] publicM.Context = func(r *http.Request) component.RouteContext { - slug, ok := control.Still.Config.SlugFromHost(r.Host) + slug, ok := server.Still.Config.SlugFromHost(r.Host) return component.RouteContext{ DefaultDomain: slug == "" && ok, } @@ -44,10 +60,10 @@ func (control *Control) Server(ctx context.Context, progress io.Writer) (public internalM.Context = publicM.Context // create a csrf protector - csrfProtector := control.CSRF() + csrfProtector := server.csrf() // iterate over all the handler - for _, s := range control.Dependencies.Routeables { + for _, s := range server.Dependencies.Routeables { routes := s.Routes() zerolog.Ctx(ctx).Info(). Str("Name", s.Name()). @@ -94,13 +110,13 @@ func (control *Control) Server(ctx context.Context, progress io.Writer) (public } // CSRF returns a CSRF handler for the given function -func (control *Control) CSRF() func(http.Handler) http.Handler { +func (server *Server) csrf() func(http.Handler) http.Handler { var opts []csrf.Option - if !control.Config.HTTPSEnabled() { + if !server.Config.HTTPSEnabled() { opts = append(opts, csrf.Secure(false)) } opts = append(opts, csrf.SameSite(csrf.SameSiteStrictMode)) opts = append(opts, csrf.CookieName(CSRFCookie)) opts = append(opts, csrf.FieldName(CSRFCookieField)) - return csrf.Protect(control.Config.CSRFSecret(), opts...) + return csrf.Protect(server.Config.CSRFSecret(), opts...) } diff --git a/internal/dis/component/control/control/Dockerfile b/internal/dis/component/server/server/Dockerfile similarity index 100% rename from internal/dis/component/control/control/Dockerfile rename to internal/dis/component/server/server/Dockerfile diff --git a/internal/dis/component/control/control/docker-compose.yml b/internal/dis/component/server/server/docker-compose.yml similarity index 100% rename from internal/dis/component/control/control/docker-compose.yml rename to internal/dis/component/server/server/docker-compose.yml diff --git a/internal/dis/component/server/stack.go b/internal/dis/component/server/stack.go new file mode 100644 index 0000000..aa54170 --- /dev/null +++ b/internal/dis/component/server/stack.go @@ -0,0 +1,55 @@ +package server + +import ( + "context" + "embed" + "io" + "path/filepath" + "syscall" + + "github.com/FAU-CDI/wisski-distillery/internal/bootstrap" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component" + "github.com/FAU-CDI/wisski-distillery/pkg/environment" +) + +func (control Server) Path() string { + return filepath.Join(control.Still.Config.DeployRoot, "core", "dis") +} + +//go:embed all:server server.env +var resources embed.FS + +func (server *Server) Stack(env environment.Environment) component.StackWithResources { + return component.MakeStack(server, env, component.StackWithResources{ + Resources: resources, + ContextPath: "server", + EnvPath: "server.env", + + EnvContext: map[string]string{ + "DOCKER_NETWORK_NAME": server.Config.DockerNetworkName, + "HOST_RULE": server.Config.DefaultHostRule(), + "HTTPS_ENABLED": server.Config.HTTPSEnabledEnv(), + + "CONFIG_PATH": server.Config.ConfigPath, + "DEPLOY_ROOT": server.Config.DeployRoot, + + "SELF_OVERRIDES_FILE": server.Config.SelfOverridesFile, + "SELF_RESOLVER_BLOCK_FILE": server.Config.SelfResolverBlockFile, + + "CUSTOM_ASSETS_PATH": server.Dependencies.Templating.CustomAssetsPath(), + }, + + CopyContextFiles: []string{bootstrap.Executable}, + }) +} + +// Trigger triggers the active cron run to immediatly invoke cron. +func (server *Server) Trigger(ctx context.Context, env environment.Environment) error { + return server.Stack(env).Kill(ctx, io.Discard, "control", syscall.SIGHUP) +} + +func (server *Server) Context(parent component.InstallationContext) component.InstallationContext { + return component.InstallationContext{ + bootstrap.Executable: server.Config.CurrentExecutable(server.Environment), // TODO: Does this make sense? + } +} diff --git a/internal/dis/component/server/templates/assets.go b/internal/dis/component/server/templates/assets.go new file mode 100644 index 0000000..cd2b19d --- /dev/null +++ b/internal/dis/component/server/templates/assets.go @@ -0,0 +1,22 @@ +package templates + +import ( + "path/filepath" + + "github.com/FAU-CDI/wisski-distillery/internal/dis/component" +) + +// CustomAssetsPath is the path custom assets are stored at +func (tpl *Templating) CustomAssetsPath() string { + return filepath.Join(tpl.Config.DeployRoot, "core", "assets") +} + +func (tpl *Templating) CustomAssetPath(name string) string { + return filepath.Join(tpl.CustomAssetsPath(), name) +} + +func (tpl *Templating) BackupName() string { return "custom" } + +func (tpl *Templating) Backup(context component.StagingContext) error { + return context.CopyDirectory("", tpl.CustomAssetsPath()) +} diff --git a/internal/dis/component/control/static/custom/context.go b/internal/dis/component/server/templates/context.go similarity index 88% rename from internal/dis/component/control/static/custom/context.go rename to internal/dis/component/server/templates/context.go index 481eb18..c3ccedd 100644 --- a/internal/dis/component/control/static/custom/context.go +++ b/internal/dis/component/server/templates/context.go @@ -1,4 +1,4 @@ -package custom +package templates import ( "html/template" @@ -16,9 +16,8 @@ import ( // baseContextName is the name of the [BaseContext] type var baseContextName = reflectx.TypeOf[BaseContext]().Name() -// BaseContext represents a context shared by all the templates. +// BaseContext represents a context used by templates // -// This context should always be initialized using the [custom.Update], [custom.New] or [custom.NewForm] functions. // Other invocations might cause an error at runtime. type BaseContext struct { inited bool // has this context been inited? @@ -56,11 +55,11 @@ func (bcg BaseContextGaps) clone() BaseContextGaps { } // update updates an embedded BaseContext field in context. -func (custom *Custom) update(context any, r *http.Request, bcg BaseContextGaps) *BaseContext { +func (tpl *Templating) update(context any, r *http.Request, bcg BaseContextGaps) *BaseContext { tc := reflect.ValueOf(context). Elem().FieldByName(baseContextName).Addr(). Interface().(*BaseContext) - // tc.custom = custom + tc.inited = true tc.requestWasNil = r == nil @@ -73,7 +72,7 @@ func (custom *Custom) update(context any, r *http.Request, bcg BaseContextGaps) } // build the menu - tc.Menu = custom.BuildMenu(r) + tc.Menu = tpl.buildMenu(r) // build the breadcrumbs tc.BaseContextGaps = bcg.clone() diff --git a/internal/dis/component/control/static/custom/footer.html b/internal/dis/component/server/templates/footer.html similarity index 100% rename from internal/dis/component/control/static/custom/footer.html rename to internal/dis/component/server/templates/footer.html diff --git a/internal/dis/component/control/static/custom/menu.go b/internal/dis/component/server/templates/menu.go similarity index 56% rename from internal/dis/component/control/static/custom/menu.go rename to internal/dis/component/server/templates/menu.go index 1b4c057..d33a341 100644 --- a/internal/dis/component/control/static/custom/menu.go +++ b/internal/dis/component/server/templates/menu.go @@ -1,4 +1,4 @@ -package custom +package templates import ( "html/template" @@ -9,11 +9,30 @@ import ( "golang.org/x/exp/slices" ) -// getMenuItems gets a fresh copy of the cached slice of menu items -func (custom *Custom) Menu(r *http.Request) []component.MenuItem { - return custom.menu.Get(func() []component.MenuItem { - items := make([]component.MenuItem, 0, len(custom.Dependencies.Routeables)) - for _, route := range custom.Dependencies.Routeables { +// buildMenu builds the manu for this request for all known components in this distillery. +// +// NOTE(twiesing): Don't name this method "Menu", as it will cause a stack overflow. +func (tpl *Templating) buildMenu(r *http.Request) []component.MenuItem { + + path := mux.NormalizePath(r.URL.Path) + + // get the static menu items, and then return all the regular ones + var items []component.MenuItem + for _, m := range tpl.Dependencies.Menuable { + items = append(items, m.Menu(r)...) + } + for i, item := range items { + items[i].Active = string(item.Path) == path + } + slices.SortFunc(items, component.MenuItemSort) + return items +} + +// Menu returns a list of menu items provided by routeables +func (tpl *Templating) Menu(r *http.Request) []component.MenuItem { + return tpl.menu.Get(func() []component.MenuItem { + items := make([]component.MenuItem, 0, len(tpl.Dependencies.Routeables)) + for _, route := range tpl.Dependencies.Routeables { routes := route.Routes() if routes.MenuTitle == "" { continue @@ -28,20 +47,3 @@ func (custom *Custom) Menu(r *http.Request) []component.MenuItem { return items }) } - -func (custom *Custom) BuildMenu(r *http.Request) []component.MenuItem { - // NOTE(twiesing): Don't name this method "Menu", as it will cause - // a stack overflow. - path := mux.NormalizePath(r.URL.Path) - - // get the static menu items, and then return all the regular ones - var items []component.MenuItem - for _, m := range custom.Dependencies.Menuable { - items = append(items, m.Menu(r)...) - } - for i, item := range items { - items[i].Active = string(item.Path) == path - } - slices.SortFunc(items, component.MenuItemSort) - return items -} diff --git a/internal/dis/component/control/static/custom/new.go b/internal/dis/component/server/templates/new.go similarity index 92% rename from internal/dis/component/control/static/custom/new.go rename to internal/dis/component/server/templates/new.go index be0e54e..8721df8 100644 --- a/internal/dis/component/control/static/custom/new.go +++ b/internal/dis/component/server/templates/new.go @@ -1,10 +1,10 @@ -package custom +package templates import ( "html/template" "net/http" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets" "github.com/FAU-CDI/wisski-distillery/pkg/httpx" ) @@ -15,7 +15,7 @@ type Parsed[C any] struct { // Parse creates a new Parsed from a template source. // Parse calls panic() when parsing fails. -func Parse[C any](name string, source []byte, Assets static.Assets) Parsed[C] { +func Parse[C any](name string, source []byte, Assets assets.Assets) Parsed[C] { return Parsed[C]{ template: Assets.MustParseShared(name, string(source)), } @@ -23,10 +23,10 @@ func Parse[C any](name string, source []byte, Assets static.Assets) Parsed[C] { // Prepare prepares this template for use inside a concrete handler. // gaps must either be of length 0 or length 1 and may pre-fill gaps to be used when executing the template later. -func (p *Parsed[C]) Prepare(custom *Custom, gaps ...BaseContextGaps) *Template[C] { +func (p *Parsed[C]) Prepare(tpl *Templating, gaps ...BaseContextGaps) *Template[C] { wrap := Template[C]{ - custom: custom, - template: custom.Template(p.template), + tpl: tpl, + template: tpl.Template(p.template), } if len(gaps) > 1 { panic("WrapTemplate: must provide either 1 or no gaps") @@ -39,7 +39,7 @@ func (p *Parsed[C]) Prepare(custom *Custom, gaps ...BaseContextGaps) *Template[C // Tempalte represents an executable template. type Template[C any] struct { - custom *Custom + tpl *Templating template *template.Template gaps BaseContextGaps } @@ -62,7 +62,7 @@ func (tw *Template[C]) Context(r *http.Request, c C, gaps ...BaseContextGaps) an if len(gaps) == 1 { g = gaps[0] } - tw.custom.update(&c, r, g) + tw.tpl.update(&c, r, g) } return c diff --git a/internal/dis/component/control/static/custom/template.go b/internal/dis/component/server/templates/template.go similarity index 69% rename from internal/dis/component/control/static/custom/template.go rename to internal/dis/component/server/templates/template.go index 267f589..85574e4 100644 --- a/internal/dis/component/control/static/custom/template.go +++ b/internal/dis/component/server/templates/template.go @@ -1,4 +1,4 @@ -package custom +package templates import ( _ "embed" @@ -19,16 +19,18 @@ var defaultFooterTemplate = template.Must(template.New("footer.html").Parse(foot // Template creates a copy of template with shared template parts updated accordingly. // Any template using this should use one of the template contexts in this package. -func (custom *Custom) Template(tpl *template.Template) *template.Template { +func (tpl *Templating) Template(t *template.Template) *template.Template { + // TODO: This should not be used! + // create a clone of the template - clone := template.Must(tpl.Clone()) + clone := template.Must(t.Clone()) // add all the fixed parse trees - footerTree := custom.getTemplateAsset(defaultFooterTemplate) + footerTree := tpl.getTemplateAsset(defaultFooterTemplate) template.Must(clone.AddParseTree(footerName, footerTree)) // optionally add the about asset - if aboutTree := custom.readTemplateAsset("about.html"); clone.Lookup(aboutName) != nil && aboutTree != nil { + if aboutTree := tpl.readTemplateAsset("about.html"); clone.Lookup(aboutName) != nil && aboutTree != nil { template.Must(clone.AddParseTree(aboutName, aboutTree)) } return clone // and return the tree @@ -38,8 +40,8 @@ func (custom *Custom) Template(tpl *template.Template) *template.Template { // // If the asset named can successfully be parsed, it is returned. // If it can not be parsed, the default template is returned. -func (custom *Custom) getTemplateAsset(dflt *template.Template) *parse.Tree { - tree := custom.readTemplateAsset(dflt.Name()) +func (tpl *Templating) getTemplateAsset(dflt *template.Template) *parse.Tree { + tree := tpl.readTemplateAsset(dflt.Name()) if tree == nil { return dflt.Tree.Copy() } @@ -48,9 +50,9 @@ func (custom *Custom) getTemplateAsset(dflt *template.Template) *parse.Tree { // readTemplateAsset is like getTemplateAssets, but takes an explicit name to read. // when the asset does not exist, or cannot be opened, returns nil. -func (custom *Custom) readTemplateAsset(name string) *parse.Tree { +func (tpl *Templating) readTemplateAsset(name string) *parse.Tree { template, err := (func() (*template.Template, error) { - data, err := environment.ReadFile(custom.Environment, custom.CustomAssetPath(name)) + data, err := environment.ReadFile(tpl.Environment, tpl.CustomAssetPath(name)) if err != nil { return nil, err } diff --git a/internal/dis/component/control/static/custom/custom.go b/internal/dis/component/server/templates/templating.go similarity index 60% rename from internal/dis/component/control/static/custom/custom.go rename to internal/dis/component/server/templates/templating.go index a463fa1..a83ca43 100644 --- a/internal/dis/component/control/static/custom/custom.go +++ b/internal/dis/component/server/templates/templating.go @@ -1,12 +1,12 @@ -package custom +package templates import ( "github.com/FAU-CDI/wisski-distillery/internal/dis/component" "github.com/FAU-CDI/wisski-distillery/pkg/lazy" ) -// Custom implements theme and page customization. -type Custom struct { +// Templating implements templating customization +type Templating struct { component.Base Dependencies struct { Routeables []component.Routeable @@ -16,6 +16,6 @@ type Custom struct { } var ( - _ component.Backupable = (*Custom)(nil) - _ component.Menuable = (*Custom)(nil) + _ component.Backupable = (*Templating)(nil) + _ component.Menuable = (*Templating)(nil) ) diff --git a/internal/dis/distillery.go b/internal/dis/distillery.go index f57104a..4a0338f 100644 --- a/internal/dis/distillery.go +++ b/internal/dis/distillery.go @@ -11,14 +11,6 @@ import ( "github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth/next" "github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth/panel" "github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth/policy" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/admin" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/cron" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/home" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/legal" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/news" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static" - "github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static/custom" "github.com/FAU-CDI/wisski-distillery/internal/dis/component/exporter" "github.com/FAU-CDI/wisski-distillery/internal/dis/component/exporter/logger" "github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances" @@ -26,6 +18,14 @@ import ( "github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances/purger" "github.com/FAU-CDI/wisski-distillery/internal/dis/component/meta" "github.com/FAU-CDI/wisski-distillery/internal/dis/component/resolver" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/admin" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/cron" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/home" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/legal" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/news" + "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates" "github.com/FAU-CDI/wisski-distillery/internal/dis/component/solr" "github.com/FAU-CDI/wisski-distillery/internal/dis/component/sql" "github.com/FAU-CDI/wisski-distillery/internal/dis/component/ssh2" @@ -68,8 +68,8 @@ type Upstream struct { // PUBLIC COMPONENT GETTERS // -func (dis *Distillery) Control() *control.Control { - return export[*control.Control](dis) +func (dis *Distillery) Control() *server.Server { + return export[*server.Server](dis) } func (dis *Distillery) Resolver() *resolver.Resolver { return export[*resolver.Resolver](dis) @@ -116,8 +116,8 @@ func (dis *Distillery) Info() *admin.Admin { func (dis *Distillery) Policy() *policy.Policy { return export[*policy.Policy](dis) } -func (dis *Distillery) Custom() *custom.Custom { - return export[*custom.Custom](dis) +func (dis *Distillery) Templating() *templates.Templating { + return export[*templates.Templating](dis) } func (dis *Distillery) Purger() *purger.Purger { @@ -177,7 +177,7 @@ func (dis *Distillery) allComponents() []initFunc { auto[*sshkeys.SSHKeys], // Control server - auto[*control.Control], + auto[*server.Server], auto[*home.Home], manual(func(resolver *resolver.Resolver) { @@ -189,8 +189,8 @@ func (dis *Distillery) allComponents() []initFunc { auto[*legal.Legal], auto[*news.News], - auto[*static.Static], - auto[*custom.Custom], + auto[*assets.Static], + auto[*templates.Templating], // Cron auto[*cron.Cron],