Home,News: Seperate data and rendering

This commit is contained in:
Tom Wiesing 2023-01-09 14:11:42 +01:00
parent dcd5f910ae
commit a1069f115e
No known key found for this signature in database
10 changed files with 187 additions and 200 deletions

View file

@ -1,83 +0,0 @@
package home
import (
"context"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
)
type UpdateInstanceList struct {
component.Base
Dependencies struct {
Home *Home
}
}
var (
_ component.Cronable = (*UpdateInstanceList)(nil)
)
func (*UpdateInstanceList) TaskName() string {
return "instance list"
}
func (ul *UpdateInstanceList) Cron(ctx context.Context) error {
names, err := ul.Dependencies.Home.instanceMap(ctx)
if err != nil {
return err
}
ul.Dependencies.Home.instanceNames.Set(names)
return nil
}
type UpdateRedirect struct {
component.Base
Dependencies struct {
Home *Home
}
}
var (
_ component.Cronable = (*UpdateRedirect)(nil)
)
func (ur *UpdateRedirect) TaskName() string {
return "redirect"
}
func (ur *UpdateRedirect) Cron(ctx context.Context) error {
redirect, err := ur.Dependencies.Home.loadRedirect(ctx)
if err != nil {
return err
}
ur.Dependencies.Home.redirect.Set(&redirect)
return nil
}
type UpdateHome struct {
component.Base
Dependencies struct {
Home *Home
}
}
var (
_ component.Cronable = (*UpdateHome)(nil)
)
func (ur *UpdateHome) TaskName() string {
return "home render"
}
func (ur *UpdateHome) Cron(ctx context.Context) error {
bytes, err := ur.Dependencies.Home.homeRender(ctx)
if err != nil {
return err
}
ur.Dependencies.Home.homeBytes.Set(httpx.MinifyHTML(bytes))
return nil
}

View file

