custom: Add new footer template and context
This commit is contained in:
parent
009d649ea6
commit
bda763725e
18 changed files with 197 additions and 33 deletions
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"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/sql"
|
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/sql"
|
||||||
"github.com/FAU-CDI/wisski-distillery/pkg/lazy"
|
"github.com/FAU-CDI/wisski-distillery/pkg/lazy"
|
||||||
"github.com/gorilla/sessions"
|
"github.com/gorilla/sessions"
|
||||||
|
|
@ -16,6 +17,7 @@ type Auth struct {
|
||||||
Dependencies struct {
|
Dependencies struct {
|
||||||
SQL *sql.SQL
|
SQL *sql.SQL
|
||||||
UserDeleteHooks []component.UserDeleteHook
|
UserDeleteHooks []component.UserDeleteHook
|
||||||
|
Custom *custom.Custom
|
||||||
}
|
}
|
||||||
|
|
||||||
store lazy.Lazy[sessions.Store]
|
store lazy.Lazy[sessions.Store]
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import (
|
||||||
|
|
||||||
"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/auth"
|
"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/models"
|
"github.com/FAU-CDI/wisski-distillery/internal/models"
|
||||||
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
|
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
|
|
@ -15,6 +16,7 @@ type UserPanel struct {
|
||||||
component.Base
|
component.Base
|
||||||
Dependencies struct {
|
Dependencies struct {
|
||||||
Auth *auth.Auth
|
Auth *auth.Auth
|
||||||
|
Custom *custom.Custom
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -67,7 +69,9 @@ func (panel *UserPanel) HandleRoute(ctx context.Context, route string) (http.Han
|
||||||
}
|
}
|
||||||
|
|
||||||
type userFormContext struct {
|
type userFormContext struct {
|
||||||
|
custom.BaseContext
|
||||||
httpx.FormContext
|
httpx.FormContext
|
||||||
|
|
||||||
User *models.User
|
User *models.User
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -75,6 +79,7 @@ func (panel *UserPanel) UserFormContext(ctx httpx.FormContext, r *http.Request)
|
||||||
user, err := panel.Dependencies.Auth.UserOf(r)
|
user, err := panel.Dependencies.Auth.UserOf(r)
|
||||||
|
|
||||||
uctx := userFormContext{FormContext: ctx}
|
uctx := userFormContext{FormContext: ctx}
|
||||||
|
panel.Dependencies.Custom.Update(&uctx)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
uctx.User = &user.User
|
uctx.User = &user.User
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,8 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (panel *UserPanel) routePassword(ctx context.Context) http.Handler {
|
func (panel *UserPanel) routePassword(ctx context.Context) http.Handler {
|
||||||
|
passwordTemplate := panel.Dependencies.Custom.Template(passwordTemplate)
|
||||||
|
|
||||||
return &httpx.Form[struct{}]{
|
return &httpx.Form[struct{}]{
|
||||||
Fields: []httpx.Field{
|
Fields: []httpx.Field{
|
||||||
{Name: "old", Type: httpx.PasswordField, EmptyOnError: true, Label: "Current Password"},
|
{Name: "old", Type: httpx.PasswordField, EmptyOnError: true, Label: "Current Password"},
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,8 @@ var totpEnableStr string
|
||||||
var totpEnableTemplate = static.AssetsUser.MustParseShared("totp_enable.html", totpEnableStr)
|
var totpEnableTemplate = static.AssetsUser.MustParseShared("totp_enable.html", totpEnableStr)
|
||||||
|
|
||||||
func (panel *UserPanel) routeTOTPEnable(ctx context.Context) http.Handler {
|
func (panel *UserPanel) routeTOTPEnable(ctx context.Context) http.Handler {
|
||||||
|
totpEnableTemplate := panel.Dependencies.Custom.Template(totpEnableTemplate)
|
||||||
|
|
||||||
return &httpx.Form[struct{}]{
|
return &httpx.Form[struct{}]{
|
||||||
Fields: []httpx.Field{
|
Fields: []httpx.Field{
|
||||||
{Name: "password", Type: httpx.PasswordField, EmptyOnError: true, Label: "Current Password"},
|
{Name: "password", Type: httpx.PasswordField, EmptyOnError: true, Label: "Current Password"},
|
||||||
|
|
@ -73,6 +75,8 @@ type totpEnrollContext struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (panel *UserPanel) routeTOTPEnroll(ctx context.Context) http.Handler {
|
func (panel *UserPanel) routeTOTPEnroll(ctx context.Context) http.Handler {
|
||||||
|
totpEnrollTemplate := panel.Dependencies.Custom.Template(totpEnrollTemplate)
|
||||||
|
|
||||||
return &httpx.Form[struct{}]{
|
return &httpx.Form[struct{}]{
|
||||||
Fields: []httpx.Field{
|
Fields: []httpx.Field{
|
||||||
{Name: "password", Type: httpx.PasswordField, EmptyOnError: true, Label: "Current Password"},
|
{Name: "password", Type: httpx.PasswordField, EmptyOnError: true, Label: "Current Password"},
|
||||||
|
|
@ -85,6 +89,8 @@ func (panel *UserPanel) routeTOTPEnroll(ctx context.Context) http.Handler {
|
||||||
return struct{}{}, err == nil && user != nil && user.IsTOTPEnabled()
|
return struct{}{}, err == nil && user != nil && user.IsTOTPEnabled()
|
||||||
},
|
},
|
||||||
RenderForm: func(context httpx.FormContext, w http.ResponseWriter, r *http.Request) {
|
RenderForm: func(context httpx.FormContext, w http.ResponseWriter, r *http.Request) {
|
||||||
|
// TODO: Do we want to reuse the same function here?
|
||||||
|
|
||||||
user, err := panel.Dependencies.Auth.UserOf(r)
|
user, err := panel.Dependencies.Auth.UserOf(r)
|
||||||
|
|
||||||
ctx := totpEnrollContext{
|
ctx := totpEnrollContext{
|
||||||
|
|
@ -92,6 +98,7 @@ func (panel *UserPanel) routeTOTPEnroll(ctx context.Context) http.Handler {
|
||||||
FormContext: context,
|
FormContext: context,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
panel.Dependencies.Custom.Update(&ctx.userFormContext)
|
||||||
|
|
||||||
if err == nil && user != nil {
|
if err == nil && user != nil {
|
||||||
ctx.userFormContext.User = &user.User
|
ctx.userFormContext.User = &user.User
|
||||||
|
|
@ -142,6 +149,8 @@ var totpDisableStr string
|
||||||
var totpDisableTemplate = static.AssetsUser.MustParseShared("totp_disable.html", totpDisableStr)
|
var totpDisableTemplate = static.AssetsUser.MustParseShared("totp_disable.html", totpDisableStr)
|
||||||
|
|
||||||
func (panel *UserPanel) routeTOTPDisable(ctx context.Context) http.Handler {
|
func (panel *UserPanel) routeTOTPDisable(ctx context.Context) http.Handler {
|
||||||
|
totpDisableTemplate := panel.Dependencies.Custom.Template(totpDisableTemplate)
|
||||||
|
|
||||||
return &httpx.Form[struct{}]{
|
return &httpx.Form[struct{}]{
|
||||||
Fields: []httpx.Field{
|
Fields: []httpx.Field{
|
||||||
{Name: "password", Type: httpx.PasswordField, EmptyOnError: true, Label: "Current Password"},
|
{Name: "password", Type: httpx.PasswordField, EmptyOnError: true, Label: "Current Password"},
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import (
|
||||||
|
|
||||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth"
|
"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"
|
||||||
|
"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"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -18,9 +19,19 @@ var userTemplate = static.AssetsUser.MustParseShared(
|
||||||
userHTMLStr,
|
userHTMLStr,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type routeUserContext struct {
|
||||||
|
custom.BaseContext
|
||||||
|
*auth.AuthUser
|
||||||
|
}
|
||||||
|
|
||||||
func (panel *UserPanel) routeUser(ctx context.Context) http.Handler {
|
func (panel *UserPanel) routeUser(ctx context.Context) http.Handler {
|
||||||
return &httpx.HTMLHandler[*auth.AuthUser]{
|
userTemplate := panel.Dependencies.Custom.Template(userTemplate)
|
||||||
Handler: panel.Dependencies.Auth.UserOf,
|
return &httpx.HTMLHandler[routeUserContext]{
|
||||||
|
Handler: func(r *http.Request) (ruc routeUserContext, err error) {
|
||||||
|
panel.Dependencies.Custom.Update(&ruc)
|
||||||
|
ruc.AuthUser, err = panel.Dependencies.Auth.UserOf(r)
|
||||||
|
return routeUserContext{}, err
|
||||||
|
},
|
||||||
Template: userTemplate,
|
Template: userTemplate,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -111,6 +111,8 @@ var errLoginFailed = errors.New("Login failed")
|
||||||
|
|
||||||
// authLogin implements a view to login a user
|
// authLogin implements a view to login a user
|
||||||
func (auth *Auth) authLogin(ctx context.Context) http.Handler {
|
func (auth *Auth) authLogin(ctx context.Context) http.Handler {
|
||||||
|
loginTemplate := auth.Dependencies.Custom.Template(loginTemplate)
|
||||||
|
|
||||||
return &httpx.Form[*AuthUser]{
|
return &httpx.Form[*AuthUser]{
|
||||||
Fields: []httpx.Field{
|
Fields: []httpx.Field{
|
||||||
{Name: "username", Type: httpx.TextField, Label: "Username"},
|
{Name: "username", Type: httpx.TextField, Label: "Username"},
|
||||||
|
|
@ -123,7 +125,7 @@ func (auth *Auth) authLogin(ctx context.Context) http.Handler {
|
||||||
if context.Err != nil {
|
if context.Err != nil {
|
||||||
context.Err = errLoginFailed
|
context.Err = errLoginFailed
|
||||||
}
|
}
|
||||||
httpx.WriteHTML(context, nil, loginTemplate, "", w, r)
|
httpx.WriteHTML(auth.Dependencies.Custom.NewForm(context), nil, loginTemplate, "", w, r)
|
||||||
},
|
},
|
||||||
|
|
||||||
Validate: func(r *http.Request, values map[string]string) (*AuthUser, error) {
|
Validate: func(r *http.Request, values map[string]string) (*AuthUser, error) {
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import (
|
||||||
|
|
||||||
"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/auth"
|
"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"
|
||||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/exporter/logger"
|
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/exporter/logger"
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
|
|
@ -26,6 +27,8 @@ type Admin struct {
|
||||||
SnapshotsLog *logger.Logger
|
SnapshotsLog *logger.Logger
|
||||||
|
|
||||||
Auth *auth.Auth
|
Auth *auth.Auth
|
||||||
|
|
||||||
|
Custom *custom.Custom
|
||||||
}
|
}
|
||||||
|
|
||||||
Analytics *lazy.PoolAnalytics
|
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
|
// add a handler for the index page
|
||||||
router.Handler(http.MethodGet, route+"index", httpx.HTMLHandler[indexContext]{
|
router.Handler(http.MethodGet, route+"index", httpx.HTMLHandler[indexContext]{
|
||||||
Handler: admin.index,
|
Handler: admin.index,
|
||||||
Template: indexTemplate,
|
Template: admin.Dependencies.Custom.Template(indexTemplate),
|
||||||
})
|
})
|
||||||
|
|
||||||
// add a handler for the user page
|
// add a handler for the user page
|
||||||
router.Handler(http.MethodGet, route+"users", httpx.HTMLHandler[userContext]{
|
router.Handler(http.MethodGet, route+"users", httpx.HTMLHandler[userContext]{
|
||||||
Handler: admin.users,
|
Handler: admin.users,
|
||||||
Template: userTemplate,
|
Template: admin.Dependencies.Custom.Template(userTemplate),
|
||||||
})
|
})
|
||||||
|
|
||||||
// add a user create form
|
// 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
|
// add a handler for the component page
|
||||||
router.Handler(http.MethodGet, route+"components", httpx.HTMLHandler[componentContext]{
|
router.Handler(http.MethodGet, route+"components", httpx.HTMLHandler[componentContext]{
|
||||||
Handler: admin.components,
|
Handler: admin.components,
|
||||||
Template: componentsTemplate,
|
Template: admin.Dependencies.Custom.Template(componentsTemplate),
|
||||||
})
|
})
|
||||||
|
|
||||||
// add a handler for the component page
|
// add a handler for the component page
|
||||||
router.Handler(http.MethodGet, route+"ingredients/:slug", httpx.HTMLHandler[ingredientsContext]{
|
router.Handler(http.MethodGet, route+"ingredients/:slug", httpx.HTMLHandler[ingredientsContext]{
|
||||||
Handler: admin.ingredients,
|
Handler: admin.ingredients,
|
||||||
Template: ingredientsTemplate,
|
Template: admin.Dependencies.Custom.Template(ingredientsTemplate),
|
||||||
})
|
})
|
||||||
|
|
||||||
// add a handler for the instance page
|
// add a handler for the instance page
|
||||||
router.Handler(http.MethodGet, route+"instance/:slug", httpx.HTMLHandler[instanceContext]{
|
router.Handler(http.MethodGet, route+"instance/:slug", httpx.HTMLHandler[instanceContext]{
|
||||||
Handler: admin.instance,
|
Handler: admin.instance,
|
||||||
Template: instanceTemplate,
|
Template: admin.Dependencies.Custom.Template(instanceTemplate),
|
||||||
})
|
})
|
||||||
|
|
||||||
// add a router for the login page
|
// add a router for the login page
|
||||||
|
|
|
||||||
|
|
@ -2,11 +2,11 @@ package admin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
|
|
||||||
_ "embed"
|
_ "embed"
|
||||||
|
|
||||||
"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/instances"
|
"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/models"
|
||||||
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
|
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
|
||||||
|
|
@ -22,15 +22,15 @@ var componentsTemplate = static.AssetsAdmin.MustParseShared(
|
||||||
)
|
)
|
||||||
|
|
||||||
type componentContext struct {
|
type componentContext struct {
|
||||||
Time time.Time
|
custom.BaseContext
|
||||||
|
|
||||||
Analytics lazy.PoolAnalytics
|
Analytics lazy.PoolAnalytics
|
||||||
}
|
}
|
||||||
|
|
||||||
func (admin *Admin) components(r *http.Request) (cp componentContext, err error) {
|
func (admin *Admin) components(r *http.Request) (cp componentContext, err error) {
|
||||||
cp.Analytics = *admin.Analytics
|
admin.Dependencies.Custom.Update(&cp)
|
||||||
cp.Time = time.Now().UTC()
|
|
||||||
|
|
||||||
|
cp.Analytics = *admin.Analytics
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -42,14 +42,14 @@ var ingredientsTemplate = static.AssetsAdmin.MustParseShared(
|
||||||
)
|
)
|
||||||
|
|
||||||
type ingredientsContext struct {
|
type ingredientsContext struct {
|
||||||
Time time.Time
|
custom.BaseContext
|
||||||
|
|
||||||
Instance models.Instance
|
Instance models.Instance
|
||||||
Analytics *lazy.PoolAnalytics
|
Analytics *lazy.PoolAnalytics
|
||||||
}
|
}
|
||||||
|
|
||||||
func (admin *Admin) ingredients(r *http.Request) (cp ingredientsContext, err error) {
|
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!
|
// find the instance itself!
|
||||||
instance, err := admin.Dependencies.Instances.WissKI(r.Context(), mux.Vars(r)["slug"])
|
instance, err := admin.Dependencies.Instances.WissKI(r.Context(), mux.Vars(r)["slug"])
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ import (
|
||||||
|
|
||||||
"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"
|
"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"
|
"github.com/FAU-CDI/wisski-distillery/internal/status"
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
)
|
)
|
||||||
|
|
@ -79,11 +80,14 @@ func (admin *Admin) Status(ctx context.Context, QuickInformation bool) (target s
|
||||||
}
|
}
|
||||||
|
|
||||||
type indexContext struct {
|
type indexContext struct {
|
||||||
|
custom.BaseContext
|
||||||
|
|
||||||
status.Distillery
|
status.Distillery
|
||||||
Instances []status.WissKI
|
Instances []status.WissKI
|
||||||
}
|
}
|
||||||
|
|
||||||
func (admin *Admin) index(r *http.Request) (idx indexContext, err error) {
|
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)
|
idx.Distillery, idx.Instances, err = admin.Status(r.Context(), true)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,9 @@ import (
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"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"
|
||||||
|
"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/models"
|
"github.com/FAU-CDI/wisski-distillery/internal/models"
|
||||||
"github.com/FAU-CDI/wisski-distillery/internal/status"
|
"github.com/FAU-CDI/wisski-distillery/internal/status"
|
||||||
|
|
@ -23,7 +23,7 @@ var instanceTemplate = static.AssetsAdmin.MustParseShared(
|
||||||
)
|
)
|
||||||
|
|
||||||
type instanceContext struct {
|
type instanceContext struct {
|
||||||
Time time.Time
|
custom.BaseContext
|
||||||
|
|
||||||
CSRF template.HTML
|
CSRF template.HTML
|
||||||
Instance models.Instance
|
Instance models.Instance
|
||||||
|
|
@ -31,6 +31,8 @@ type instanceContext struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (admin *Admin) instance(r *http.Request) (is instanceContext, err error) {
|
func (admin *Admin) instance(r *http.Request) (is instanceContext, err error) {
|
||||||
|
admin.Dependencies.Custom.Update(&is)
|
||||||
|
|
||||||
is.CSRF = csrf.TemplateField(r)
|
is.CSRF = csrf.TemplateField(r)
|
||||||
|
|
||||||
// find the instance itself!
|
// find the instance itself!
|
||||||
|
|
@ -49,8 +51,5 @@ func (admin *Admin) instance(r *http.Request) (is instanceContext, err error) {
|
||||||
return is, err
|
return is, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// current time
|
|
||||||
is.Time = time.Now().UTC()
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,12 +5,12 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
|
|
||||||
_ "embed"
|
_ "embed"
|
||||||
|
|
||||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth"
|
"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"
|
||||||
|
"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/gorilla/csrf"
|
"github.com/gorilla/csrf"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
|
|
@ -24,15 +24,17 @@ var userTemplate = static.AssetsAdmin.MustParseShared(
|
||||||
)
|
)
|
||||||
|
|
||||||
type userContext struct {
|
type userContext struct {
|
||||||
Time time.Time
|
custom.BaseContext
|
||||||
|
httpx.FormContext
|
||||||
|
|
||||||
CSRF template.HTML
|
CSRF template.HTML
|
||||||
Users []*auth.AuthUser
|
Users []*auth.AuthUser
|
||||||
}
|
}
|
||||||
|
|
||||||
func (admin *Admin) users(r *http.Request) (uc userContext, err error) {
|
func (admin *Admin) users(r *http.Request) (uc userContext, err error) {
|
||||||
|
admin.Dependencies.Custom.Update(&uc)
|
||||||
|
|
||||||
uc.CSRF = csrf.TemplateField(r)
|
uc.CSRF = csrf.TemplateField(r)
|
||||||
uc.Time = time.Now()
|
|
||||||
uc.Users, err = admin.Dependencies.Auth.Users(r.Context())
|
uc.Users, err = admin.Dependencies.Auth.Users(r.Context())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -56,6 +58,8 @@ type createUserResult struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (admin *Admin) createUser(ctx context.Context) http.Handler {
|
func (admin *Admin) createUser(ctx context.Context) http.Handler {
|
||||||
|
userCreateTemplate := admin.Dependencies.Custom.Template(userCreateTemplate)
|
||||||
|
|
||||||
return &httpx.Form[createUserResult]{
|
return &httpx.Form[createUserResult]{
|
||||||
Fields: []httpx.Field{
|
Fields: []httpx.Field{
|
||||||
{Name: "username", Type: httpx.TextField, Label: "Username"},
|
{Name: "username", Type: httpx.TextField, Label: "Username"},
|
||||||
|
|
@ -65,6 +69,7 @@ func (admin *Admin) createUser(ctx context.Context) http.Handler {
|
||||||
FieldTemplate: httpx.PureCSSFieldTemplate,
|
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) {
|
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"
|
cu.User, cu.Passsword, cu.Admin = values["username"], values["password"], values["admin"] == "on"
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,11 @@ 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/instances"
|
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances"
|
||||||
"github.com/FAU-CDI/wisski-distillery/pkg/lazy"
|
"github.com/FAU-CDI/wisski-distillery/pkg/lazy"
|
||||||
)
|
)
|
||||||
|
|
@ -14,11 +16,13 @@ type Home struct {
|
||||||
component.Base
|
component.Base
|
||||||
Dependencies struct {
|
Dependencies struct {
|
||||||
Instances *instances.Instances
|
Instances *instances.Instances
|
||||||
|
Custom *custom.Custom
|
||||||
}
|
}
|
||||||
|
|
||||||
redirect lazy.Lazy[*Redirect]
|
redirect lazy.Lazy[*Redirect]
|
||||||
instanceNames lazy.Lazy[map[string]struct{}]
|
instanceNames lazy.Lazy[map[string]struct{}]
|
||||||
homeBytes lazy.Lazy[[]byte]
|
homeBytes lazy.Lazy[[]byte]
|
||||||
|
homeTemplate lazy.Lazy[*template.Template]
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
|
||||||
|
|
@ -3,11 +3,13 @@ package home
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"html/template"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
_ "embed"
|
_ "embed"
|
||||||
|
|
||||||
"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/status"
|
"github.com/FAU-CDI/wisski-distillery/internal/status"
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
)
|
)
|
||||||
|
|
@ -30,7 +32,8 @@ var homeHTMLStr string
|
||||||
var homeTemplate = static.AssetsHome.MustParseShared("home.html", homeHTMLStr)
|
var homeTemplate = static.AssetsHome.MustParseShared("home.html", homeHTMLStr)
|
||||||
|
|
||||||
func (home *Home) homeRender(ctx context.Context) ([]byte, error) {
|
func (home *Home) homeRender(ctx context.Context) ([]byte, error) {
|
||||||
var context HomeContext
|
var context homeContext
|
||||||
|
home.Dependencies.Custom.Update(&context)
|
||||||
|
|
||||||
// setup a couple of static things
|
// setup a couple of static things
|
||||||
context.Time = time.Now().UTC()
|
context.Time = time.Now().UTC()
|
||||||
|
|
@ -57,11 +60,15 @@ func (home *Home) homeRender(ctx context.Context) ([]byte, error) {
|
||||||
|
|
||||||
// render the template
|
// render the template
|
||||||
var buffer bytes.Buffer
|
var buffer bytes.Buffer
|
||||||
homeTemplate.Execute(&buffer, context)
|
home.homeTemplate.Get(func() *template.Template {
|
||||||
|
return home.Dependencies.Custom.Template(homeTemplate)
|
||||||
|
}).Execute(&buffer, context)
|
||||||
return buffer.Bytes(), nil
|
return buffer.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type HomeContext struct {
|
type homeContext struct {
|
||||||
|
custom.BaseContext
|
||||||
|
|
||||||
Instances []status.WissKI
|
Instances []status.WissKI
|
||||||
|
|
||||||
Time time.Time
|
Time time.Time
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"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"
|
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/control"
|
||||||
"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/pkg/httpx"
|
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
|
||||||
|
|
||||||
_ "embed"
|
_ "embed"
|
||||||
|
|
@ -15,6 +16,10 @@ import (
|
||||||
|
|
||||||
type Legal struct {
|
type Legal struct {
|
||||||
component.Base
|
component.Base
|
||||||
|
Dependencies struct {
|
||||||
|
Static *static.Static
|
||||||
|
Custom *custom.Custom
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -33,6 +38,8 @@ func (legal *Legal) Routes() component.Routes {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (legal *Legal) HandleRoute(ctx context.Context, route string) (http.Handler, error) {
|
func (legal *Legal) HandleRoute(ctx context.Context, route string) (http.Handler, error) {
|
||||||
|
legalTemplate := legal.Dependencies.Custom.Template(legalTemplate)
|
||||||
|
|
||||||
return httpx.HTMLHandler[legalContext]{
|
return httpx.HTMLHandler[legalContext]{
|
||||||
Handler: legal.context,
|
Handler: legal.context,
|
||||||
Template: legalTemplate,
|
Template: legalTemplate,
|
||||||
|
|
@ -40,6 +47,8 @@ func (legal *Legal) HandleRoute(ctx context.Context, route string) (http.Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
type legalContext struct {
|
type legalContext struct {
|
||||||
|
custom.BaseContext
|
||||||
|
|
||||||
LegalNotices string
|
LegalNotices string
|
||||||
|
|
||||||
CSRFCookie string
|
CSRFCookie string
|
||||||
|
|
@ -49,6 +58,7 @@ type legalContext struct {
|
||||||
|
|
||||||
func (legal *Legal) context(r *http.Request) (legalContext, error) {
|
func (legal *Legal) context(r *http.Request) (legalContext, error) {
|
||||||
return legalContext{
|
return legalContext{
|
||||||
|
BaseContext: legal.Dependencies.Custom.New(),
|
||||||
LegalNotices: cli.LegalNotices,
|
LegalNotices: cli.LegalNotices,
|
||||||
|
|
||||||
CSRFCookie: control.CSRFCookie,
|
CSRFCookie: control.CSRFCookie,
|
||||||
|
|
|
||||||
8
internal/dis/component/control/static/custom/custom.go
Normal file
8
internal/dis/component/control/static/custom/custom.go
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
package custom
|
||||||
|
|
||||||
|
import "github.com/FAU-CDI/wisski-distillery/internal/dis/component"
|
||||||
|
|
||||||
|
// Custom implements theme and page customization.
|
||||||
|
type Custom struct {
|
||||||
|
component.Base
|
||||||
|
}
|
||||||
82
internal/dis/component/control/static/custom/template.go
Normal file
82
internal/dis/component/control/static/custom/template.go
Normal file
|
|
@ -0,0 +1,82 @@
|
||||||
|
package custom
|
||||||
|
|
||||||
|
import (
|
||||||
|
"html/template"
|
||||||
|
"net/http"
|
||||||
|
"reflect"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
|
||||||
|
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
|
||||||
|
"github.com/tkw1536/goprogram/lib/reflectx"
|
||||||
|
)
|
||||||
|
|
||||||
|
const footerName = "footer"
|
||||||
|
|
||||||
|
// defaultTemplate is the default footer template
|
||||||
|
var defaultTemplate = template.Must(template.New("footer.html").Parse(`<p>Powered By WissKI Distillery</p>`))
|
||||||
|
|
||||||
|
// Template creates a copy of template with shared template parts updated accordingly.
|
||||||
|
// Any template using this should use one of the template contexts in this package.
|
||||||
|
func (custom *Custom) Template(tpl *template.Template) *template.Template {
|
||||||
|
tree := defaultTemplate.Tree.Copy()
|
||||||
|
|
||||||
|
clone := template.Must(tpl.Clone()) // create a clone of the template
|
||||||
|
template.Must(clone.AddParseTree(footerName, tree)) // add the parse tree to it
|
||||||
|
return clone // and return the tree
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewContext returns a new BaseContext
|
||||||
|
func (custom *Custom) New() (ctx BaseContext) {
|
||||||
|
ctx.Use(custom.Base)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewForm is like New, but returns a new BaseFormContext
|
||||||
|
func (custom *Custom) NewForm(context httpx.FormContext) (ctx BaseFormContext) {
|
||||||
|
ctx.FormContext = context
|
||||||
|
ctx.Use(custom.Base)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// RenderContext can be used as httpx.Form.RenderTemplateContext.
|
||||||
|
// It returns a new [BaseFormContext].
|
||||||
|
func (custom *Custom) RenderContext(ctx httpx.FormContext, r *http.Request) any {
|
||||||
|
return BaseFormContext{
|
||||||
|
FormContext: ctx,
|
||||||
|
BaseContext: custom.New(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update updates an embedded BaseContext field in context.
|
||||||
|
//
|
||||||
|
// Assumes that context is a pointer to a struct type.
|
||||||
|
// If this is not the case, might call panic().
|
||||||
|
func (custom *Custom) Update(context any) *BaseContext {
|
||||||
|
ctx := reflect.ValueOf(context).
|
||||||
|
Elem().FieldByName(contextName).Addr().
|
||||||
|
Interface().(*BaseContext)
|
||||||
|
ctx.Use(custom.Base)
|
||||||
|
return ctx
|
||||||
|
}
|
||||||
|
|
||||||
|
// contextName is the name of the [BaseContext] field.
|
||||||
|
var contextName = reflectx.TypeOf[BaseContext]().Name()
|
||||||
|
|
||||||
|
// BaseContext is a context struct shared by all contexts
|
||||||
|
type BaseContext struct {
|
||||||
|
Time time.Time // time this page was generated at
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use updates this context to use the values from the given base.
|
||||||
|
// For convenience the passed context is also returned.
|
||||||
|
func (tc *BaseContext) Use(base component.Base) *BaseContext {
|
||||||
|
tc.Time = time.Now().UTC()
|
||||||
|
return tc
|
||||||
|
}
|
||||||
|
|
||||||
|
// BaseFormContext combines BaseContext and FormContext
|
||||||
|
type BaseFormContext struct {
|
||||||
|
BaseContext
|
||||||
|
httpx.FormContext
|
||||||
|
}
|
||||||
|
|
@ -24,11 +24,15 @@
|
||||||
</div>
|
</div>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
{{ if .Time }}
|
|
||||||
<footer>
|
<footer>
|
||||||
|
{{ if .Time }}
|
||||||
Generated at <code>{{ .Time }}</code>
|
Generated at <code>{{ .Time }}</code>
|
||||||
</footer>
|
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
{{ block "footer" . }}
|
||||||
|
<!-- no footer by default -->
|
||||||
|
{{ end }}
|
||||||
|
</footer>
|
||||||
|
|
||||||
|
|
||||||
{{ block "scripts" . }}scripts{{ end }}
|
{{ block "scripts" . }}scripts{{ end }}
|
||||||
</body>
|
</body>
|
||||||
|
|
@ -16,6 +16,7 @@ import (
|
||||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/home"
|
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/home"
|
||||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/legal"
|
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/legal"
|
||||||
"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/exporter"
|
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/exporter"
|
||||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/exporter/logger"
|
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/exporter/logger"
|
||||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances"
|
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances"
|
||||||
|
|
@ -108,6 +109,9 @@ func (dis *Distillery) Info() *admin.Admin {
|
||||||
func (dis *Distillery) Policy() *policy.Policy {
|
func (dis *Distillery) Policy() *policy.Policy {
|
||||||
return export[*policy.Policy](dis)
|
return export[*policy.Policy](dis)
|
||||||
}
|
}
|
||||||
|
func (dis *Distillery) Custom() *custom.Custom {
|
||||||
|
return export[*custom.Custom](dis)
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// All components
|
// All components
|
||||||
|
|
@ -154,7 +158,7 @@ func (dis *Distillery) allComponents() []initFunc {
|
||||||
|
|
||||||
// Control server
|
// Control server
|
||||||
auto[*control.Control],
|
auto[*control.Control],
|
||||||
auto[*static.Static],
|
|
||||||
auto[*home.Home],
|
auto[*home.Home],
|
||||||
manual(func(resolver *resolver.Resolver) {
|
manual(func(resolver *resolver.Resolver) {
|
||||||
resolver.RefreshInterval = time.Minute
|
resolver.RefreshInterval = time.Minute
|
||||||
|
|
@ -164,6 +168,9 @@ func (dis *Distillery) allComponents() []initFunc {
|
||||||
}),
|
}),
|
||||||
auto[*legal.Legal],
|
auto[*legal.Legal],
|
||||||
|
|
||||||
|
auto[*static.Static],
|
||||||
|
auto[*custom.Custom],
|
||||||
|
|
||||||
// Cron
|
// Cron
|
||||||
auto[*cron.Cron],
|
auto[*cron.Cron],
|
||||||
auto[*home.UpdateHome],
|
auto[*home.UpdateHome],
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue