2024-02-15 22:47:56 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"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"
|
|
|
|
"code.tjo.space/mentos1386/zdravko/web/templates/components"
|
|
|
|
"github.com/gorilla/mux"
|
2024-02-16 12:41:18 +00:00
|
|
|
"github.com/gosimple/slug"
|
2024-02-15 22:47:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type SettingsHealthchecks struct {
|
|
|
|
*Settings
|
2024-02-16 12:07:29 +00:00
|
|
|
Healthchecks []*models.HealthcheckHttp
|
2024-02-15 22:47:56 +00:00
|
|
|
HealthchecksLength int
|
|
|
|
}
|
|
|
|
|
|
|
|
type SettingsHealthcheck struct {
|
|
|
|
*Settings
|
2024-02-16 12:07:29 +00:00
|
|
|
Healthcheck *models.HealthcheckHttp
|
2024-02-15 22:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *BaseHandler) SettingsHealthchecksGET(w http.ResponseWriter, r *http.Request, user *AuthenticatedUser) {
|
|
|
|
ts, err := template.ParseFS(templates.Templates,
|
|
|
|
"components/base.tmpl",
|
|
|
|
"components/settings.tmpl",
|
|
|
|
"pages/settings_healthchecks.tmpl",
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-16 12:07:29 +00:00
|
|
|
healthchecks, err := h.query.HealthcheckHttp.WithContext(context.Background()).Find()
|
2024-02-15 22:47:56 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ts.ExecuteTemplate(w, "base", &SettingsHealthchecks{
|
|
|
|
Settings: NewSettings(
|
|
|
|
user,
|
|
|
|
GetPageByTitle(SettingsPages, "Healthchecks"),
|
|
|
|
[]*components.Page{GetPageByTitle(SettingsPages, "Healthchecks")},
|
|
|
|
),
|
|
|
|
Healthchecks: healthchecks,
|
|
|
|
HealthchecksLength: len(healthchecks),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *BaseHandler) SettingsHealthchecksDescribeGET(w http.ResponseWriter, r *http.Request, user *AuthenticatedUser) {
|
|
|
|
vars := mux.Vars(r)
|
2024-02-16 12:41:18 +00:00
|
|
|
slug := vars["slug"]
|
2024-02-15 22:47:56 +00:00
|
|
|
|
|
|
|
ts, err := template.ParseFS(templates.Templates,
|
|
|
|
"components/base.tmpl",
|
|
|
|
"components/settings.tmpl",
|
|
|
|
"pages/settings_healthchecks_describe.tmpl",
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-16 12:41:18 +00:00
|
|
|
healthcheck, err := services.GetHealthcheckHttp(context.Background(), h.query, slug)
|
2024-02-15 22:47:56 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ts.ExecuteTemplate(w, "base", &SettingsHealthcheck{
|
|
|
|
Settings: NewSettings(
|
|
|
|
user,
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *BaseHandler) SettingsHealthchecksCreateGET(w http.ResponseWriter, r *http.Request, user *AuthenticatedUser) {
|
|
|
|
ts, err := template.ParseFS(templates.Templates,
|
|
|
|
"components/base.tmpl",
|
|
|
|
"components/settings.tmpl",
|
|
|
|
"pages/settings_healthchecks_create.tmpl",
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ts.ExecuteTemplate(w, "base", NewSettings(
|
|
|
|
user,
|
|
|
|
GetPageByTitle(SettingsPages, "Healthchecks"),
|
|
|
|
[]*components.Page{
|
|
|
|
GetPageByTitle(SettingsPages, "Healthchecks"),
|
|
|
|
GetPageByTitle(SettingsPages, "Healthchecks Create"),
|
|
|
|
},
|
|
|
|
))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *BaseHandler) SettingsHealthchecksCreatePOST(w http.ResponseWriter, r *http.Request, user *AuthenticatedUser) {
|
2024-02-16 12:07:29 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2024-02-16 12:41:18 +00:00
|
|
|
healthcheckHttp := &models.HealthcheckHttp{
|
|
|
|
Healthcheck: models.Healthcheck{
|
|
|
|
Name: r.FormValue("name"),
|
|
|
|
Slug: slug.Make(r.FormValue("name")),
|
|
|
|
Schedule: r.FormValue("schedule"),
|
|
|
|
},
|
|
|
|
Url: r.FormValue("url"),
|
|
|
|
Method: r.FormValue("method"),
|
|
|
|
}
|
|
|
|
|
2024-02-16 12:07:29 +00:00
|
|
|
err := services.CreateHealthcheckHttp(
|
|
|
|
ctx,
|
|
|
|
h.db,
|
2024-02-16 12:41:18 +00:00
|
|
|
healthcheckHttp,
|
|
|
|
)
|
2024-02-16 12:07:29 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
2024-02-16 12:41:18 +00:00
|
|
|
err = services.StartHealthcheckHttp(ctx, h.temporal, healthcheckHttp)
|
2024-02-16 12:07:29 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2024-02-15 22:47:56 +00:00
|
|
|
}
|
|
|
|
|
2024-02-15 22:55:48 +00:00
|
|
|
http.Redirect(w, r, "/settings/healthchecks", http.StatusSeeOther)
|
2024-02-15 22:47:56 +00:00
|
|
|
}
|