Home,News: Seperate data and rendering
This commit is contained in:
parent
dcd5f910ae
commit
a1069f115e
10 changed files with 187 additions and 200 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
@ -3,12 +3,12 @@ package home
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"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/status"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/lazy"
|
||||
)
|
||||
|
||||
|
|
@ -19,10 +19,8 @@ type Home struct {
|
|||
Custom *custom.Custom
|
||||
}
|
||||
|
||||
redirect lazy.Lazy[*Redirect]
|
||||
instanceNames lazy.Lazy[map[string]struct{}]
|
||||
homeBytes lazy.Lazy[[]byte]
|
||||
homeTemplate lazy.Lazy[*template.Template]
|
||||
instanceNames lazy.Lazy[map[string]struct{}] // instance names
|
||||
homeInstances lazy.Lazy[[]status.WissKI] // list of home instances (updated via cron)
|
||||
}
|
||||
|
||||
var (
|
||||
|
|
@ -37,10 +35,14 @@ func (*Home) Routes() component.Routes {
|
|||
}
|
||||
|
||||
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)
|
||||
switch {
|
||||
case !ok:
|
||||
|
|
@ -48,20 +50,22 @@ func (home *Home) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
case slug != "":
|
||||
home.serveWissKI(w, slug, r)
|
||||
default:
|
||||
home.serveRoot(w, r)
|
||||
dflt.ServeHTTP(w, r)
|
||||
}
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (home *Home) serveRoot(w http.ResponseWriter, r *http.Request) {
|
||||
// not the root url => server the fallback
|
||||
if !(r.URL.Path == "" || r.URL.Path == "/") {
|
||||
home.redirect.Get(nil).ServeHTTP(w, r)
|
||||
return
|
||||
func (home *Home) instanceMap(ctx context.Context) (map[string]struct{}, error) {
|
||||
wissKIs, err := home.Dependencies.Instances.All(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
w.Write(home.homeBytes.Get(nil))
|
||||
names := make(map[string]struct{}, len(wissKIs))
|
||||
for _, w := range wissKIs {
|
||||
names[w.Slug] = struct{}{}
|
||||
}
|
||||
return names, nil
|
||||
}
|
||||
|
||||
func (home *Home) serveWissKI(w http.ResponseWriter, slug string, r *http.Request) {
|
||||
|
|
|
|||
86
internal/dis/component/control/home/instances.go
Normal file
86
internal/dis/component/control/home/instances.go
Normal 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
|
||||
}
|
||||
|
|
@ -1,77 +1,43 @@
|
|||
package home
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"html/template"
|
||||
"time"
|
||||
|
||||
_ "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/custom"
|
||||
"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) {
|
||||
wissKIs, err := home.Dependencies.Instances.All(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
//go:embed "public.html"
|
||||
var publicHTMLStr string
|
||||
var publicTemplate = static.AssetsHome.MustParseShared("public.html", publicHTMLStr)
|
||||
|
||||
names := make(map[string]struct{}, len(wissKIs))
|
||||
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 {
|
||||
type publicContext struct {
|
||||
custom.BaseContext
|
||||
|
||||
Instances []status.WissKI
|
||||
|
||||
Time time.Time
|
||||
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,8 @@ func (home *Home) loadRedirect(ctx context.Context) (redirect Redirect, err erro
|
|||
if redirect.Overrides == nil {
|
||||
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.Permanent = false
|
||||
|
|
|
|||
|
|
@ -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/custom"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/lazy"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/yuin/goldmark"
|
||||
gmmeta "github.com/yuin/goldmark-meta"
|
||||
"github.com/yuin/goldmark/parser"
|
||||
|
|
@ -84,22 +84,20 @@ func (item *Item) parse(path string, builder *strings.Builder) error {
|
|||
//go:embed "NEWS/*.md"
|
||||
var newsFS embed.FS
|
||||
|
||||
var news lazy.Lazy[[]Item]
|
||||
|
||||
// Items returns a list of all news items
|
||||
func Items() []Item {
|
||||
return news.Get(func() (items []Item) {
|
||||
func Items() ([]Item, error) {
|
||||
var builder strings.Builder
|
||||
|
||||
files, err := fs.Glob(newsFS, "NEWS/*.md")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return nil, err
|
||||
}
|
||||
items = make([]Item, len(files))
|
||||
|
||||
items := make([]Item, len(files))
|
||||
for i, file := range files {
|
||||
items[i].ID = file[len("NEWS/") : len(file)-len(".md")]
|
||||
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
|
||||
})
|
||||
return items, nil
|
||||
}
|
||||
|
||||
//go:embed "news.html"
|
||||
|
|
@ -122,14 +119,22 @@ type newsContext struct {
|
|||
|
||||
// HandleRoute returns the handler for the requested path
|
||||
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]{
|
||||
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)
|
||||
nc.Items = Items()
|
||||
nc.Items, err = items, itemsErr
|
||||
|
||||
return
|
||||
},
|
||||
Template: newsTemplate,
|
||||
Template: news.Dependencies.Custom.Template(newsTemplate),
|
||||
}, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
// Other invocations might cause an error at runtime.
|
||||
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
|
||||
|
||||
|
|
@ -33,13 +34,18 @@ const (
|
|||
errorSuffix template.HTML = "</div>"
|
||||
|
||||
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.
|
||||
//
|
||||
// The given request *must not* be nil.
|
||||
//
|
||||
// For convenience the passed context is also returned.
|
||||
func (tc *BaseContext) use(base component.Base, r *http.Request) *BaseContext {
|
||||
tc.inited = true
|
||||
tc.requestWasNil = r == nil
|
||||
|
||||
tc.GeneratedAt = time.Now().UTC()
|
||||
|
||||
|
|
@ -56,6 +62,9 @@ func (bc BaseContext) DoInitCheck() template.HTML {
|
|||
if !bc.inited {
|
||||
return initError
|
||||
}
|
||||
if bc.requestWasNil {
|
||||
return requestNilError
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta charset="utf-8">
|
||||
|
||||
<title>{{ block "block" . }}WissKI Distillery{{ end }}</title>
|
||||
<title>{{ block "title" . }}WissKI Distillery{{ end }}</title>
|
||||
{{ block "styles" . }}styles{{ end }}
|
||||
</head>
|
||||
|
||||
|
|
|
|||
|
|
@ -183,6 +183,5 @@ func (dis *Distillery) allComponents() []initFunc {
|
|||
auto[*cron.Cron],
|
||||
auto[*home.UpdateHome],
|
||||
auto[*home.UpdateInstanceList],
|
||||
auto[*home.UpdateRedirect],
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue