diff options
author | Jakob Borg <jakob@kastelo.net> | 2023-08-30 09:36:27 +0200 |
---|---|---|
committer | Jakob Borg <jakob@kastelo.net> | 2023-08-30 09:36:27 +0200 |
commit | a80e6be3536eae7a68e60a61d0c5edc53e08e258 (patch) | |
tree | 52f8468e312529cc7eff6ded27d0bc6d76e30dcb | |
parent | acc532fc603450a83e32562e7700a92b57cd51be (diff) | |
download | syncthing-a80e6be3536eae7a68e60a61d0c5edc53e08e258.tar.gz syncthing-a80e6be3536eae7a68e60a61d0c5edc53e08e258.zip |
cmd/stdiscosrv: Streamline context handling
-rw-r--r-- | cmd/stdiscosrv/apisrv.go | 22 |
1 files changed, 10 insertions, 12 deletions
diff --git a/cmd/stdiscosrv/apisrv.go b/cmd/stdiscosrv/apisrv.go index a30d16918..56806ffb2 100644 --- a/cmd/stdiscosrv/apisrv.go +++ b/cmd/stdiscosrv/apisrv.go @@ -111,8 +111,6 @@ func (s *apiSrv) Serve(_ context.Context) error { return err } -var topCtx = context.Background() - func (s *apiSrv) handler(w http.ResponseWriter, req *http.Request) { t0 := time.Now() @@ -125,10 +123,10 @@ func (s *apiSrv) handler(w http.ResponseWriter, req *http.Request) { }() reqID := requestID(rand.Int63()) - ctx := context.WithValue(topCtx, idKey, reqID) + req = req.WithContext(context.WithValue(req.Context(), idKey, reqID)) if debug { - log.Println(reqID, req.Method, req.URL) + log.Println(reqID, req.Method, req.URL, req.Proto) } remoteAddr := &net.TCPAddr{ @@ -154,17 +152,17 @@ func (s *apiSrv) handler(w http.ResponseWriter, req *http.Request) { } switch req.Method { - case "GET": - s.handleGET(ctx, lw, req) - case "POST": - s.handlePOST(ctx, remoteAddr, lw, req) + case http.MethodGet: + s.handleGET(lw, req) + case http.MethodPost: + s.handlePOST(remoteAddr, lw, req) default: http.Error(lw, "Method Not Allowed", http.StatusMethodNotAllowed) } } -func (s *apiSrv) handleGET(ctx context.Context, w http.ResponseWriter, req *http.Request) { - reqID := ctx.Value(idKey).(requestID) +func (s *apiSrv) handleGET(w http.ResponseWriter, req *http.Request) { + reqID := req.Context().Value(idKey).(requestID) deviceID, err := protocol.DeviceIDFromString(req.URL.Query().Get("device")) if err != nil { @@ -232,8 +230,8 @@ func (s *apiSrv) handleGET(ctx context.Context, w http.ResponseWriter, req *http }) } -func (s *apiSrv) handlePOST(ctx context.Context, remoteAddr *net.TCPAddr, w http.ResponseWriter, req *http.Request) { - reqID := ctx.Value(idKey).(requestID) +func (s *apiSrv) handlePOST(remoteAddr *net.TCPAddr, w http.ResponseWriter, req *http.Request) { + reqID := req.Context().Value(idKey).(requestID) rawCert, err := certificateBytes(req) if err != nil { |