2024-02-24 21:07:49 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2024-02-27 22:47:20 +00:00
|
|
|
type WorkerWithTokenAndActiveWorkers struct {
|
2024-02-24 21:07:49 +00:00
|
|
|
*models.WorkerGroup
|
2024-02-27 22:47:20 +00:00
|
|
|
Token string
|
|
|
|
ActiveWorkers []string
|
|
|
|
}
|
|
|
|
|
|
|
|
type WorkerGroupWithActiveWorkers struct {
|
|
|
|
*models.WorkerGroupWithMonitors
|
|
|
|
ActiveWorkers []string
|
2024-02-24 21:07:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SettingsWorkerGroups struct {
|
|
|
|
*Settings
|
2024-02-27 22:47:20 +00:00
|
|
|
WorkerGroups []*WorkerGroupWithActiveWorkers
|
2024-02-24 21:07:49 +00:00
|
|
|
WorkerGroupsLength int
|
|
|
|
}
|
|
|
|
|
|
|
|
type SettingsWorker struct {
|
|
|
|
*Settings
|
2024-02-27 22:47:20 +00:00
|
|
|
Worker *WorkerWithTokenAndActiveWorkers
|
2024-02-24 21:07:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *BaseHandler) SettingsWorkerGroupsGET(c echo.Context) error {
|
|
|
|
cc := c.(AuthenticatedContext)
|
|
|
|
|
2024-02-27 11:04:05 +00:00
|
|
|
workerGroups, err := services.GetWorkerGroupsWithMonitors(context.Background(), h.db)
|
2024-02-24 21:07:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-27 22:47:20 +00:00
|
|
|
workerGroupsWithActiveWorkers := make([]*WorkerGroupWithActiveWorkers, len(workerGroups))
|
|
|
|
for i, workerGroup := range workerGroups {
|
|
|
|
activeWorkers, err := services.GetActiveWorkers(context.Background(), workerGroup.Slug, h.temporal)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
workerGroupsWithActiveWorkers[i] = &WorkerGroupWithActiveWorkers{
|
|
|
|
WorkerGroupWithMonitors: 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")},
|
|
|
|
),
|
2024-02-27 22:47:20 +00:00
|
|
|
WorkerGroups: workerGroupsWithActiveWorkers,
|
|
|
|
WorkerGroupsLength: len(workerGroupsWithActiveWorkers),
|
2024-02-24 21:07:49 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *BaseHandler) SettingsWorkerGroupsDescribeGET(c echo.Context) error {
|
|
|
|
cc := c.(AuthenticatedContext)
|
|
|
|
|
|
|
|
slug := c.Param("slug")
|
|
|
|
|
2024-02-27 11:04:05 +00:00
|
|
|
worker, err := services.GetWorkerGroup(context.Background(), h.db, slug)
|
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-27 22:47:20 +00:00
|
|
|
activeWorkers, err := services.GetActiveWorkers(context.Background(), worker.Slug, 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"),
|
|
|
|
{
|
|
|
|
Path: fmt.Sprintf("/settings/worker-groups/%s", slug),
|
|
|
|
Title: "Describe",
|
|
|
|
Breadcrumb: worker.Name,
|
|
|
|
},
|
|
|
|
}),
|
2024-02-27 22:47:20 +00:00
|
|
|
Worker: &WorkerWithTokenAndActiveWorkers{
|
|
|
|
WorkerGroup: worker,
|
|
|
|
Token: token,
|
|
|
|
ActiveWorkers: activeWorkers,
|
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()
|
|
|
|
slug := slug.Make(c.FormValue("name"))
|
|
|
|
|
2024-02-27 11:04:05 +00:00
|
|
|
workerGroup := &models.WorkerGroup{
|
2024-02-24 21:07:49 +00:00
|
|
|
Name: c.FormValue("name"),
|
|
|
|
Slug: slug,
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Redirect(http.StatusSeeOther, fmt.Sprintf("/settings/worker-groups/%s", slug))
|
|
|
|
}
|