zdravko/internal/handlers/settingsworkergroups.go

158 lines
4 KiB
Go
Raw Normal View History

2024-02-24 21:07:49 +00:00
package handlers
import (
"context"
"fmt"
"net/http"
2024-03-04 13:20:01 +00:00
"strings"
2024-02-24 21:07:49 +00:00
2024-02-27 11:04:05 +00:00
"code.tjo.space/mentos1386/zdravko/database/models"
2024-02-24 21:07:49 +00:00
"code.tjo.space/mentos1386/zdravko/internal/jwt"
"code.tjo.space/mentos1386/zdravko/internal/services"
"code.tjo.space/mentos1386/zdravko/web/templates/components"
"github.com/go-playground/validator/v10"
"github.com/gosimple/slug"
"github.com/labstack/echo/v4"
)
type WorkerWithTokenAndActiveWorkers struct {
2024-02-24 21:07:49 +00:00
*models.WorkerGroup
Token string
ActiveWorkers []string
}
type WorkerGroupWithActiveWorkers struct {
*models.WorkerGroupWithChecks
ActiveWorkers []string
2024-02-24 21:07:49 +00:00
}
type SettingsWorkerGroups struct {
*Settings
WorkerGroups []*WorkerGroupWithActiveWorkers
2024-02-24 21:07:49 +00:00
}
type SettingsWorker struct {
*Settings
Worker *WorkerWithTokenAndActiveWorkers
2024-02-24 21:07:49 +00:00
}
func (h *BaseHandler) SettingsWorkerGroupsGET(c echo.Context) error {
cc := c.(AuthenticatedContext)
workerGroups, err := services.GetWorkerGroupsWithChecks(context.Background(), h.db)
2024-02-24 21:07:49 +00:00
if err != nil {
return err
}
workerGroupsWithActiveWorkers := make([]*WorkerGroupWithActiveWorkers, len(workerGroups))
for i, workerGroup := range workerGroups {
2024-02-29 22:42:56 +00:00
activeWorkers, err := services.GetActiveWorkers(context.Background(), workerGroup.Id, h.temporal)
if err != nil {
return err
}
workerGroupsWithActiveWorkers[i] = &WorkerGroupWithActiveWorkers{
WorkerGroupWithChecks: workerGroup,
ActiveWorkers: activeWorkers,
}
}
2024-02-24 21:07:49 +00:00
return c.Render(http.StatusOK, "settings_worker_groups.tmpl", &SettingsWorkerGroups{
Settings: NewSettings(
cc.Principal.User,
GetPageByTitle(SettingsPages, "Worker Groups"),
[]*components.Page{GetPageByTitle(SettingsPages, "Worker Groups")},
),
WorkerGroups: workerGroupsWithActiveWorkers,
2024-02-24 21:07:49 +00:00
})
}
func (h *BaseHandler) SettingsWorkerGroupsDescribeGET(c echo.Context) error {
cc := c.(AuthenticatedContext)
2024-02-29 22:42:56 +00:00
id := c.Param("id")
2024-02-24 21:07:49 +00:00
2024-02-29 22:42:56 +00:00
worker, err := services.GetWorkerGroup(context.Background(), h.db, id)
2024-02-24 21:07:49 +00:00
if err != nil {
return err
}
// Allow write access to default namespace
token, err := jwt.NewTokenForWorker(h.config.Jwt.PrivateKey, h.config.Jwt.PublicKey, worker)
if err != nil {
return err
}
2024-02-29 22:42:56 +00:00
activeWorkers, err := services.GetActiveWorkers(context.Background(), worker.Id, h.temporal)
if err != nil {
return err
}
2024-02-24 21:07:49 +00:00
return c.Render(http.StatusOK, "settings_worker_groups_describe.tmpl", &SettingsWorker{
Settings: NewSettings(
cc.Principal.User,
GetPageByTitle(SettingsPages, "Worker Groups"),
[]*components.Page{
GetPageByTitle(SettingsPages, "Worker Groups"),
{
2024-02-29 22:42:56 +00:00
Path: fmt.Sprintf("/settings/worker-groups/%s", id),
2024-02-24 21:07:49 +00:00
Title: "Describe",
Breadcrumb: worker.Name,
},
}),
Worker: &WorkerWithTokenAndActiveWorkers{
WorkerGroup: worker,
Token: token,
ActiveWorkers: activeWorkers,
2024-02-24 21:07:49 +00:00
},
})
}
func (h *BaseHandler) SettingsWorkerGroupsDescribeDELETE(c echo.Context) error {
2024-02-29 22:42:56 +00:00
id := c.Param("id")
2024-02-29 22:42:56 +00:00
err := services.DeleteWorkerGroup(context.Background(), h.db, id)
if err != nil {
return err
}
return c.Redirect(http.StatusSeeOther, "/settings/worker-groups")
}
2024-02-24 21:07:49 +00:00
func (h *BaseHandler) SettingsWorkerGroupsCreateGET(c echo.Context) error {
cc := c.(AuthenticatedContext)
return c.Render(http.StatusOK, "settings_worker_groups_create.tmpl", NewSettings(
cc.Principal.User,
GetPageByTitle(SettingsPages, "Worker Groups"),
[]*components.Page{
GetPageByTitle(SettingsPages, "Worker Groups"),
GetPageByTitle(SettingsPages, "Worker Groups Create"),
},
))
}
func (h *BaseHandler) SettingsWorkerGroupsCreatePOST(c echo.Context) error {
ctx := context.Background()
2024-02-29 22:42:56 +00:00
id := slug.Make(c.FormValue("name"))
2024-02-24 21:07:49 +00:00
2024-02-27 11:04:05 +00:00
workerGroup := &models.WorkerGroup{
2024-03-04 13:20:01 +00:00
Name: strings.ToLower(c.FormValue("name")),
2024-02-29 22:42:56 +00:00
Id: id,
2024-02-24 21:07:49 +00:00
}
2024-02-27 11:04:05 +00:00
err := validator.New(validator.WithRequiredStructEnabled()).Struct(workerGroup)
2024-02-24 21:07:49 +00:00
if err != nil {
return err
}
2024-02-27 11:04:05 +00:00
err = services.CreateWorkerGroup(
2024-02-24 21:07:49 +00:00
ctx,
2024-02-27 11:04:05 +00:00
h.db,
workerGroup,
2024-02-24 21:07:49 +00:00
)
if err != nil {
return err
}
2024-02-29 22:42:56 +00:00
return c.Redirect(http.StatusSeeOther, fmt.Sprintf("/settings/worker-groups/%s", id))
2024-02-24 21:07:49 +00:00
}