-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_write.go
66 lines (56 loc) · 1.57 KB
/
api_write.go
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
package main
import (
"fmt"
"log/slog"
"net/http"
)
func api_write(w http.ResponseWriter, req *http.Request) {
driver_name := req.PathValue("driver")
tag_name := req.PathValue("tag")
defer req.Body.Close()
if driver_name == "" {
w.WriteHeader(http.StatusBadRequest)
_, err := w.Write([]byte("No driver name specified."))
if err != nil {
slog.Error("failed to write bad driver msg to %s: %w", req.RemoteAddr, err)
}
return
}
if tag_name == "" {
w.WriteHeader(http.StatusBadRequest)
_, err := w.Write([]byte("No tag name specified."))
if err != nil {
slog.Error("failed to write bad tagname msg to %s: %w", req.RemoteAddr, err)
}
return
}
driver, ok := drivers[driver_name]
if !ok {
w.WriteHeader(http.StatusNotFound)
_, err := w.Write([]byte(fmt.Sprintf("driver %s was not found", driver_name)))
if err != nil {
slog.Error("failed to write unknown driver name msg to %s: %w", req.RemoteAddr, err)
}
return
}
err := req.ParseForm()
if err != nil {
w.WriteHeader(http.StatusBadRequest)
_, err := w.Write([]byte("No value specified."))
if err != nil {
slog.Error("failed to write bad tag value to %s: %w", req.RemoteAddr, err)
}
return
}
val := req.FormValue("value")
err = driver.Write(tag_name, val)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, err := w.Write([]byte(fmt.Sprintf("error writing %s/%s = %v: %v", driver_name, tag_name, val, err)))
if err != nil {
slog.Error("failed to write unknown driver name msg to %s: %w", req.RemoteAddr, err)
}
return
}
w.WriteHeader(http.StatusOK)
}