@ -3,12 +3,12 @@ package home
import ( import (
"context" "context"
"fmt" "fmt"
"html/template"
"net/http" "net/http"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component" "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/control/static/custom"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances" "github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances"
"github.com/FAU-CDI/wisski-distillery/internal/status"
"github.com/FAU-CDI/wisski-distillery/pkg/lazy" "github.com/FAU-CDI/wisski-distillery/pkg/lazy"
) )
@ -19,10 +19,8 @@ type Home struct {
Custom *custom.Custom Custom *custom.Custom
} }
redirect lazy.Lazy[*Redirect] instanceNames lazy.Lazy[map[string]struct{}] // instance names
instanceNames lazy.Lazy[map[string]struct{}] homeInstances lazy.Lazy[[]status.WissKI] // list of home instances (updated via cron)
homeBytes lazy.Lazy[[]byte]
homeTemplate lazy.Lazy[*template.Template]
} }
var ( var (
@ -37,10 +35,14 @@ func (*Home) Routes() component.Routes {
} }
func (home *Home) HandleRoute(ctx context.Context, route string) (http.Handler, error) { func (home *Home) HandleRoute(ctx context.Context, route string) (http.Handler, error) {
return home, nil // generate a default handler
} dflt, err := home.loadRedirect(ctx)
if err != nil {
return nil, err
}
dflt.Fallback = home.publicHandler(ctx)
func (home *Home) ServeHTTP(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
slug, ok := home.Config.SlugFromHost(r.Host) slug, ok := home.Config.SlugFromHost(r.Host)
switch { switch {
case !ok: case !ok:
@ -48,20 +50,22 @@ func (home *Home) ServeHTTP(w http.ResponseWriter, r *http.Request) {
case slug != "": case slug != "":
home.serveWissKI(w, slug, r) home.serveWissKI(w, slug, r)
default: default:
home.serveRoot(w, r) dflt.ServeHTTP(w, r)
} }
}), nil
} }
func (home *Home) serveRoot(w http.ResponseWriter, r *http.Request) { func (home *Home) instanceMap(ctx context.Context) (map[string]struct{}, error) {
// not the root url => server the fallback wissKIs, err := home.Dependencies.Instances.All(ctx)
if !(r.URL.Path == "" || r.URL.Path == "/") { if err != nil {
home.redirect.Get(nil).ServeHTTP(w, r) return nil, err
return
} }
w.Header().Set("Content-Type", "text/html") names := make(map[string]struct{}, len(wissKIs))
w.WriteHeader(http.StatusAccepted) for _, w := range wissKIs {
w.Write(home.homeBytes.Get(nil)) names[w.Slug] = struct{}{}
}
return names, nil
} }
func (home *Home) serveWissKI(w http.ResponseWriter, slug string, r *http.Request) { func (home *Home) serveWissKI(w http.ResponseWriter, slug string, r *http.Request) {

View file

@ -0,0 +1,86 @@
package home
import (
"context"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/status"
"golang.org/x/sync/errgroup"
)
// loadInstances loads all the instances into the home route
func (home *Home) loadInstances(ctx context.Context) ([]status.WissKI, error) {
// find all the WissKIs
wissKIs, err := home.Dependencies.Instances.All(ctx)
if err != nil {
return nil, err
}
instances := make([]status.WissKI, len(wissKIs))
// determine their infos
var eg errgroup.Group
for i, instance := range wissKIs {
i := i
wissKI := instance
eg.Go(func() (err error) {
instances[i], err = wissKI.Info().Information(ctx, false)
return
})
}
eg.Wait()
// and return the new instances
return instances, nil
}
// UpdateInstanceList updates the instances list of the home struct
type UpdateInstanceList struct {
component.Base
Dependencies struct {
Home *Home
}
}
var (
_ component.Cronable = (*UpdateInstanceList)(nil)
)
func (*UpdateInstanceList) TaskName() string {
return "instance list"
}
func (ul *UpdateInstanceList) Cron(ctx context.Context) error {
names, err := ul.Dependencies.Home.instanceMap(ctx)
if err != nil {
return err
}
ul.Dependencies.Home.instanceNames.Set(names)
return nil
}
type UpdateHome struct {
component.Base
Dependencies struct {
Home *Home
}
}
var (
_ component.Cronable = (*UpdateHome)(nil)
)
func (ur *UpdateHome) TaskName() string {
return "home instances fetch"
}
func (ur *UpdateHome) Cron(ctx context.Context) error {
instances, err := ur.Dependencies.Home.loadInstances(ctx)
if err != nil {
return err
}
ur.Dependencies.Home.homeInstances.Set(instances)
return nil
}

View file

@ -1,77 +1,43 @@
package home package home
import ( import (
"bytes"
"context" "context"
"html/template"
"time"
_ "embed" _ "embed"
"net/http"
"strings"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static" "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/control/static/custom"
"github.com/FAU-CDI/wisski-distillery/internal/status" "github.com/FAU-CDI/wisski-distillery/internal/status"
"golang.org/x/sync/errgroup" "github.com/FAU-CDI/wisski-distillery/pkg/httpx"
) )
func (home *Home) instanceMap(ctx context.Context) (map[string]struct{}, error) { //go:embed "public.html"
wissKIs, err := home.Dependencies.Instances.All(ctx) var publicHTMLStr string
if err != nil { var publicTemplate = static.AssetsHome.MustParseShared("public.html", publicHTMLStr)
return nil, err
}
names := make(map[string]struct{}, len(wissKIs)) type publicContext struct {
for _, w := range wissKIs {
names[w.Slug] = struct{}{}
}
return names, nil
}
//go:embed "home.html"
var homeHTMLStr string
var homeTemplate = static.AssetsHome.MustParseShared("home.html", homeHTMLStr)
func (home *Home) homeRender(ctx context.Context) ([]byte, error) {
var context homeContext
home.Dependencies.Custom.Update(&context, nil)
// setup a couple of static things
context.Time = time.Now().UTC()
context.SelfRedirect = home.Config.SelfRedirect.String()
// find all the WissKIs
wissKIs, err := home.Dependencies.Instances.All(ctx)
if err != nil {
return nil, err
}
context.Instances = make([]status.WissKI, len(wissKIs))
// determine their infos
var eg errgroup.Group
for i, instance := range wissKIs {
i := i
wissKI := instance
eg.Go(func() (err error) {
context.Instances[i], err = wissKI.Info().Information(ctx, false)
return
})
}
eg.Wait()
// render the template
var buffer bytes.Buffer
err = home.homeTemplate.Get(func() *template.Template {
return home.Dependencies.Custom.Template(homeTemplate)
}).Execute(&buffer, context)
return buffer.Bytes(), err
}
type homeContext struct {
custom.BaseContext custom.BaseContext
Instances []status.WissKI Instances []status.WissKI
Time time.Time
SelfRedirect string SelfRedirect string
} }
func (home *Home) publicHandler(ctx context.Context) http.Handler {
return httpx.HTMLHandler[publicContext]{
Handler: func(r *http.Request) (pc publicContext, err error) {
// only act on the root path!
if strings.TrimSuffix(r.URL.Path, "/") != "" {
return pc, httpx.ErrNotFound
}
home.Dependencies.Custom.Update(&pc, r)
pc.Instances = home.homeInstances.Get(nil)
pc.SelfRedirect = home.Config.SelfRedirect.String()
return
},
Template: home.Dependencies.Custom.Template(publicTemplate),
}
}

View file

@ -11,7 +11,8 @@ func (home *Home) loadRedirect(ctx context.Context) (redirect Redirect, err erro
if redirect.Overrides == nil { if redirect.Overrides == nil {
redirect.Overrides = make(map[string]string) redirect.Overrides = make(map[string]string)
} }
redirect.Overrides[""] = home.Config.SelfRedirect.String()
delete(redirect.Overrides, "") // make sure there is no root redirect
redirect.Absolute = false redirect.Absolute = false
redirect.Permanent = false redirect.Permanent = false

View file

@ -13,7 +13,7 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static" "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/control/static/custom"
"github.com/FAU-CDI/wisski-distillery/pkg/httpx" "github.com/FAU-CDI/wisski-distillery/pkg/httpx"
"github.com/FAU-CDI/wisski-distillery/pkg/lazy" "github.com/rs/zerolog"
"github.com/yuin/goldmark" "github.com/yuin/goldmark"
gmmeta "github.com/yuin/goldmark-meta" gmmeta "github.com/yuin/goldmark-meta"
"github.com/yuin/goldmark/parser" "github.com/yuin/goldmark/parser"
@ -84,22 +84,20 @@ func (item *Item) parse(path string, builder *strings.Builder) error {
//go:embed "NEWS/*.md" //go:embed "NEWS/*.md"
var newsFS embed.FS var newsFS embed.FS
var news lazy.Lazy[[]Item]
// Items returns a list of all news items // Items returns a list of all news items
func Items() []Item { func Items() ([]Item, error) {
return news.Get(func() (items []Item) {
var builder strings.Builder var builder strings.Builder
files, err := fs.Glob(newsFS, "NEWS/*.md") files, err := fs.Glob(newsFS, "NEWS/*.md")
if err != nil { if err != nil {
panic(err) return nil, err
} }
items = make([]Item, len(files))
items := make([]Item, len(files))
for i, file := range files { for i, file := range files {
items[i].ID = file[len("NEWS/") : len(file)-len(".md")] items[i].ID = file[len("NEWS/") : len(file)-len(".md")]
if err := items[i].parse(file, &builder); err != nil { if err := items[i].parse(file, &builder); err != nil {
panic(err) return nil, err
} }
} }
@ -107,8 +105,7 @@ func Items() []Item {
return !a.Date.Before(b.Date) return !a.Date.Before(b.Date)
}) })
return return items, nil
})
} }
//go:embed "news.html" //go:embed "news.html"
@ -122,14 +119,22 @@ type newsContext struct {
// HandleRoute returns the handler for the requested path // HandleRoute returns the handler for the requested path
func (news *News) HandleRoute(ctx context.Context, path string) (http.Handler, error) { func (news *News) HandleRoute(ctx context.Context, path string) (http.Handler, error) {
newsTemplate := news.Dependencies.Custom.Template(newsTemplate) items, itemsErr := Items()
if itemsErr != nil {
zerolog.Ctx(ctx).Err(itemsErr).Msg("Unable to load news items")
}
return httpx.HTMLHandler[newsContext]{ return httpx.HTMLHandler[newsContext]{
Handler: func(r *http.Request) (nc newsContext, err error) { Handler: func(r *http.Request) (nc newsContext, err error) {
if strings.TrimSuffix(r.URL.Path, "/") != strings.TrimSuffix(path, "/") {
return nc, httpx.ErrNotFound
}
news.Dependencies.Custom.Update(&nc, r) news.Dependencies.Custom.Update(&nc, r)
nc.Items = Items() nc.Items, err = items, itemsErr
return return
}, },
Template: newsTemplate, Template: news.Dependencies.Custom.Template(newsTemplate),
}, nil }, nil
} }

View file

@ -20,7 +20,8 @@ var baseContextName = reflectx.TypeOf[BaseContext]().Name()
// This context should always be initialized using the [custom.Update], [custom.New] or [custom.NewForm] functions. // This context should always be initialized using the [custom.Update], [custom.New] or [custom.NewForm] functions.
// Other invocations might cause an error at runtime. // Other invocations might cause an error at runtime.
type BaseContext struct { type BaseContext struct {
inited bool // has this context been inited inited bool // has this context been inited?
requestWasNil bool // was the passed request nil
GeneratedAt time.Time // time this page was generated at GeneratedAt time.Time // time this page was generated at
@ -33,13 +34,18 @@ const (
errorSuffix template.HTML = "</div>" errorSuffix template.HTML = "</div>"
csrfError template.HTML = errorPrefix + "CSRF used but not provided" + errorSuffix csrfError template.HTML = errorPrefix + "CSRF used but not provided" + errorSuffix
initError template.HTML = errorPrefix + "BaseContext not initialized" + errorSuffix initError template.HTML = errorPrefix + "<code>BaseContext.use()</code> not called" + errorSuffix
requestNilError template.HTML = errorPrefix + "<code>BaseContext.use()</code> called with nil request" + errorSuffix
) )
// Use updates this context to use the values from the given base. // Use updates this context to use the values from the given base.
//
// The given request *must not* be nil.
//
// For convenience the passed context is also returned. // For convenience the passed context is also returned.
func (tc *BaseContext) use(base component.Base, r *http.Request) *BaseContext { func (tc *BaseContext) use(base component.Base, r *http.Request) *BaseContext {
tc.inited = true tc.inited = true
tc.requestWasNil = r == nil
tc.GeneratedAt = time.Now().UTC() tc.GeneratedAt = time.Now().UTC()
@ -56,6 +62,9 @@ func (bc BaseContext) DoInitCheck() template.HTML {
if !bc.inited { if !bc.inited {
return initError return initError
} }
if bc.requestWasNil {
return requestNilError
}
return "" return ""
} }

View file

@ -4,7 +4,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8"> <meta charset="utf-8">
<title>{{ block "block" . }}WissKI Distillery{{ end }}</title> <title>{{ block "title" . }}WissKI Distillery{{ end }}</title>
{{ block "styles" . }}styles{{ end }} {{ block "styles" . }}styles{{ end }}
</head> </head>

View file

@ -183,6 +183,5 @@ func (dis *Distillery) allComponents() []initFunc {
auto[*cron.Cron], auto[*cron.Cron],
auto[*home.UpdateHome], auto[*home.UpdateHome],
auto[*home.UpdateInstanceList], auto[*home.UpdateInstanceList],
auto[*home.UpdateRedirect],
} }
} }