2024-02-15 22:47:56 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2024-02-19 10:43:43 +00:00
|
|
|
"strings"
|
2024-02-15 22:47:56 +00:00
|
|
|
|
|
|
|
"code.tjo.space/mentos1386/zdravko/internal/models"
|
2024-02-16 12:07:29 +00:00
|
|
|
"code.tjo.space/mentos1386/zdravko/internal/services"
|
2024-02-15 22:47:56 +00:00
|
|
|
"code.tjo.space/mentos1386/zdravko/web/templates/components"
|
2024-02-18 21:37:17 +00:00
|
|
|
"github.com/go-playground/validator/v10"
|
2024-02-16 12:41:18 +00:00
|
|
|
"github.com/gosimple/slug"
|
2024-02-20 10:24:04 +00:00
|
|
|
"github.com/labstack/echo/v4"
|
2024-02-15 22:47:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type SettingsHealthchecks struct {
|
|
|
|
*Settings
|
2024-02-21 09:06:54 +00:00
|
|
|
Healthchecks []*models.Healthcheck
|
2024-02-15 22:47:56 +00:00
|
|
|
HealthchecksLength int
|
|
|
|
}
|
|
|
|
|
|
|
|
type SettingsHealthcheck struct {
|
|
|
|
*Settings
|
2024-02-21 09:06:54 +00:00
|
|
|
Healthcheck *models.Healthcheck
|
2024-02-15 22:47:56 +00:00
|
|
|
}
|
|
|
|
|
2024-02-20 10:24:04 +00:00
|
|
|
func (h *BaseHandler) SettingsHealthchecksGET(c echo.Context) error {
|
|
|
|
cc := c.(AuthenticatedContext)
|
2024-02-15 22:47:56 +00:00
|
|
|
|
2024-02-21 09:06:54 +00:00
|
|
|
healthchecks, err := h.query.Healthcheck.WithContext(context.Background()).Find()
|
2024-02-15 22:47:56 +00:00
|
|
|
if err != nil {
|
2024-02-20 10:24:04 +00:00
|
|
|
return err
|
2024-02-15 22:47:56 +00:00
|
|
|
}
|
|
|
|
|
2024-02-20 10:24:04 +00:00
|
|
|
return c.Render(http.StatusOK, "settings_healthchecks.tmpl", &SettingsHealthchecks{
|
2024-02-15 22:47:56 +00:00
|
|
|
Settings: NewSettings(
|
2024-02-20 10:24:04 +00:00
|
|
|
cc.Principal.User,
|
2024-02-15 22:47:56 +00:00
|
|
|
GetPageByTitle(SettingsPages, "Healthchecks"),
|
|
|
|
[]*components.Page{GetPageByTitle(SettingsPages, "Healthchecks")},
|
|
|
|
),
|
|
|
|
Healthchecks: healthchecks,
|
|
|
|
HealthchecksLength: len(healthchecks),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-02-20 10:24:04 +00:00
|
|
|
func (h *BaseHandler) SettingsHealthchecksDescribeGET(c echo.Context) error {
|
|
|
|
cc := c.(AuthenticatedContext)
|
2024-02-15 22:47:56 +00:00
|
|
|
|
2024-02-20 10:24:04 +00:00
|
|
|
slug := c.Param("slug")
|
2024-02-15 22:47:56 +00:00
|
|
|
|
2024-02-21 09:06:54 +00:00
|
|
|
healthcheck, err := services.GetHealthcheck(context.Background(), h.query, slug)
|
2024-02-15 22:47:56 +00:00
|
|
|
if err != nil {
|
2024-02-20 10:24:04 +00:00
|
|
|
return err
|
2024-02-15 22:47:56 +00:00
|
|
|
}
|
|
|
|
|
2024-02-20 10:24:04 +00:00
|
|
|
return c.Render(http.StatusOK, "settings_healthchecks_describe.tmpl", &SettingsHealthcheck{
|
2024-02-15 22:47:56 +00:00
|
|
|
Settings: NewSettings(
|
2024-02-20 10:24:04 +00:00
|
|
|
cc.Principal.User,
|
2024-02-15 22:47:56 +00:00
|
|
|
GetPageByTitle(SettingsPages, "Healthchecks"),
|
|
|
|
[]*components.Page{
|
|
|
|
GetPageByTitle(SettingsPages, "Healthchecks"),
|
2024-02-16 12:07:29 +00:00
|
|
|
{
|
2024-02-16 12:41:18 +00:00
|
|
|
Path: fmt.Sprintf("/settings/healthchecks/%s", slug),
|
2024-02-15 22:47:56 +00:00
|
|
|
Title: "Describe",
|
|
|
|
Breadcrumb: healthcheck.Name,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
Healthcheck: healthcheck,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-02-23 21:08:17 +00:00
|
|
|
func (h *BaseHandler) SettingsHealthchecksDescribePOST(c echo.Context) error {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
slug := c.Param("slug")
|
|
|
|
|
|
|
|
healthcheck, err := services.GetHealthcheck(ctx, h.query, slug)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
update := &models.Healthcheck{
|
|
|
|
Slug: healthcheck.Slug,
|
|
|
|
Name: healthcheck.Name,
|
|
|
|
Schedule: c.FormValue("schedule"),
|
|
|
|
WorkerGroups: strings.Split(c.FormValue("workergroups"), " "),
|
|
|
|
Script: c.FormValue("script"),
|
|
|
|
}
|
|
|
|
|
|
|
|
err = validator.New(validator.WithRequiredStructEnabled()).Struct(update)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = services.UpdateHealthcheck(
|
|
|
|
ctx,
|
|
|
|
h.query,
|
|
|
|
update,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = services.CreateOrUpdateHealthcheckSchedule(ctx, h.temporal, healthcheck)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Redirect(http.StatusSeeOther, fmt.Sprintf("/settings/healthchecks/%s", slug))
|
|
|
|
}
|
|
|
|
|
2024-02-20 10:24:04 +00:00
|
|
|
func (h *BaseHandler) SettingsHealthchecksCreateGET(c echo.Context) error {
|
|
|
|
cc := c.(AuthenticatedContext)
|
2024-02-15 22:47:56 +00:00
|
|
|
|
2024-02-20 10:24:04 +00:00
|
|
|
return c.Render(http.StatusOK, "settings_healthchecks_create.tmpl", NewSettings(
|
|
|
|
cc.Principal.User,
|
2024-02-15 22:47:56 +00:00
|
|
|
GetPageByTitle(SettingsPages, "Healthchecks"),
|
|
|
|
[]*components.Page{
|
|
|
|
GetPageByTitle(SettingsPages, "Healthchecks"),
|
|
|
|
GetPageByTitle(SettingsPages, "Healthchecks Create"),
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2024-02-20 10:24:04 +00:00
|
|
|
func (h *BaseHandler) SettingsHealthchecksCreatePOST(c echo.Context) error {
|
2024-02-16 12:07:29 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2024-02-21 09:06:54 +00:00
|
|
|
healthcheckHttp := &models.Healthcheck{
|
|
|
|
Name: c.FormValue("name"),
|
|
|
|
Slug: slug.Make(c.FormValue("name")),
|
|
|
|
Schedule: c.FormValue("schedule"),
|
2024-02-23 13:56:00 +00:00
|
|
|
WorkerGroups: strings.Split(c.FormValue("workergroups"), " "),
|
2024-02-21 09:06:54 +00:00
|
|
|
Script: c.FormValue("script"),
|
2024-02-16 12:41:18 +00:00
|
|
|
}
|
|
|
|
|
2024-02-18 21:37:17 +00:00
|
|
|
err := validator.New(validator.WithRequiredStructEnabled()).Struct(healthcheckHttp)
|
|
|
|
if err != nil {
|
2024-02-20 10:24:04 +00:00
|
|
|
return err
|
2024-02-18 21:37:17 +00:00
|
|
|
}
|
|
|
|
|
2024-02-21 09:06:54 +00:00
|
|
|
err = services.CreateHealthcheck(
|
2024-02-16 12:07:29 +00:00
|
|
|
ctx,
|
2024-02-23 21:08:17 +00:00
|
|
|
h.query,
|
2024-02-16 12:41:18 +00:00
|
|
|
healthcheckHttp,
|
|
|
|
)
|
2024-02-16 12:07:29 +00:00
|
|
|
if err != nil {
|
2024-02-20 10:24:04 +00:00
|
|
|
return err
|
2024-02-16 12:07:29 +00:00
|
|
|
}
|
|
|
|
|
2024-02-23 21:08:17 +00:00
|
|
|
err = services.CreateOrUpdateHealthcheckSchedule(ctx, h.temporal, healthcheckHttp)
|
2024-02-16 12:07:29 +00:00
|
|
|
if err != nil {
|
2024-02-20 10:24:04 +00:00
|
|
|
return err
|
2024-02-15 22:47:56 +00:00
|
|
|
}
|
|
|
|
|
2024-02-20 10:24:04 +00:00
|
|
|
return c.Redirect(http.StatusSeeOther, "/settings/healthchecks")
|
2024-02-15 22:47:56 +00:00
|
|
|
}
|