custom: Improve templating of assets
This commit is contained in:
parent
7d0fb60d67
commit
b6bf0a8900
19 changed files with 516 additions and 432 deletions
|
|
@ -80,21 +80,16 @@ func (admin *Admin) HandleRoute(ctx context.Context, route string) (handler http
|
|||
}
|
||||
|
||||
// add a handler for the index page
|
||||
router.Handler(http.MethodGet, route, httpx.HTMLHandler[indexContext]{
|
||||
Handler: admin.index,
|
||||
Template: admin.Dependencies.Custom.Template(indexTemplate),
|
||||
})
|
||||
|
||||
// fallback to the "/" page
|
||||
router.HandlerFunc(http.MethodGet, route+"index", func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, route, http.StatusTemporaryRedirect)
|
||||
})
|
||||
{
|
||||
index := admin.index(ctx)
|
||||
router.Handler(http.MethodGet, route, index)
|
||||
}
|
||||
|
||||
// add a handler for the user page
|
||||
router.Handler(http.MethodGet, route+"users", httpx.HTMLHandler[userContext]{
|
||||
Handler: admin.users,
|
||||
Template: admin.Dependencies.Custom.Template(userTemplate),
|
||||
})
|
||||
{
|
||||
users := admin.users(ctx)
|
||||
router.Handler(http.MethodGet, route+"users", users)
|
||||
}
|
||||
|
||||
// add a user create form
|
||||
{
|
||||
|
|
@ -113,32 +108,28 @@ func (admin *Admin) HandleRoute(ctx context.Context, route string) (handler http
|
|||
router.Handler(http.MethodPost, route+"users/unsetpassword", admin.usersUnsetPasswordHandler(ctx))
|
||||
|
||||
// add a handler for the component page
|
||||
router.Handler(http.MethodGet, route+"components", httpx.HTMLHandler[componentContext]{
|
||||
Handler: admin.components,
|
||||
Template: admin.Dependencies.Custom.Template(componentsTemplate),
|
||||
})
|
||||
{
|
||||
components := admin.components(ctx)
|
||||
router.Handler(http.MethodGet, route+"components", components)
|
||||
}
|
||||
|
||||
// add a handler for the component page
|
||||
router.Handler(http.MethodGet, route+"ingredients/:slug", httpx.HTMLHandler[ingredientsContext]{
|
||||
Handler: admin.ingredients,
|
||||
Template: admin.Dependencies.Custom.Template(ingredientsTemplate),
|
||||
})
|
||||
// add a handler for the ingredients page
|
||||
{
|
||||
ingredients := admin.ingredients(ctx)
|
||||
router.Handler(http.MethodGet, route+"ingredients/:slug", ingredients)
|
||||
}
|
||||
|
||||
// add a handler for the instance page
|
||||
router.Handler(http.MethodGet, route+"instance/:slug", httpx.HTMLHandler[instanceContext]{
|
||||
Handler: admin.instance,
|
||||
Template: admin.Dependencies.Custom.Template(instanceTemplate),
|
||||
})
|
||||
{
|
||||
instance := admin.instance(ctx)
|
||||
router.Handler(http.MethodGet, route+"instance/:slug", instance)
|
||||
}
|
||||
|
||||
// add a router for the grants pages
|
||||
router.Handler(http.MethodGet, route+"grants/:slug", httpx.HTMLHandler[grantsContext]{
|
||||
Handler: admin.getGrants,
|
||||
Template: admin.Dependencies.Custom.Template(grantsTemplate),
|
||||
})
|
||||
router.Handler(http.MethodPost, route+"grants/", httpx.HTMLHandler[grantsContext]{
|
||||
Handler: admin.postGrants,
|
||||
Template: admin.Dependencies.Custom.Template(grantsTemplate),
|
||||
})
|
||||
{
|
||||
grants := admin.grants(ctx)
|
||||
router.Handler(http.MethodGet, route+"grants/:slug", grants)
|
||||
router.Handler(http.MethodPost, route+"grants/", grants) // NOTE(twiesing): This path is intentionally different!
|
||||
}
|
||||
|
||||
// add a router for the login page
|
||||
router.Handler(http.MethodPost, route+"login", admin.loginHandler(ctx))
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package admin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"html/template"
|
||||
"net/http"
|
||||
|
||||
|
|
@ -17,11 +18,8 @@ import (
|
|||
)
|
||||
|
||||
//go:embed "html/components.html"
|
||||
var componentsTemplateString string
|
||||
var componentsTemplate = static.AssetsAdmin.MustParseShared(
|
||||
"components.html",
|
||||
componentsTemplateString,
|
||||
)
|
||||
var componentsHTML []byte
|
||||
var componentsTemplate = custom.Parse[componentContext]("components.html", componentsHTML, static.AssetsAdmin)
|
||||
|
||||
type componentContext struct {
|
||||
custom.BaseContext
|
||||
|
|
@ -29,24 +27,23 @@ type componentContext struct {
|
|||
Analytics lazy.PoolAnalytics
|
||||
}
|
||||
|
||||
func (admin *Admin) components(r *http.Request) (cp componentContext, err error) {
|
||||
admin.Dependencies.Custom.Update(&cp, r, custom.BaseContextGaps{
|
||||
func (admin *Admin) components(ctx context.Context) http.Handler {
|
||||
tpl := componentsTemplate.Prepare(admin.Dependencies.Custom, custom.BaseContextGaps{
|
||||
Crumbs: []component.MenuItem{
|
||||
{Title: "Admin", Path: "/admin/"},
|
||||
{Title: "Components", Path: "/admin/components/"},
|
||||
},
|
||||
})
|
||||
|
||||
cp.Analytics = *admin.Analytics
|
||||
return
|
||||
return tpl.HTMLHandler(func(r *http.Request) (cp componentContext, err error) {
|
||||
cp.Analytics = *admin.Analytics
|
||||
return
|
||||
})
|
||||
}
|
||||
|
||||
//go:embed "html/ingredients.html"
|
||||
var ingredientsTemplateString string
|
||||
var ingredientsTemplate = static.AssetsAdmin.MustParseShared(
|
||||
"ingredients.html",
|
||||
ingredientsTemplateString,
|
||||
)
|
||||
var ingredientsHTML []byte
|
||||
var ingredientsTemplate = custom.Parse[ingredientsContext]("ingredients.html", ingredientsHTML, static.AssetsAdmin)
|
||||
|
||||
type ingredientsContext struct {
|
||||
custom.BaseContext
|
||||
|
|
@ -55,29 +52,34 @@ type ingredientsContext struct {
|
|||
Analytics *lazy.PoolAnalytics
|
||||
}
|
||||
|
||||
func (admin *Admin) ingredients(r *http.Request) (cp ingredientsContext, err error) {
|
||||
slug := httprouter.ParamsFromContext(r.Context()).ByName("slug")
|
||||
|
||||
admin.Dependencies.Custom.Update(&cp, r, custom.BaseContextGaps{
|
||||
func (admin *Admin) ingredients(ctx context.Context) http.Handler {
|
||||
tpl := ingredientsTemplate.Prepare(admin.Dependencies.Custom, custom.BaseContextGaps{
|
||||
Crumbs: []component.MenuItem{
|
||||
{Title: "Admin", Path: "/admin/"},
|
||||
{Title: "Instance", Path: template.URL("/admin/instance/" + slug)},
|
||||
{Title: "Ingredients", Path: template.URL("/admin/instance/" + slug + "/ingredients/")},
|
||||
{Title: "Instance", Path: "* to be updated *"},
|
||||
{Title: "Ingredients", Path: "* to be updated *"},
|
||||
},
|
||||
})
|
||||
|
||||
// find the instance itself!
|
||||
instance, err := admin.Dependencies.Instances.WissKI(r.Context(), slug)
|
||||
if err == instances.ErrWissKINotFound {
|
||||
return cp, httpx.ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return cp, err
|
||||
}
|
||||
cp.Instance = instance.Instance
|
||||
return tpl.HTMLHandlerWithGaps(func(r *http.Request, gaps *custom.BaseContextGaps) (ic ingredientsContext, err error) {
|
||||
slug := httprouter.ParamsFromContext(r.Context()).ByName("slug")
|
||||
|
||||
// and get the components
|
||||
cp.Analytics = instance.Info().Analytics
|
||||
gaps.Crumbs[1] = component.MenuItem{Title: "Instance", Path: template.URL("/admin/instance/" + slug)}
|
||||
gaps.Crumbs[2] = component.MenuItem{Title: "Ingredients", Path: template.URL("/admin/instance/" + slug + "/ingredients/")}
|
||||
|
||||
return
|
||||
// find the instance itself!
|
||||
instance, err := admin.Dependencies.Instances.WissKI(r.Context(), slug)
|
||||
if err == instances.ErrWissKINotFound {
|
||||
return ic, httpx.ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return ic, err
|
||||
}
|
||||
ic.Instance = instance.Instance
|
||||
|
||||
// and get the components
|
||||
ic.Analytics = instance.Info().Analytics
|
||||
|
||||
return
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package admin
|
||||
|
||||
import (
|
||||
"context"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"html/template"
|
||||
|
|
@ -20,11 +21,8 @@ import (
|
|||
)
|
||||
|
||||
//go:embed "html/grants.html"
|
||||
var grantsStr string
|
||||
var grantsTemplate = static.AssetsAdmin.MustParseShared(
|
||||
"grants.html",
|
||||
grantsStr,
|
||||
)
|
||||
var grantsHTML []byte
|
||||
var grantsTemplate = custom.Parse[grantsContext]("grants.html", grantsHTML, static.AssetsAdmin)
|
||||
|
||||
type grantsContext struct {
|
||||
custom.BaseContext
|
||||
|
|
@ -39,15 +37,88 @@ type grantsContext struct {
|
|||
Drupals []string // unusued drupal usernames
|
||||
}
|
||||
|
||||
func (gc *grantsContext) use(r *http.Request, slug string, admin *Admin) (err error) {
|
||||
admin.Dependencies.Custom.Update(gc, r, custom.BaseContextGaps{
|
||||
func (admin *Admin) grants(ctx context.Context) http.Handler {
|
||||
tpl := grantsTemplate.Prepare(admin.Dependencies.Custom, custom.BaseContextGaps{
|
||||
Crumbs: []component.MenuItem{
|
||||
{Title: "Admin", Path: "/admin/"},
|
||||
{Title: "Instance", Path: template.URL("/admin/instance/" + slug)},
|
||||
{Title: "Grants", Path: template.URL("/admin/instance/" + slug + "/grants/")},
|
||||
{Title: "Instance", Path: "*to be updated*"},
|
||||
{Title: "Grants", Path: "*to be updated*"},
|
||||
},
|
||||
})
|
||||
|
||||
return tpl.HTMLHandlerWithGaps(func(r *http.Request, gaps *custom.BaseContextGaps) (grantsContext, error) {
|
||||
if r.Method == http.MethodGet {
|
||||
return admin.getGrants(r, gaps)
|
||||
} else {
|
||||
return admin.postGrants(r, gaps)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (admin *Admin) getGrants(r *http.Request, gaps *custom.BaseContextGaps) (gc grantsContext, err error) {
|
||||
slug := httprouter.ParamsFromContext(r.Context()).ByName("slug")
|
||||
if err := gc.use(r, gaps, slug, admin); err != nil {
|
||||
return gc, err
|
||||
}
|
||||
|
||||
if err := gc.useGrants(r, admin); err != nil {
|
||||
return gc, err
|
||||
}
|
||||
|
||||
return gc, nil
|
||||
}
|
||||
|
||||
func (admin *Admin) postGrants(r *http.Request, gaps *custom.BaseContextGaps) (gc grantsContext, err error) {
|
||||
// parse the form
|
||||
if err := r.ParseForm(); err != nil {
|
||||
return gc, err
|
||||
}
|
||||
|
||||
// read out the form values
|
||||
var (
|
||||
slug = r.PostFormValue("slug")
|
||||
delete = r.PostFormValue("action") == "delete"
|
||||
distilleryUser = r.PostFormValue("distillery-user")
|
||||
drupalUser = r.PostFormValue("drupal-user")
|
||||
adminRole = r.PostFormValue("admin") == field.CheckboxChecked
|
||||
)
|
||||
|
||||
// set the common fields
|
||||
if err := gc.use(r, gaps, slug, admin); err != nil {
|
||||
return gc, err
|
||||
}
|
||||
|
||||
if delete {
|
||||
// delete the user grant
|
||||
err := admin.Dependencies.Policy.Remove(r.Context(), distilleryUser, slug)
|
||||
if err != nil {
|
||||
return gc, err
|
||||
}
|
||||
} else {
|
||||
// update the grant
|
||||
err := admin.Dependencies.Policy.Set(r.Context(), models.Grant{
|
||||
User: distilleryUser,
|
||||
Slug: slug,
|
||||
|
||||
DrupalUsername: drupalUser,
|
||||
DrupalAdminRole: adminRole,
|
||||
})
|
||||
if err != nil {
|
||||
gc.Error = fmt.Sprintf("Unable to update grant for user %s: %s", distilleryUser, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// fetch the grants for the instance
|
||||
if err := gc.useGrants(r, admin); err != nil {
|
||||
return gc, err
|
||||
}
|
||||
return gc, nil
|
||||
}
|
||||
|
||||
func (gc *grantsContext) use(r *http.Request, gaps *custom.BaseContextGaps, slug string, admin *Admin) (err error) {
|
||||
gaps.Crumbs[1] = component.MenuItem{Title: "Instance", Path: template.URL("/admin/instance/" + slug)}
|
||||
gaps.Crumbs[2] = component.MenuItem{Title: "Grants", Path: template.URL("/admin/instance/" + slug + "/grants/")}
|
||||
|
||||
// find the instance itself
|
||||
gc.instance, err = admin.Dependencies.Instances.WissKI(r.Context(), slug)
|
||||
if err == instances.ErrWissKINotFound {
|
||||
|
|
@ -100,63 +171,3 @@ func (gc *grantsContext) useGrants(r *http.Request, admin *Admin) (err error) {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (admin *Admin) getGrants(r *http.Request) (gc grantsContext, err error) {
|
||||
slug := httprouter.ParamsFromContext(r.Context()).ByName("slug")
|
||||
if err := gc.use(r, slug, admin); err != nil {
|
||||
return gc, err
|
||||
}
|
||||
|
||||
if err := gc.useGrants(r, admin); err != nil {
|
||||
return gc, err
|
||||
}
|
||||
|
||||
return gc, nil
|
||||
}
|
||||
|
||||
func (admin *Admin) postGrants(r *http.Request) (gc grantsContext, err error) {
|
||||
// parse the form
|
||||
if err := r.ParseForm(); err != nil {
|
||||
return gc, err
|
||||
}
|
||||
|
||||
// read out the form values
|
||||
var (
|
||||
slug = r.PostFormValue("slug")
|
||||
delete = r.PostFormValue("action") == "delete"
|
||||
distilleryUser = r.PostFormValue("distillery-user")
|
||||
drupalUser = r.PostFormValue("drupal-user")
|
||||
adminRole = r.PostFormValue("admin") == field.CheckboxChecked
|
||||
)
|
||||
|
||||
// set the common fields
|
||||
if err := gc.use(r, slug, admin); err != nil {
|
||||
return gc, err
|
||||
}
|
||||
|
||||
if delete {
|
||||
// delete the user grant
|
||||
err := admin.Dependencies.Policy.Remove(r.Context(), distilleryUser, slug)
|
||||
if err != nil {
|
||||
return gc, err
|
||||
}
|
||||
} else {
|
||||
// update the grant
|
||||
err := admin.Dependencies.Policy.Set(r.Context(), models.Grant{
|
||||
User: distilleryUser,
|
||||
Slug: slug,
|
||||
|
||||
DrupalUsername: drupalUser,
|
||||
DrupalAdminRole: adminRole,
|
||||
})
|
||||
if err != nil {
|
||||
gc.Error = fmt.Sprintf("Unable to update grant for user %s: %s", distilleryUser, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// fetch the grants for the instance
|
||||
if err := gc.useGrants(r, admin); err != nil {
|
||||
return gc, err
|
||||
}
|
||||
return gc, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,13 +14,6 @@ import (
|
|||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
//go:embed "html/index.html"
|
||||
var indexTemplateStr string
|
||||
var indexTemplate = static.AssetsAdmin.MustParseShared(
|
||||
"index.html",
|
||||
indexTemplateStr,
|
||||
)
|
||||
|
||||
// Status produces a new observation of the distillery, and a new information of all instances
|
||||
// The information on all instances is passed the given quick flag.
|
||||
func (admin *Admin) Status(ctx context.Context, QuickInformation bool) (target status.Distillery, information []status.WissKI, err error) {
|
||||
|
|
@ -79,6 +72,16 @@ func (admin *Admin) Status(ctx context.Context, QuickInformation bool) (target s
|
|||
return
|
||||
}
|
||||
|
||||
func (admin *Admin) Fetch(flags component.FetcherFlags, target *status.Distillery) error {
|
||||
target.Time = time.Now().UTC()
|
||||
target.Config = admin.Config
|
||||
return nil
|
||||
}
|
||||
|
||||
//go:embed "html/index.html"
|
||||
var indexHTML []byte
|
||||
var indexTemplate = custom.Parse[indexContext]("index.html", indexHTML, static.AssetsAdmin)
|
||||
|
||||
type indexContext struct {
|
||||
custom.BaseContext
|
||||
|
||||
|
|
@ -86,8 +89,8 @@ type indexContext struct {
|
|||
Instances []status.WissKI
|
||||
}
|
||||
|
||||
func (admin *Admin) index(r *http.Request) (idx indexContext, err error) {
|
||||
admin.Dependencies.Custom.Update(&idx, r, custom.BaseContextGaps{
|
||||
func (admin *Admin) index(ctx context.Context) http.Handler {
|
||||
tpl := indexTemplate.Prepare(admin.Dependencies.Custom, custom.BaseContextGaps{
|
||||
Crumbs: []component.MenuItem{
|
||||
{Title: "Admin", Path: "/admin/"},
|
||||
},
|
||||
|
|
@ -96,12 +99,9 @@ func (admin *Admin) index(r *http.Request) (idx indexContext, err error) {
|
|||
{Title: "Components", Path: "/admin/components/", Priority: component.SmallButton},
|
||||
},
|
||||
})
|
||||
idx.Distillery, idx.Instances, err = admin.Status(r.Context(), true)
|
||||
return
|
||||
}
|
||||
|
||||
func (admin *Admin) Fetch(flags component.FetcherFlags, target *status.Distillery) error {
|
||||
target.Time = time.Now().UTC()
|
||||
target.Config = admin.Config
|
||||
return nil
|
||||
return tpl.HTMLHandlerWithGaps(func(r *http.Request, gaps *custom.BaseContextGaps) (idx indexContext, err error) {
|
||||
idx.Distillery, idx.Instances, err = admin.Status(r.Context(), true)
|
||||
return
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package admin
|
||||
|
||||
import (
|
||||
"context"
|
||||
_ "embed"
|
||||
"html/template"
|
||||
"net/http"
|
||||
|
|
@ -16,11 +17,8 @@ import (
|
|||
)
|
||||
|
||||
//go:embed "html/instance.html"
|
||||
var instanceTemplateString string
|
||||
var instanceTemplate = static.AssetsAdmin.MustParseShared(
|
||||
"instance.html",
|
||||
instanceTemplateString,
|
||||
)
|
||||
var instanceHTML []byte
|
||||
var instanceTemplate = custom.Parse[instanceContext]("instance.html", instanceHTML, static.AssetsAdmin)
|
||||
|
||||
type instanceContext struct {
|
||||
custom.BaseContext
|
||||
|
|
@ -29,35 +27,42 @@ type instanceContext struct {
|
|||
Info status.WissKI
|
||||
}
|
||||
|
||||
func (admin *Admin) instance(r *http.Request) (is instanceContext, err error) {
|
||||
slug := httprouter.ParamsFromContext(r.Context()).ByName("slug")
|
||||
|
||||
admin.Dependencies.Custom.Update(&is, r, custom.BaseContextGaps{
|
||||
func (admin *Admin) instance(ctx context.Context) http.Handler {
|
||||
tpl := instanceTemplate.Prepare(admin.Dependencies.Custom, custom.BaseContextGaps{
|
||||
Crumbs: []component.MenuItem{
|
||||
{Title: "Admin", Path: "/admin/"},
|
||||
{Title: "Instance", Path: template.URL("/admin/instance/" + slug)},
|
||||
{Title: "Instance", Path: "*to be replaced*"},
|
||||
},
|
||||
Actions: []component.MenuItem{
|
||||
{Title: "Grants", Path: template.URL("/admin/grants/" + slug)},
|
||||
{Title: "Ingredients", Path: template.URL("/admin/ingredients/" + slug), Priority: component.SmallButton},
|
||||
{Title: "Grants", Path: "*to be replaced*"},
|
||||
{Title: "Ingredients", Path: "*to be replaced*", Priority: component.SmallButton},
|
||||
},
|
||||
})
|
||||
|
||||
// find the instance itself!
|
||||
instance, err := admin.Dependencies.Instances.WissKI(r.Context(), slug)
|
||||
if err == instances.ErrWissKINotFound {
|
||||
return is, httpx.ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return is, err
|
||||
}
|
||||
is.Instance = instance.Instance
|
||||
return tpl.HTMLHandlerWithGaps(func(r *http.Request, gaps *custom.BaseContextGaps) (ic instanceContext, err error) {
|
||||
slug := httprouter.ParamsFromContext(r.Context()).ByName("slug")
|
||||
|
||||
// get some more info about the wisski
|
||||
is.Info, err = instance.Info().Information(r.Context(), false)
|
||||
if err != nil {
|
||||
return is, err
|
||||
}
|
||||
gaps.Crumbs[1] = component.MenuItem{Title: "Instance", Path: template.URL("/admin/instance/" + slug)}
|
||||
|
||||
return
|
||||
gaps.Actions[0] = component.MenuItem{Title: "Grants", Path: template.URL("/admin/grants/" + slug)}
|
||||
gaps.Actions[1] = component.MenuItem{Title: "Ingredients", Path: template.URL("/admin/ingredients/" + slug), Priority: component.SmallButton}
|
||||
|
||||
// find the instance itself!
|
||||
instance, err := admin.Dependencies.Instances.WissKI(r.Context(), slug)
|
||||
if err == instances.ErrWissKINotFound {
|
||||
return ic, httpx.ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return ic, err
|
||||
}
|
||||
ic.Instance = instance.Instance
|
||||
|
||||
// get some more info about the wisski
|
||||
ic.Info, err = instance.Info().Information(r.Context(), false)
|
||||
if err != nil {
|
||||
return ic, err
|
||||
}
|
||||
|
||||
return
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,21 +18,18 @@ import (
|
|||
)
|
||||
|
||||
//go:embed "html/users.html"
|
||||
var userTemplateString string
|
||||
var userTemplate = static.AssetsAdmin.MustParseShared(
|
||||
"users.html",
|
||||
userTemplateString,
|
||||
)
|
||||
var usersHTML []byte
|
||||
var usersTemplate = custom.Parse[usersContext]("user.html", usersHTML, static.AssetsAdmin)
|
||||
|
||||
type userContext struct {
|
||||
type usersContext struct {
|
||||
custom.BaseContext
|
||||
|
||||
Error string
|
||||
Users []*auth.AuthUser
|
||||
}
|
||||
|
||||
func (admin *Admin) users(r *http.Request) (uc userContext, err error) {
|
||||
admin.Dependencies.Custom.Update(&uc, r, custom.BaseContextGaps{
|
||||
func (admin *Admin) users(ctx context.Context) http.Handler {
|
||||
tpl := usersTemplate.Prepare(admin.Dependencies.Custom, custom.BaseContextGaps{
|
||||
Crumbs: []component.MenuItem{
|
||||
{Title: "Admin", Path: "/admin/"},
|
||||
{Title: "Users", Path: "/admin/users/"},
|
||||
|
|
@ -42,17 +39,16 @@ func (admin *Admin) users(r *http.Request) (uc userContext, err error) {
|
|||
},
|
||||
})
|
||||
|
||||
uc.Error = r.URL.Query().Get("error")
|
||||
uc.Users, err = admin.Dependencies.Auth.Users(r.Context())
|
||||
return
|
||||
return tpl.HTMLHandler(func(r *http.Request) (uc usersContext, err error) {
|
||||
uc.Error = r.URL.Query().Get("error")
|
||||
uc.Users, err = admin.Dependencies.Auth.Users(r.Context())
|
||||
return
|
||||
})
|
||||
}
|
||||
|
||||
//go:embed "html/user_create.html"
|
||||
var userCreateTemplateString string
|
||||
var userCreateTemplate = static.AssetsAdmin.MustParseShared(
|
||||
"user_create.html",
|
||||
userCreateTemplateString,
|
||||
)
|
||||
var userCreateHTML []byte
|
||||
var userCreateTemplate = custom.ParseForm("user_create.html", userCreateHTML, static.AssetsAdmin)
|
||||
|
||||
var (
|
||||
errCreateInvalidUsername = errors.New("invalid username")
|
||||
|
|
@ -66,14 +62,13 @@ type createUserResult struct {
|
|||
}
|
||||
|
||||
func (admin *Admin) createUser(ctx context.Context) http.Handler {
|
||||
userCreateTemplate := admin.Dependencies.Custom.Template(userCreateTemplate)
|
||||
gaps := custom.BaseContextGaps{
|
||||
tpl := userCreateTemplate.Prepare(admin.Dependencies.Custom, custom.BaseContextGaps{
|
||||
Crumbs: []component.MenuItem{
|
||||
{Title: "Admin", Path: "/admin/"},
|
||||
{Title: "Users", Path: "/admin/users"},
|
||||
{Title: "Create", Path: "/admin/users/create"},
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
return &httpx.Form[createUserResult]{
|
||||
Fields: []field.Field{
|
||||
|
|
@ -83,10 +78,8 @@ func (admin *Admin) createUser(ctx context.Context) http.Handler {
|
|||
},
|
||||
FieldTemplate: field.PureCSSFieldTemplate,
|
||||
|
||||
RenderTemplate: userCreateTemplate,
|
||||
RenderTemplateContext: func(ctx httpx.FormContext, r *http.Request) any {
|
||||
return admin.Dependencies.Custom.NewForm(ctx, r, gaps)
|
||||
},
|
||||
RenderTemplate: tpl.Template(),
|
||||
RenderTemplateContext: custom.FormTemplateContext(tpl),
|
||||
|
||||
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"] == field.CheckboxChecked
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue