custom: Add new footer template and context

This commit is contained in:
Tom Wiesing 2023-01-06 19:56:13 +01:00
parent 009d649ea6
commit bda763725e
No known key found for this signature in database
18 changed files with 197 additions and 33 deletions

View file

@ -6,6 +6,7 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static/custom"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/exporter"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/exporter/logger"
"github.com/julienschmidt/httprouter"
@ -26,6 +27,8 @@ type Admin struct {
SnapshotsLog *logger.Logger
Auth *auth.Auth
Custom *custom.Custom
}
Analytics *lazy.PoolAnalytics
@ -64,13 +67,13 @@ func (admin *Admin) HandleRoute(ctx context.Context, route string) (handler http
// add a handler for the index page
router.Handler(http.MethodGet, route+"index", httpx.HTMLHandler[indexContext]{
Handler: admin.index,
Template: indexTemplate,
Template: admin.Dependencies.Custom.Template(indexTemplate),
})
// add a handler for the user page
router.Handler(http.MethodGet, route+"users", httpx.HTMLHandler[userContext]{
Handler: admin.users,
Template: userTemplate,
Template: admin.Dependencies.Custom.Template(userTemplate),
})
// add a user create form
@ -90,19 +93,19 @@ func (admin *Admin) HandleRoute(ctx context.Context, route string) (handler http
// add a handler for the component page
router.Handler(http.MethodGet, route+"components", httpx.HTMLHandler[componentContext]{
Handler: admin.components,
Template: componentsTemplate,
Template: admin.Dependencies.Custom.Template(componentsTemplate),
})
// add a handler for the component page
router.Handler(http.MethodGet, route+"ingredients/:slug", httpx.HTMLHandler[ingredientsContext]{
Handler: admin.ingredients,
Template: ingredientsTemplate,
Template: admin.Dependencies.Custom.Template(ingredientsTemplate),
})
// add a handler for the instance page
router.Handler(http.MethodGet, route+"instance/:slug", httpx.HTMLHandler[instanceContext]{
Handler: admin.instance,
Template: instanceTemplate,
Template: admin.Dependencies.Custom.Template(instanceTemplate),
})
// add a router for the login page

View file

@ -2,11 +2,11 @@ package admin
import (
"net/http"
"time"
_ "embed"
"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/internal/models"
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
@ -22,15 +22,15 @@ var componentsTemplate = static.AssetsAdmin.MustParseShared(
)
type componentContext struct {
Time time.Time
custom.BaseContext
Analytics lazy.PoolAnalytics
}
func (admin *Admin) components(r *http.Request) (cp componentContext, err error) {
cp.Analytics = *admin.Analytics
cp.Time = time.Now().UTC()
admin.Dependencies.Custom.Update(&cp)
cp.Analytics = *admin.Analytics
return
}
@ -42,14 +42,14 @@ var ingredientsTemplate = static.AssetsAdmin.MustParseShared(
)
type ingredientsContext struct {
Time time.Time
custom.BaseContext
Instance models.Instance
Analytics *lazy.PoolAnalytics
}
func (admin *Admin) ingredients(r *http.Request) (cp ingredientsContext, err error) {
cp.Time = time.Now().UTC()
admin.Dependencies.Custom.Update(&cp)
// find the instance itself!
instance, err := admin.Dependencies.Instances.WissKI(r.Context(), mux.Vars(r)["slug"])

View file

@ -9,6 +9,7 @@ import (
"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/status"
"golang.org/x/sync/errgroup"
)
@ -79,11 +80,14 @@ func (admin *Admin) Status(ctx context.Context, QuickInformation bool) (target s
}
type indexContext struct {
custom.BaseContext
status.Distillery
Instances []status.WissKI
}
func (admin *Admin) index(r *http.Request) (idx indexContext, err error) {
admin.Dependencies.Custom.Update(&idx)
idx.Distillery, idx.Instances, err = admin.Status(r.Context(), true)
return
}

View file

@ -4,9 +4,9 @@ import (
_ "embed"
"html/template"
"net/http"
"time"
"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/internal/models"
"github.com/FAU-CDI/wisski-distillery/internal/status"
@ -23,7 +23,7 @@ var instanceTemplate = static.AssetsAdmin.MustParseShared(
)
type instanceContext struct {
Time time.Time
custom.BaseContext
CSRF template.HTML
Instance models.Instance
@ -31,6 +31,8 @@ type instanceContext struct {
}
func (admin *Admin) instance(r *http.Request) (is instanceContext, err error) {
admin.Dependencies.Custom.Update(&is)
is.CSRF = csrf.TemplateField(r)
// find the instance itself!
@ -49,8 +51,5 @@ func (admin *Admin) instance(r *http.Request) (is instanceContext, err error) {
return is, err
}
// current time
is.Time = time.Now().UTC()
return
}

View file

@ -5,12 +5,12 @@ import (
"errors"
"html/template"
"net/http"
"time"
_ "embed"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth"
"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/gorilla/csrf"
"github.com/rs/zerolog"
@ -24,15 +24,17 @@ var userTemplate = static.AssetsAdmin.MustParseShared(
)
type userContext struct {
Time time.Time
custom.BaseContext
httpx.FormContext
CSRF template.HTML
Users []*auth.AuthUser
}
func (admin *Admin) users(r *http.Request) (uc userContext, err error) {
admin.Dependencies.Custom.Update(&uc)
uc.CSRF = csrf.TemplateField(r)
uc.Time = time.Now()
uc.Users, err = admin.Dependencies.Auth.Users(r.Context())
return
}
@ -56,6 +58,8 @@ type createUserResult struct {
}
func (admin *Admin) createUser(ctx context.Context) http.Handler {
userCreateTemplate := admin.Dependencies.Custom.Template(userCreateTemplate)
return &httpx.Form[createUserResult]{
Fields: []httpx.Field{
{Name: "username", Type: httpx.TextField, Label: "Username"},
@ -64,7 +68,8 @@ func (admin *Admin) createUser(ctx context.Context) http.Handler {
},
FieldTemplate: httpx.PureCSSFieldTemplate,
RenderTemplate: userCreateTemplate,
RenderTemplate: userCreateTemplate,
RenderTemplateContext: admin.Dependencies.Custom.RenderContext,
Validate: func(r *http.Request, values map[string]string) (cu createUserResult, err error) {
cu.User, cu.Passsword, cu.Admin = values["username"], values["password"], values["admin"] == "on"