Add new debug option for http

This commit is contained in:
Tom Wiesing 2023-11-22 17:28:46 +01:00
parent 0ba34fe80f
commit 0290a42d07
No known key found for this signature in database
39 changed files with 293 additions and 189 deletions

View file

@ -9,6 +9,7 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth/policy"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth/scopes"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/admin/socket"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/handling"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templating"
"github.com/julienschmidt/httprouter"
"github.com/rs/zerolog"
@ -20,6 +21,7 @@ import (
type Admin struct {
component.Base
dependencies struct {
Handling *handling.Handling
Fetchers []component.DistilleryFetcher
Instances *instances.Instances
@ -178,7 +180,7 @@ func (admin *Admin) HandleRoute(ctx context.Context, route string) (handler http
func (admin *Admin) loginHandler(ctx context.Context) http.Handler {
logger := zerolog.Ctx(ctx)
return httpx.RedirectHandler(func(r *http.Request) (string, int, error) {
return admin.dependencies.Handling.Redirect(func(r *http.Request) (string, int, error) {
// parse the form
if err := r.ParseForm(); err != nil {
logger.Err(err).Msg("failed to parse admin login")

View file

@ -120,7 +120,7 @@ func (admin *Admin) index(ctx context.Context) http.Handler {
),
)
return tpl.HTMLHandler(func(r *http.Request) (idx indexContext, err error) {
return tpl.HTMLHandler(admin.dependencies.Handling, func(r *http.Request) (idx indexContext, err error) {
idx.Distillery, idx.Instances, err = admin.Status(r.Context(), false)
return
})
@ -138,7 +138,7 @@ func (admin *Admin) instances(ctx context.Context) http.Handler {
),
)
return tpl.HTMLHandler(func(r *http.Request) (idx indexContext, err error) {
return tpl.HTMLHandler(admin.dependencies.Handling, func(r *http.Request) (idx indexContext, err error) {
idx.Distillery, idx.Instances, err = admin.Status(r.Context(), true)
return
})

View file

@ -42,7 +42,7 @@ func (admin *Admin) instance(ctx context.Context) http.Handler {
),
)
return tpl.HTMLHandlerWithFlags(func(r *http.Request) (ic instanceContext, funcs []templating.FlagFunc, err error) {
return tpl.HTMLHandlerWithFlags(admin.dependencies.Handling, func(r *http.Request) (ic instanceContext, funcs []templating.FlagFunc, err error) {
slug := httprouter.ParamsFromContext(r.Context()).ByName("slug")
// find the instance itself!

View file

@ -43,7 +43,7 @@ func (admin *Admin) instanceData(ctx context.Context) http.Handler {
),
)
return tpl.HTMLHandlerWithFlags(func(r *http.Request) (ctx instanceDataContext, funcs []templating.FlagFunc, err error) {
return tpl.HTMLHandlerWithFlags(admin.dependencies.Handling, func(r *http.Request) (ctx instanceDataContext, funcs []templating.FlagFunc, err error) {
slug := httprouter.ParamsFromContext(r.Context()).ByName("slug")
// setup the context with just the instance

View file

@ -50,7 +50,7 @@ func (admin *Admin) instanceDrupal(ctx context.Context) http.Handler {
),
)
return tpl.HTMLHandlerWithFlags(func(r *http.Request) (ctx instanceDrupalContext, funcs []templating.FlagFunc, err error) {
return tpl.HTMLHandlerWithFlags(admin.dependencies.Handling, func(r *http.Request) (ctx instanceDrupalContext, funcs []templating.FlagFunc, err error) {
slug := httprouter.ParamsFromContext(r.Context()).ByName("slug")
// setup the context with just the instance

View file

@ -26,7 +26,7 @@ func (admin *Admin) instanceProvision(ctx context.Context) http.Handler {
),
)
return tpl.HTMLHandler(func(r *http.Request) (ipc instanceSystemContext, err error) {
return tpl.HTMLHandler(admin.dependencies.Handling, func(r *http.Request) (ipc instanceSystemContext, err error) {
ipc.prepare(false)
ipc.DefaultProfile = manager.DefaultProfile()
ipc.Profiles = collection.MapValues(manager.Profiles(), func(_ string, profile manager.Profile) string { return profile.Description })

View file

@ -40,7 +40,7 @@ func (admin *Admin) instancePurge(ctx context.Context) http.Handler {
),
)
return tpl.HTMLHandlerWithFlags(func(r *http.Request) (ctx instancePurgeContext, funcs []templating.FlagFunc, err error) {
return tpl.HTMLHandlerWithFlags(admin.dependencies.Handling, func(r *http.Request) (ctx instancePurgeContext, funcs []templating.FlagFunc, err error) {
slug := httprouter.ParamsFromContext(r.Context()).ByName("slug")
// setup the context with just the instance

View file

@ -65,7 +65,7 @@ func (admin *Admin) instanceRebuild(ctx context.Context) http.Handler {
),
)
return tpl.HTMLHandlerWithFlags(func(r *http.Request) (isc instanceSystemContext, funcs []templating.FlagFunc, err error) {
return tpl.HTMLHandlerWithFlags(admin.dependencies.Handling, func(r *http.Request) (isc instanceSystemContext, funcs []templating.FlagFunc, err error) {
slug := httprouter.ParamsFromContext(r.Context()).ByName("slug")
var instance *wisski.WissKI

View file

@ -42,7 +42,7 @@ func (admin *Admin) instanceSnapshots(ctx context.Context) http.Handler {
),
)
return tpl.HTMLHandlerWithFlags(func(r *http.Request) (ctx instanceSnapshotsContext, funcs []templating.FlagFunc, err error) {
return tpl.HTMLHandlerWithFlags(admin.dependencies.Handling, func(r *http.Request) (ctx instanceSnapshotsContext, funcs []templating.FlagFunc, err error) {
slug := httprouter.ParamsFromContext(r.Context()).ByName("slug")
// setup the context with just the instance

View file

@ -47,7 +47,7 @@ func (admin *Admin) instanceSSH(ctx context.Context) http.Handler {
),
)
return tpl.HTMLHandlerWithFlags(func(r *http.Request) (ctx instanceSSHContext, funcs []templating.FlagFunc, err error) {
return tpl.HTMLHandlerWithFlags(admin.dependencies.Handling, func(r *http.Request) (ctx instanceSSHContext, funcs []templating.FlagFunc, err error) {
slug := httprouter.ParamsFromContext(r.Context()).ByName("slug")
// setup the context with just the instance

View file

@ -42,7 +42,7 @@ func (admin *Admin) instanceStats(ctx context.Context) http.Handler {
),
)
return tpl.HTMLHandlerWithFlags(func(r *http.Request) (ctx instanceStatsContext, funcs []templating.FlagFunc, err error) {
return tpl.HTMLHandlerWithFlags(admin.dependencies.Handling, func(r *http.Request) (ctx instanceStatsContext, funcs []templating.FlagFunc, err error) {
slug := httprouter.ParamsFromContext(r.Context()).ByName("slug")
// setup the context with just the instance

View file

@ -15,7 +15,7 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/status"
"github.com/FAU-CDI/wisski-distillery/internal/wisski"
"github.com/tkw1536/pkglib/httpx"
"github.com/tkw1536/pkglib/httpx/field"
"github.com/tkw1536/pkglib/httpx/form/field"
"github.com/julienschmidt/httprouter"
"golang.org/x/exp/maps"
@ -55,7 +55,7 @@ func (admin *Admin) instanceUsers(ctx context.Context) http.Handler {
),
)
return tpl.HTMLHandlerWithFlags(func(r *http.Request) (instanceUsersContext, []templating.FlagFunc, error) {
return tpl.HTMLHandlerWithFlags(admin.dependencies.Handling, func(r *http.Request) (instanceUsersContext, []templating.FlagFunc, error) {
if r.Method == http.MethodGet {
return admin.getGrantsUsers(r)
} else {

View file

@ -10,8 +10,7 @@ import (
"time"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth"
"github.com/gorilla/websocket"
"github.com/tkw1536/pkglib/httpx"
"github.com/tkw1536/pkglib/httpx/websocket"
)
// ActionMap handles a set of WebSocket actions
@ -40,7 +39,7 @@ func (err errPanic) Error() string {
// Finally it will send a ResultMessage once handling is complete.
//
// A corresponding client implementation of this can be found in ..../remote/proto.ts
func (am ActionMap) Handle(auth *auth.Auth, conn httpx.WebSocketConnection) (name string, err error) {
func (am ActionMap) Handle(auth *auth.Auth, conn *websocket.Connection) (name string, err error) {
var wg sync.WaitGroup
// once we have finished executing send a binary message (indicating success) to the client.
@ -67,7 +66,7 @@ func (am ActionMap) Handle(auth *auth.Auth, conn httpx.WebSocketConnection) (nam
}
// encode the result message to json!
var message httpx.WebSocketMessage
var message websocket.Message
message.Type = websocket.BinaryMessage
message.Bytes, err = json.Marshal(result)

View file

@ -14,7 +14,7 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/admin/socket/actions"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/admin/socket/proto"
"github.com/rs/zerolog"
"github.com/tkw1536/pkglib/httpx"
"github.com/tkw1536/pkglib/httpx/websocket"
"github.com/tkw1536/pkglib/lazy"
)
@ -48,14 +48,14 @@ func (socket *Sockets) Routes() component.Routes {
}
func (sockets *Sockets) HandleRoute(ctx context.Context, path string) (http.Handler, error) {
return &httpx.WebSocket{
return &websocket.Server{
Context: ctx,
Handler: sockets.Serve,
}, nil
}
// Serve handles a connection to the websocket api
func (socket *Sockets) Serve(conn httpx.WebSocketConnection) {
func (socket *Sockets) Serve(conn *websocket.Connection) {
// handle the websocket connection!
name, err := socket.actions.Get(func() proto.ActionMap { return socket.Actions(conn.Context()) }).Handle(socket.dependencies.Auth, conn)
if err != nil {

View file

@ -11,7 +11,8 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templating"
"github.com/rs/zerolog"
"github.com/tkw1536/pkglib/httpx"
"github.com/tkw1536/pkglib/httpx/field"
"github.com/tkw1536/pkglib/httpx/form"
"github.com/tkw1536/pkglib/httpx/form/field"
_ "embed"
)
@ -43,7 +44,7 @@ func (admin *Admin) users(ctx context.Context) http.Handler {
),
)
return tpl.HTMLHandler(func(r *http.Request) (uc usersContext, err error) {
return tpl.HTMLHandler(admin.dependencies.Handling, 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
@ -53,7 +54,7 @@ func (admin *Admin) users(ctx context.Context) http.Handler {
//go:embed "html/user_create.html"
var userCreateHTML []byte
var userCreateTemplate = templating.ParseForm(
"user_create.html", userCreateHTML, httpx.FormTemplate,
"user_create.html", userCreateHTML, form.FormTemplate,
templating.Title("Create User"),
templating.Assets(assets.AssetsAdmin),
@ -80,16 +81,16 @@ func (admin *Admin) createUser(ctx context.Context) http.Handler {
),
)
return &httpx.Form[createUserResult]{
return &form.Form[createUserResult]{
Fields: []field.Field{
{Name: "username", Type: field.Text, Autocomplete: field.Username, Label: "Username"},
{Name: "password", Type: field.Password, Autocomplete: field.NewPassword, Label: "Password"},
{Name: "admin", Type: field.Checkbox, Label: "Distillery Administrator"},
},
FieldTemplate: field.PureCSSFieldTemplate,
FieldTemplate: assets.PureCSSFieldTemplate,
RenderTemplate: tpl.Template(),
RenderTemplateContext: templating.FormTemplateContext(tpl),
Template: tpl.Template(),
TemplateContext: templating.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
@ -110,7 +111,7 @@ func (admin *Admin) createUser(ctx context.Context) http.Handler {
return cu, nil
},
RenderSuccess: func(cu createUserResult, values map[string]string, w http.ResponseWriter, r *http.Request) error {
Success: func(cu createUserResult, values map[string]string, w http.ResponseWriter, r *http.Request) error {
// create the user
user, err := admin.dependencies.Auth.CreateUser(r.Context(), cu.User)
if err != nil {