Skip to content

Commit

Permalink
Print the addresses of clients who invoke RPC methods
Browse files Browse the repository at this point in the history
I have slightly mixed feelings about giving the raw request to the
handler like this. It makes the function signatures longer, but it's
better than hiding this data in some backchannel.
  • Loading branch information
ahamlinman committed Sep 7, 2024
1 parent 8411ff4 commit 7b43ee3
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 9 deletions.
7 changes: 5 additions & 2 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package api
import (
"encoding/json"
"errors"
"log/slog"
"net/http"

"github.com/gorilla/websocket"
Expand Down Expand Up @@ -52,18 +53,20 @@ func (h *Handler) handleConfigChannels(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(h.tuner.ChannelNames())
}

func (h *Handler) rpcStop(_ struct{}) (code int, body any) {
func (h *Handler) rpcStop(r *http.Request, _ struct{}) (code int, body any) {
slog.Info("Stopping tuner", "client", r.RemoteAddr)
if err := h.tuner.Stop(); err != nil {
return http.StatusInternalServerError, err
}
return http.StatusNoContent, nil
}

func (h *Handler) rpcTune(params struct{ ChannelName string }) (code int, body any) {
func (h *Handler) rpcTune(r *http.Request, params struct{ ChannelName string }) (code int, body any) {
if params.ChannelName == "" {
return http.StatusBadRequest, errors.New("channel name required")
}

slog.Info("Tuning to channel", "client", r.RemoteAddr, "channel", params.ChannelName)
err := h.tuner.Tune(params.ChannelName)
switch {
case errors.Is(err, tuner.ErrChannelNotFound):
Expand Down
4 changes: 2 additions & 2 deletions internal/api/rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var MaxRequestBodySize int64 = 1024
// object with an "Error" key containing the stringified error message.
// Otherwise, when the body is non-nil, the framework encodes it to JSON
// following standard json.Marshal rules.
type HandlerFunc[T any] func(params T) (code int, body any)
type HandlerFunc[T any] func(r *http.Request, params T) (code int, body any)

// HTTPHandler conveniently boxes an RPC handler function into an http.Handler,
// without requiring an explicit type argument for a HandlerFunc[T] conversion.
Expand All @@ -56,7 +56,7 @@ func (handler HandlerFunc[T]) ServeHTTP(w http.ResponseWriter, r *http.Request)
var code int
var body any
if params, err := readRPCParams[T](r); err == nil {
code, body = handler(params)
code, body = handler(r, params)
} else {
code, body = errorHTTPCode(err), err
}
Expand Down
5 changes: 2 additions & 3 deletions internal/api/rpc/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ func Example() {

mux := http.NewServeMux()
mux.Handle("/rpc/setstatus",
rpc.HTTPHandler(func(params struct{ Enabled *bool }) (code int, body any) {
rpc.HTTPHandler(func(_ *http.Request, params struct{ Enabled *bool }) (code int, body any) {
if params.Enabled == nil {
return http.StatusBadRequest, errors.New(`missing "Enabled" parameter`)
}

setEnabled(*params.Enabled)
return http.StatusNoContent, nil
}))
Expand All @@ -45,7 +44,7 @@ func TestRPC(t *testing.T) {
rpc.MaxRequestBodySize = 32
defer func() { rpc.MaxRequestBodySize = originalMaxSize }()

handler := func(_ struct{}) (code int, body any) {
handler := func(_ *http.Request, _ struct{}) (code int, body any) {
return http.StatusNoContent, nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/api/tunerstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (h *Handler) handleSocketTunerStatus(w http.ResponseWriter, r *http.Request
}

func (tsh *TunerStatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
tsh.log.Info("Connected tuner status socket")
tsh.log.Info("Connecting tuner status socket")
defer func() {
tsh.waitForCleanup()
tsh.log.Info("Disconnected tuner status socket", "error", context.Cause(tsh.ctx))
Expand Down
2 changes: 1 addition & 1 deletion internal/api/webrtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (h *Handler) handleSocketWebRTCPeer(w http.ResponseWriter, r *http.Request)
}

func (wh *WebRTCHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
wh.log.Info("Connected WebRTC socket")
wh.log.Info("Connecting WebRTC socket")
defer func() {
wh.waitForCleanup()
wh.log.Info("Disconnected WebRTC socket", "error", context.Cause(wh.ctx))
Expand Down

0 comments on commit 7b43ee3

Please sign in to comment.