DEVELOPMENT ENVIRONMENT

~liljamo/emerwen-web

ref: 4e555b1e3309881de4969794eff19e5275430069 emerwen-web/internal/handlers/api.go -rw-r--r-- 4.9 KiB
4e555b1eJonni Liljamo feat: UI for new/patch/delete worker/target 11 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * 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())
	}
}