blob: 55022f33fb0aec6f73f3ef16829aab06049c70c0 (
plain) (
blame)
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
|
package main
import (
"fmt"
"github.com/sam-rba/share"
"log"
"net/http"
"strconv"
)
const (
minDutyCycle = 0.0
maxDutyCycle = 100.0
)
type DutyCycle float32
type DutyCycleHandler struct {
dc share.Val[DutyCycle]
}
func (h DutyCycleHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Println(r.Method, r.URL)
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
fmt.Fprintf(w, "invalid method: '%s'", r.Method)
return
}
dc, err := strconv.ParseFloat(r.URL.RawQuery, 32)
if err != nil || !isValidDutyCycle(dc) {
badRequest(w, "invalid duty cycle: '%s'", r.URL.RawQuery)
return
}
h.dc.Set <- DutyCycle(dc)
}
func isValidDutyCycle(dc float64) bool {
return dc >= minDutyCycle && dc <= maxDutyCycle
}
|