resolver: Use custom template

This commit is contained in:
Tom Wiesing 2023-01-08 08:43:31 +01:00
parent a292c25f84
commit 729b9cfb51
No known key found for this signature in database
4 changed files with 102 additions and 6 deletions

View file

@ -10,15 +10,21 @@ import (
"github.com/FAU-CDI/wdresolve"
"github.com/FAU-CDI/wdresolve/resolvers"
"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/pkg/httpx"
"github.com/FAU-CDI/wisski-distillery/pkg/lazy"
"github.com/rs/zerolog"
_ "embed"
)
type Resolver struct {
component.Base
Dependencies struct {
Instances *instances.Instances
Custom *custom.Custom
}
prefixes lazy.Lazy[map[string]string] // cached prefixes (from the server)
@ -39,11 +45,30 @@ func (resolver *Resolver) Routes() component.Routes {
}
}
//go:embed "resolver.html"
var resolverHTMLStr string
var resolverTemplate = static.AssetsHome.MustParseShared("resolver.html", resolverHTMLStr)
type resolverContext struct {
custom.BaseContext
wdresolve.IndexContext
}
func (resolver *Resolver) HandleRoute(ctx context.Context, route string) (http.Handler, error) {
resolverTemplate := resolver.Dependencies.Custom.Template(resolverTemplate)
logger := zerolog.Ctx(ctx)
var err error
return resolver.handler.Get(func() (p wdresolve.ResolveHandler) {
p.HandleIndex = func(context wdresolve.IndexContext, w http.ResponseWriter, r *http.Request) {
ctx := resolverContext{
IndexContext: context,
}
resolver.Dependencies.Custom.Update(&ctx, r)
httpx.WriteHTML(ctx, nil, resolverTemplate, "", w, r)
}
p.TrustXForwardedProto = true
fallback := &resolvers.Regexp{