Add support for custom footer

This commit is contained in:
Tom Wiesing 2023-01-08 08:13:47 +01:00
parent 9f3e7a7b86
commit a292c25f84
No known key found for this signature in database
7 changed files with 60 additions and 3 deletions

View file

@ -7,4 +7,6 @@ SELF_OVERRIDES_FILE=${SELF_OVERRIDES_FILE}
SELF_RESOLVER_BLOCK_FILE=${SELF_RESOLVER_BLOCK_FILE}
DOCKER_NETWORK_NAME=${DOCKER_NETWORK_NAME}
HTTPS_ENABLED=${HTTPS_ENABLED}
HTTPS_ENABLED=${HTTPS_ENABLED}
CUSTOM_ASSETS_PATH=${CUSTOM_ASSETS_PATH}

View file

@ -9,6 +9,7 @@ import (
"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"
)
@ -18,6 +19,8 @@ type Control struct {
Dependencies struct {
Routeables []component.Routeable
Cronables []component.Cronable
Custom *custom.Custom
}
}
@ -49,6 +52,8 @@ func (control *Control) Stack(env environment.Environment) component.StackWithRe
"GLOBAL_AUTHORIZED_KEYS_FILE": control.Config.GlobalAuthorizedKeysFile,
"SELF_OVERRIDES_FILE": control.Config.SelfOverridesFile,
"SELF_RESOLVER_BLOCK_FILE": control.Config.SelfResolverBlockFile,
"CUSTOM_ASSETS_PATH": control.Dependencies.Custom.CustomAssetsPath(),
},
CopyContextFiles: []string{bootstrap.Executable},

View file

@ -28,6 +28,7 @@ services:
- "${GLOBAL_AUTHORIZED_KEYS_FILE}:${GLOBAL_AUTHORIZED_KEYS_FILE}:ro"
- "${SELF_OVERRIDES_FILE}:${SELF_OVERRIDES_FILE}:ro"
- "${SELF_RESOLVER_BLOCK_FILE}:${SELF_RESOLVER_BLOCK_FILE}:ro"
- "${CUSTOM_ASSETS_PATH}:${CUSTOM_ASSETS_PATH}:ro"
networks:
default:

View file

@ -0,0 +1,22 @@
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) FooterPath() string {
return filepath.Join(custom.CustomAssetsPath(), "footer.html")
}
func (custom *Custom) BackupName() string { return "custom" }
func (custom *Custom) Backup(context component.StagingContext) error {
return context.CopyDirectory("", custom.CustomAssetsPath())
}

View file

@ -1,6 +1,8 @@
package custom
import "github.com/FAU-CDI/wisski-distillery/internal/dis/component"
import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
)
// Custom implements theme and page customization.
type Custom struct {
@ -9,3 +11,7 @@ type Custom struct {
// nothing yet
}
}
var (
_ component.Backupable = (*Custom)(nil)
)

View file

@ -3,6 +3,9 @@ package custom
import (
_ "embed"
"html/template"
"text/template/parse"
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
)
const footerName = "footer"
@ -14,9 +17,26 @@ var defaultTemplate = template.Must(template.New("footer.html").Parse(defaultTem
// 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 {
tree := defaultTemplate.Tree.Copy()
tree := custom.footerTemplate()
clone := template.Must(tpl.Clone()) // create a clone of the template
template.Must(clone.AddParseTree(footerName, tree)) // add the parse tree to it
return clone // and return the tree
}
// footerTemplate returns a new copy of the footer template
func (custom *Custom) footerTemplate() *parse.Tree {
footer, err := (func() (*template.Template, error) {
data, err := environment.ReadFile(custom.Environment, custom.FooterPath())
if err != nil {
return nil, err
}
return template.New("footer.html").Parse(string(data))
})()
if err != nil {
return defaultTemplate.Tree.Copy()
}
return footer.Tree
}