Refactor server and templates package
This commit is contained in:
parent
b6bf0a8900
commit
6ede99d7c6
105 changed files with 341 additions and 339 deletions
|
|
@ -5,7 +5,7 @@ import (
|
|||
"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/server/templates"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/sql"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/lazy"
|
||||
"github.com/gorilla/sessions"
|
||||
|
|
@ -17,7 +17,7 @@ type Auth struct {
|
|||
Dependencies struct {
|
||||
SQL *sql.SQL
|
||||
UserDeleteHooks []component.UserDeleteHook
|
||||
Custom *custom.Custom
|
||||
Templating *templates.Templating
|
||||
}
|
||||
|
||||
store lazy.Lazy[sessions.Store]
|
||||
|
|
|
|||
|
|
@ -8,8 +8,8 @@ import (
|
|||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth/next"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth/policy"
|
||||
"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/server/templates"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/ssh2/sshkeys"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/models"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
|
||||
|
|
@ -19,12 +19,12 @@ import (
|
|||
type UserPanel struct {
|
||||
component.Base
|
||||
Dependencies struct {
|
||||
Auth *auth.Auth
|
||||
Custom *custom.Custom
|
||||
Policy *policy.Policy
|
||||
Instances *instances.Instances
|
||||
Next *next.Next
|
||||
Keys *sshkeys.SSHKeys
|
||||
Auth *auth.Auth
|
||||
Templating *templates.Templating
|
||||
Policy *policy.Policy
|
||||
Instances *instances.Instances
|
||||
Next *next.Next
|
||||
Keys *sshkeys.SSHKeys
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -106,14 +106,14 @@ func (panel *UserPanel) HandleRoute(ctx context.Context, route string) (http.Han
|
|||
}
|
||||
|
||||
type userFormContext struct {
|
||||
custom.BaseContext
|
||||
templates.BaseContext
|
||||
httpx.FormContext
|
||||
|
||||
User *models.User
|
||||
}
|
||||
|
||||
func (panel *UserPanel) UserFormContext2(tpl *custom.Template[userFormContext], last component.MenuItem, gaps ...custom.BaseContextGaps) func(ctx httpx.FormContext, r *http.Request) any {
|
||||
var g custom.BaseContextGaps
|
||||
func (panel *UserPanel) UserFormContext2(tpl *templates.Template[userFormContext], last component.MenuItem, gaps ...templates.BaseContextGaps) func(ctx httpx.FormContext, r *http.Request) any {
|
||||
var g templates.BaseContextGaps
|
||||
if len(gaps) > 1 {
|
||||
panic("UserFormContext2: gaps must be of length 0 or 1")
|
||||
}
|
||||
|
|
@ -125,7 +125,7 @@ func (panel *UserPanel) UserFormContext2(tpl *custom.Template[userFormContext],
|
|||
last,
|
||||
}
|
||||
|
||||
return custom.MappedHandler(tpl, func(ctx httpx.FormContext, r *http.Request) (userFormContext, custom.BaseContextGaps) {
|
||||
return templates.MappedHandler(tpl, func(ctx httpx.FormContext, r *http.Request) (userFormContext, templates.BaseContextGaps) {
|
||||
uctx := userFormContext{FormContext: ctx}
|
||||
if user, err := panel.Dependencies.Auth.UserOf(r); err == nil {
|
||||
uctx.User = &user.User
|
||||
|
|
|
|||
|
|
@ -8,15 +8,15 @@ import (
|
|||
_ "embed"
|
||||
|
||||
"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/dis/component/server/assets"
|
||||
templating "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/httpx/field"
|
||||
)
|
||||
|
||||
//go:embed "templates/password.html"
|
||||
var passwordHTML []byte
|
||||
var passwordTemplate = custom.Parse[userFormContext]("password.html", passwordHTML, static.AssetsUser)
|
||||
var passwordTemplate = templating.Parse[userFormContext]("password.html", passwordHTML, assets.AssetsUser)
|
||||
|
||||
var (
|
||||
errPasswordsNotIdentical = errors.New("passwords are not identical")
|
||||
|
|
@ -28,7 +28,7 @@ var (
|
|||
)
|
||||
|
||||
func (panel *UserPanel) routePassword(ctx context.Context) http.Handler {
|
||||
tpl := passwordTemplate.Prepare(panel.Dependencies.Custom)
|
||||
tpl := passwordTemplate.Prepare(panel.Dependencies.Templating)
|
||||
|
||||
return &httpx.Form[struct{}]{
|
||||
Fields: []field.Field{
|
||||
|
|
|
|||
|
|
@ -7,8 +7,8 @@ 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"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static/custom"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/models"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/httpx/field"
|
||||
|
|
@ -22,10 +22,10 @@ import (
|
|||
|
||||
//go:embed "templates/ssh.html"
|
||||
var sshHTML []byte
|
||||
var sshTemplate = custom.Parse[SSHTemplateContext]("ssh.html", sshHTML, static.AssetsUser)
|
||||
var sshTemplate = templates.Parse[SSHTemplateContext]("ssh.html", sshHTML, assets.AssetsUser)
|
||||
|
||||
type SSHTemplateContext struct {
|
||||
custom.BaseContext
|
||||
templates.BaseContext
|
||||
|
||||
Keys []models.Keys
|
||||
|
||||
|
|
@ -37,7 +37,7 @@ type SSHTemplateContext struct {
|
|||
}
|
||||
|
||||
func (panel *UserPanel) sshRoute(ctx context.Context) http.Handler {
|
||||
tpl := sshTemplate.Prepare(panel.Dependencies.Custom, custom.BaseContextGaps{
|
||||
tpl := sshTemplate.Prepare(panel.Dependencies.Templating, templates.BaseContextGaps{
|
||||
Crumbs: []component.MenuItem{
|
||||
{Title: "User", Path: "/user/"},
|
||||
{Title: "SSH Keys", Path: "/user/ssh/"},
|
||||
|
|
@ -114,7 +114,7 @@ func (panel *UserPanel) sshDeleteRoute(ctx context.Context) http.Handler {
|
|||
|
||||
//go:embed "templates/ssh_add.html"
|
||||
var sshAddHTML []byte
|
||||
var sshAddTemplate = custom.ParseForm("ssh_add.html", sshAddHTML, static.AssetsUser)
|
||||
var sshAddTemplate = templates.ParseForm("ssh_add.html", sshAddHTML, assets.AssetsUser)
|
||||
|
||||
type addKeyResult struct {
|
||||
User *auth.AuthUser
|
||||
|
|
@ -123,7 +123,7 @@ type addKeyResult struct {
|
|||
}
|
||||
|
||||
func (panel *UserPanel) sshAddRoute(ctx context.Context) http.Handler {
|
||||
tpl := sshAddTemplate.Prepare(panel.Dependencies.Custom, custom.BaseContextGaps{
|
||||
tpl := sshAddTemplate.Prepare(panel.Dependencies.Templating, templates.BaseContextGaps{
|
||||
Crumbs: []component.MenuItem{
|
||||
{Title: "User", Path: "/user/"},
|
||||
{Title: "SSH Keys", Path: "/user/ssh/"},
|
||||
|
|
@ -139,7 +139,7 @@ func (panel *UserPanel) sshAddRoute(ctx context.Context) http.Handler {
|
|||
FieldTemplate: field.PureCSSFieldTemplate,
|
||||
|
||||
RenderTemplate: tpl.Template(),
|
||||
RenderTemplateContext: custom.FormTemplateContext(tpl),
|
||||
RenderTemplateContext: templates.FormTemplateContext(tpl),
|
||||
|
||||
Validate: func(r *http.Request, values map[string]string) (ak addKeyResult, err error) {
|
||||
ak.User, err = panel.Dependencies.Auth.UserOf(r)
|
||||
|
|
|
|||
|
|
@ -7,8 +7,8 @@ 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"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static/custom"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/httpx/field"
|
||||
|
||||
|
|
@ -17,10 +17,10 @@ import (
|
|||
|
||||
//go:embed "templates/totp_enable.html"
|
||||
var totpEnableHTML []byte
|
||||
var totpEnable = custom.Parse[userFormContext]("totp_enable.html", totpEnableHTML, static.AssetsUser)
|
||||
var totpEnable = templates.Parse[userFormContext]("totp_enable.html", totpEnableHTML, assets.AssetsUser)
|
||||
|
||||
func (panel *UserPanel) routeTOTPEnable(ctx context.Context) http.Handler {
|
||||
tpl := totpEnable.Prepare(panel.Dependencies.Custom)
|
||||
tpl := totpEnable.Prepare(panel.Dependencies.Templating)
|
||||
|
||||
return &httpx.Form[struct{}]{
|
||||
Fields: []field.Field{
|
||||
|
|
@ -69,7 +69,7 @@ func (panel *UserPanel) routeTOTPEnable(ctx context.Context) http.Handler {
|
|||
|
||||
//go:embed "templates/totp_enroll.html"
|
||||
var totpEnrollHTML []byte
|
||||
var totpEnrollTemplate = custom.Parse[totpEnrollContext]("totp_enroll.html", totpEnrollHTML, static.AssetsUser)
|
||||
var totpEnrollTemplate = templates.Parse[totpEnrollContext]("totp_enroll.html", totpEnrollHTML, assets.AssetsUser)
|
||||
|
||||
type totpEnrollContext struct {
|
||||
userFormContext
|
||||
|
|
@ -80,7 +80,7 @@ type totpEnrollContext struct {
|
|||
}
|
||||
|
||||
func (panel *UserPanel) routeTOTPEnroll(ctx context.Context) http.Handler {
|
||||
tpl := totpEnrollTemplate.Prepare(panel.Dependencies.Custom, custom.BaseContextGaps{
|
||||
tpl := totpEnrollTemplate.Prepare(panel.Dependencies.Templating, templates.BaseContextGaps{
|
||||
Crumbs: []component.MenuItem{
|
||||
{Title: "User", Path: "/user/"},
|
||||
{Title: "Enable TOTP", Path: "/user/totp/enable/"},
|
||||
|
|
@ -156,10 +156,10 @@ func (panel *UserPanel) routeTOTPEnroll(ctx context.Context) http.Handler {
|
|||
|
||||
//go:embed "templates/totp_disable.html"
|
||||
var totpDisableHTML []byte
|
||||
var totpDisableTemplate = custom.Parse[userFormContext]("totp_disable.html", totpDisableHTML, static.AssetsUser)
|
||||
var totpDisableTemplate = templates.Parse[userFormContext]("totp_disable.html", totpDisableHTML, assets.AssetsUser)
|
||||
|
||||
func (panel *UserPanel) routeTOTPDisable(ctx context.Context) http.Handler {
|
||||
tpl := totpDisableTemplate.Prepare(panel.Dependencies.Custom)
|
||||
tpl := totpDisableTemplate.Prepare(panel.Dependencies.Templating)
|
||||
|
||||
return &httpx.Form[struct{}]{
|
||||
Fields: []field.Field{
|
||||
|
|
|
|||
|
|
@ -9,17 +9,17 @@ 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"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static/custom"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/models"
|
||||
)
|
||||
|
||||
//go:embed "templates/user.html"
|
||||
var userHTML []byte
|
||||
var userTemplate = custom.Parse[userContext]("user.html", userHTML, static.AssetsUser)
|
||||
var userTemplate = templates.Parse[userContext]("user.html", userHTML, assets.AssetsUser)
|
||||
|
||||
type userContext struct {
|
||||
custom.BaseContext
|
||||
templates.BaseContext
|
||||
*auth.AuthUser
|
||||
|
||||
Grants []GrantWithURL
|
||||
|
|
@ -31,7 +31,7 @@ type GrantWithURL struct {
|
|||
}
|
||||
|
||||
func (panel *UserPanel) routeUser(ctx context.Context) http.Handler {
|
||||
tpl := userTemplate.Prepare(panel.Dependencies.Custom, custom.BaseContextGaps{
|
||||
tpl := userTemplate.Prepare(panel.Dependencies.Templating, templates.BaseContextGaps{
|
||||
Crumbs: []component.MenuItem{
|
||||
{Title: "User", Path: "/user/"},
|
||||
},
|
||||
|
|
@ -42,7 +42,7 @@ func (panel *UserPanel) routeUser(ctx context.Context) http.Handler {
|
|||
},
|
||||
})
|
||||
|
||||
return tpl.HTMLHandlerWithGaps(func(r *http.Request, gaps *custom.BaseContextGaps) (uc userContext, err error) {
|
||||
return tpl.HTMLHandlerWithGaps(func(r *http.Request, gaps *templates.BaseContextGaps) (uc userContext, err error) {
|
||||
// find the user
|
||||
uc.AuthUser, err = panel.Dependencies.Auth.UserOf(r)
|
||||
if err != nil || uc.AuthUser == nil {
|
||||
|
|
|
|||
|
|
@ -7,9 +7,9 @@ import (
|
|||
"net/http"
|
||||
|
||||
"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/static"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static/custom"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/httpx/field"
|
||||
"github.com/gorilla/sessions"
|
||||
|
|
@ -35,7 +35,7 @@ func (auth *Auth) UserOf(r *http.Request) (user *AuthUser, err error) {
|
|||
}
|
||||
|
||||
// try to read the name from the session
|
||||
name, ok := sess.Values[control.SessionUserKey]
|
||||
name, ok := sess.Values[server.SessionUserKey]
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
|
|
@ -67,7 +67,7 @@ func (auth *Auth) UserOf(r *http.Request) (user *AuthUser, err error) {
|
|||
func (auth *Auth) session(r *http.Request) (*sessions.Session, error) {
|
||||
return auth.store.Get(func() sessions.Store {
|
||||
return sessions.NewCookieStore([]byte(auth.Config.SessionSecret))
|
||||
}).Get(r, control.SessionCookie)
|
||||
}).Get(r, server.SessionCookie)
|
||||
}
|
||||
|
||||
func (auth *Auth) Menu(r *http.Request) []component.MenuItem {
|
||||
|
|
@ -101,7 +101,7 @@ func (auth *Auth) Login(w http.ResponseWriter, r *http.Request, user *AuthUser)
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sess.Values[control.SessionUserKey] = user.User.User
|
||||
sess.Values[server.SessionUserKey] = user.User.User
|
||||
return sess.Save(r, w)
|
||||
}
|
||||
|
||||
|
|
@ -120,7 +120,7 @@ func (auth *Auth) Logout(w http.ResponseWriter, r *http.Request) error {
|
|||
|
||||
//go:embed "login.html"
|
||||
var loginHTML []byte
|
||||
var loginTemplate = custom.ParseForm("login.html", loginHTML, static.AssetsUser)
|
||||
var loginTemplate = templates.ParseForm("login.html", loginHTML, assets.AssetsUser)
|
||||
|
||||
var loginResponse = httpx.Response{
|
||||
ContentType: "text/plain",
|
||||
|
|
@ -131,7 +131,7 @@ var errLoginFailed = errors.New("Login failed")
|
|||
|
||||
// authLogin implements a view to login a user
|
||||
func (auth *Auth) authLogin(ctx context.Context) http.Handler {
|
||||
tpl := loginTemplate.Prepare(auth.Dependencies.Custom)
|
||||
tpl := loginTemplate.Prepare(auth.Dependencies.Templating)
|
||||
|
||||
return &httpx.Form[*AuthUser]{
|
||||
Fields: []field.Field{
|
||||
|
|
@ -145,7 +145,7 @@ func (auth *Auth) authLogin(ctx context.Context) http.Handler {
|
|||
if context.Err != nil {
|
||||
context.Err = errLoginFailed
|
||||
}
|
||||
tpl.Execute(w, r, custom.BaseFormContext{FormContext: context}, custom.BaseContextGaps{
|
||||
tpl.Execute(w, r, templates.BaseFormContext{FormContext: context}, templates.BaseContextGaps{
|
||||
Crumbs: []component.MenuItem{
|
||||
{Title: "Login", Path: template.URL(r.URL.RequestURI())},
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue