2024-02-19 09:09:30 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2024-02-20 10:24:04 +00:00
|
|
|
"context"
|
2024-02-27 11:04:05 +00:00
|
|
|
"database/sql"
|
2024-02-23 11:18:02 +00:00
|
|
|
"errors"
|
2024-02-19 09:09:30 +00:00
|
|
|
"net/http"
|
2024-02-20 10:24:04 +00:00
|
|
|
|
2024-02-27 11:04:05 +00:00
|
|
|
"code.tjo.space/mentos1386/zdravko/database/models"
|
2024-02-20 10:24:04 +00:00
|
|
|
"code.tjo.space/mentos1386/zdravko/internal/services"
|
2024-02-21 22:15:21 +00:00
|
|
|
"code.tjo.space/mentos1386/zdravko/pkg/api"
|
2024-02-20 10:24:04 +00:00
|
|
|
"github.com/labstack/echo/v4"
|
2024-02-19 09:09:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ApiV1WorkersConnectGETResponse struct {
|
|
|
|
Endpoint string `json:"endpoint"`
|
|
|
|
Group string `json:"group"`
|
|
|
|
}
|
|
|
|
|
2024-02-20 10:24:04 +00:00
|
|
|
func (h *BaseHandler) ApiV1WorkersConnectGET(c echo.Context) error {
|
2024-02-23 11:18:02 +00:00
|
|
|
ctx := context.Background()
|
2024-02-20 10:24:04 +00:00
|
|
|
cc := c.(AuthenticatedContext)
|
2024-02-19 09:09:30 +00:00
|
|
|
|
2024-02-27 11:04:05 +00:00
|
|
|
workerGroup, err := services.GetWorkerGroup(ctx, h.db, cc.Principal.Worker.Group)
|
2024-02-23 11:18:02 +00:00
|
|
|
if err != nil {
|
2024-02-27 11:04:05 +00:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2024-02-23 11:18:02 +00:00
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Token invalid")
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-19 09:09:30 +00:00
|
|
|
response := ApiV1WorkersConnectGETResponse{
|
|
|
|
Endpoint: h.config.Temporal.ServerHost,
|
2024-02-24 21:07:49 +00:00
|
|
|
Group: workerGroup.Slug,
|
2024-02-19 09:09:30 +00:00
|
|
|
}
|
|
|
|
|
2024-02-20 10:24:04 +00:00
|
|
|
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.
|
2024-02-24 11:57:53 +00:00
|
|
|
func (h *BaseHandler) ApiV1MonitorsHistoryPOST(c echo.Context) error {
|
2024-02-20 10:24:04 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
slug := c.Param("slug")
|
|
|
|
|
2024-02-24 11:57:53 +00:00
|
|
|
var body api.ApiV1MonitorsHistoryPOSTBody
|
2024-02-23 14:16:51 +00:00
|
|
|
err := (&echo.DefaultBinder{}).BindBody(c, &body)
|
2024-02-21 22:15:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-27 11:04:05 +00:00
|
|
|
_, err = services.GetMonitor(ctx, h.db, slug)
|
2024-02-19 09:09:30 +00:00
|
|
|
if err != nil {
|
2024-02-27 11:04:05 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return echo.NewHTTPError(http.StatusNotFound, "Monitor not found")
|
|
|
|
}
|
2024-02-20 10:24:04 +00:00
|
|
|
return err
|
2024-02-19 09:09:30 +00:00
|
|
|
}
|
|
|
|
|
2024-02-27 11:04:05 +00:00
|
|
|
err = services.AddHistoryForMonitor(ctx, h.db, &models.MonitorHistory{
|
|
|
|
MonitorSlug: slug,
|
|
|
|
Status: body.Status,
|
|
|
|
Note: body.Note,
|
|
|
|
})
|
2024-02-19 09:09:30 +00:00
|
|
|
if err != nil {
|
2024-02-20 10:24:04 +00:00
|
|
|
return err
|
2024-02-19 09:09:30 +00:00
|
|
|
}
|
2024-02-20 10:24:04 +00:00
|
|
|
|
|
|
|
return c.JSON(http.StatusCreated, map[string]string{"status": "ok"})
|
2024-02-19 09:09:30 +00:00
|
|
|
}
|