zdravko/internal/handlers/api.go

72 lines
1.7 KiB
Go
Raw Normal View History

package handlers
import (
"context"
2024-02-23 11:18:02 +00:00
"errors"
"net/http"
"code.tjo.space/mentos1386/zdravko/internal/models"
"code.tjo.space/mentos1386/zdravko/internal/services"
"code.tjo.space/mentos1386/zdravko/pkg/api"
"github.com/labstack/echo/v4"
2024-02-23 11:18:02 +00:00
"gorm.io/gorm"
)
type ApiV1WorkersConnectGETResponse struct {
Endpoint string `json:"endpoint"`
Group string `json:"group"`
Slug string `json:"slug"`
}
func (h *BaseHandler) ApiV1WorkersConnectGET(c echo.Context) error {
2024-02-23 11:18:02 +00:00
ctx := context.Background()
cc := c.(AuthenticatedContext)
2024-02-23 11:18:02 +00:00
worker, err := services.GetWorker(ctx, h.query, cc.Principal.Worker.Slug)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return echo.NewHTTPError(http.StatusUnauthorized, "Token invalid")
}
return err
}
response := ApiV1WorkersConnectGETResponse{
Endpoint: h.config.Temporal.ServerHost,
2024-02-23 11:18:02 +00:00
Group: worker.Group,
Slug: worker.Slug,
}
return c.JSON(http.StatusOK, response)
}
// TODO: Can we instead get this from the Workflow outcome?
//
// To somehow listen for the outcomes and then store them automatically.
func (h *BaseHandler) ApiV1HealthchecksHistoryPOST(c echo.Context) error {
ctx := context.Background()
slug := c.Param("slug")
var body api.ApiV1HealthchecksHistoryPOSTBody
2024-02-23 14:16:51 +00:00
err := (&echo.DefaultBinder{}).BindBody(c, &body)
if err != nil {
return err
}
healthcheck, err := services.GetHealthcheck(ctx, h.query, slug)
if err != nil {
return err
}
err = h.query.Healthcheck.History.Model(healthcheck).Append(
&models.HealthcheckHistory{
Status: body.Status,
Note: body.Note,
})
if err != nil {
return err
}
return c.JSON(http.StatusCreated, map[string]string{"status": "ok"})
}