/*
* Copyright (C) 2024 Jonni Liljamo <jonni@liljamo.com>
*
* This file is licensed under GPL-3.0-or-later, see NOTICE and LICENSE for
* more information.
*/
package handlers
import (
"net/http"
"strconv"
"strings"
"git.src.quest/~liljamo/emerwen-proto/go/proto"
"git.src.quest/~liljamo/emerwen-proto/go/proto/shared"
"git.src.quest/~liljamo/emerwen-web/internal/client"
"github.com/gin-gonic/gin"
)
// APIPostWorker returns a gin handler for the post worker route.
func APIPostWorker(client *client.Client) gin.HandlerFunc {
return func(c *gin.Context) {
response, err := client.Client.NewWorker(client.Ctx, &proto.NewWorkerRequest{Name: c.PostForm("name")})
if err != nil {
handleClientError(c, err)
return
}
c.Header("HX-Trigger", "refresh-workers")
c.String(http.StatusOK, response.String())
}
}
// APIPatchWorker returns a gin handler for the patch worker route.
func APIPatchWorker(client *client.Client) gin.HandlerFunc {
return func(c *gin.Context) {
request := proto.PatchWorkerRequest{
WorkerId: c.PostForm("id"),
}
if newName := c.PostForm("name"); newName != "" {
request.Name = &newName
}
response, err := client.Client.PatchWorker(client.Ctx, &request)
if err != nil {
handleClientError(c, err)
return
}
c.Header("HX-Trigger", "refresh-workers")
c.String(http.StatusOK, response.String())
}
}
// APIDeleteWorker returns a gin handler for the delete worker route.
func APIDeleteWorker(client *client.Client) gin.HandlerFunc {
return func(c *gin.Context) {
response, err := client.Client.DeleteWorker(client.Ctx, &proto.DeleteWorkerRequest{WorkerId: c.Query("id")})
if err != nil {
handleClientError(c, err)
return
}
c.Header("HX-Trigger", "refresh-workers")
c.String(http.StatusOK, response.String())
}
}
// APIPostTarget returns a gin handler for the post target route.
func APIPostTarget(client *client.Client) gin.HandlerFunc {
return func(c *gin.Context) {
request := proto.NewTargetRequest{
WorkerId: c.PostForm("worker_id"),
Name: c.PostForm("name"),
Addr: c.PostForm("addr"),
}
if interval, err := strconv.Atoi(c.PostForm("interval")); err == nil {
request.Interval = int32(interval)
} else {
c.String(http.StatusBadRequest, "Bad interval.")
return
}
switch c.PostForm("method") {
case "ping":
request.Method = &proto.NewTargetRequest_Ping{}
case "get":
var okCodes []int32
if okCodesStr := c.PostForm("ok_codes"); okCodesStr != "" {
for _, codeStr := range strings.Fields(okCodesStr) {
if len(codeStr) != 3 {
c.String(http.StatusBadRequest, "Bad code.")
return
}
if code, err := strconv.Atoi(codeStr); err == nil {
okCodes = append(okCodes, int32(code))
} else {
c.String(http.StatusBadRequest, "Bad code.")
return
}
}
}
request.Method = &proto.NewTargetRequest_Get{Get: &shared.MethodGET{OkCodes: okCodes}}
default:
c.String(http.StatusBadRequest, "Bad method.")
return
}
response, err := client.Client.NewTarget(client.Ctx, &request)
if err != nil {
handleClientError(c, err)
return
}
c.Header("HX-Trigger", "refresh-workers")
c.String(http.StatusOK, response.String())
}
}
// APIPatchTarget returns a gin handler for the patch target route.
func APIPatchTarget(client *client.Client) gin.HandlerFunc {
return func(c *gin.Context) {
request := proto.PatchTargetRequest{
TargetId: c.PostForm("id"),
}
if newName := c.PostForm("name"); newName != "" {
request.Name = &newName
}
if newAddr := c.PostForm("addr"); newAddr != "" {
request.Addr = &newAddr
}
if interval, err := strconv.Atoi(c.PostForm("interval")); err == nil {
interval := int32(interval)
request.Interval = &interval
} else {
c.String(http.StatusBadRequest, "Bad interval.")
return
}
if okCodesStr := c.PostForm("ok_codes"); okCodesStr != "" {
var okCodes []int32
for _, codeStr := range strings.Fields(okCodesStr) {
if len(codeStr) != 3 {
c.String(http.StatusBadRequest, "Bad code.")
return
}
if code, err := strconv.Atoi(codeStr); err == nil {
okCodes = append(okCodes, int32(code))
} else {
c.String(http.StatusBadRequest, "Bad code.")
return
}
}
request.Method = &proto.PatchTargetRequest_Get{Get: &shared.MethodGET{OkCodes: okCodes}}
}
response, err := client.Client.PatchTarget(client.Ctx, &request)
if err != nil {
handleClientError(c, err)
return
}
c.Header("HX-Trigger", "refresh-workers")
c.String(http.StatusOK, response.String())
}
}
// APIDeleteTarget returns a gin handler for the delete target route.
func APIDeleteTarget(client *client.Client) gin.HandlerFunc {
return func(c *gin.Context) {
response, err := client.Client.DeleteTarget(client.Ctx, &proto.DeleteTargetRequest{TargetId: c.Query("id")})
if err != nil {
handleClientError(c, err)
return
}
c.Header("HX-Trigger", "refresh-workers")
c.String(http.StatusOK, response.String())
}
}