wisski-cloud-distillery/internal/dis/component/server/legal/legal.go
Tom Wiesing d235ee4e5c
Refactor html templates
This commit entirely refactors the use of html templates. Instead of
inheriting from a shared template, we insert the results into a base
template.
2023-01-23 11:41:20 +01:00

73 lines
1.5 KiB
Go

package legal
import (
"context"
"net/http"
"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/server"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templating"
_ "embed"
)
type Legal struct {
component.Base
Dependencies struct {
Static *assets.Static
Templating *templating.Templating
}
}
var (
_ component.Routeable = (*Legal)(nil)
)
//go:embed "legal.html"
var legalHTML []byte
var legalTemplate = templating.Parse[legalContext](
"legal.html", legalHTML, nil,
templating.Title("Legal"),
templating.Assets(assets.AssetsDefault),
)
type legalContext struct {
templating.RuntimeFlags
LegalNotices string
CSRFCookie string
SessionCookie string
AssetsDisclaimer string
}
func (legal *Legal) Routes() component.Routes {
return component.Routes{
Prefix: "/legal/",
Exact: true,
CSRF: false,
}
}
func (legal *Legal) HandleRoute(ctx context.Context, route string) (http.Handler, error) {
tpl := legalTemplate.Prepare(
legal.Dependencies.Templating,
templating.Crumbs(
component.MenuItem{Title: "Legal", Path: "/legal/"},
),
)
return tpl.HTMLHandler(func(r *http.Request) (lc legalContext, err error) {
lc.LegalNotices = cli.LegalNotices
lc.CSRFCookie = server.CSRFCookie
lc.SessionCookie = server.SessionCookie
lc.AssetsDisclaimer = assets.Disclaimer
return
}), nil
}