From 3685ec6c933fe0a6d22df0e41c16f13d7be075cf Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Thu, 7 Nov 2024 18:41:53 -0500 Subject: server: refactor --- server/duty.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 server/duty.go (limited to 'server/duty.go') diff --git a/server/duty.go b/server/duty.go new file mode 100644 index 0000000..ae32ecb --- /dev/null +++ b/server/duty.go @@ -0,0 +1,36 @@ +package main + +import ( + "fmt" + "log" + "net/http" + "strconv" + "sync" +) + +type DutyCycle float32 + +type DutyCycleHandler struct { + mu sync.Mutex + dc 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 { + badRequest(w, "invalid duty cycle: '%s'", r.URL.RawQuery) + return + } + + h.mu.Lock() + defer h.mu.Unlock() + h.dc = DutyCycle(dc) +} -- cgit v1.2.3