mirror of
https://github.com/syncthing/syncthing.git
synced 2024-11-16 02:18:44 -07:00
cmd/stdiscosrv: Streamline context handling
This commit is contained in:
parent
acc532fc60
commit
a80e6be353
@ -111,8 +111,6 @@ func (s *apiSrv) Serve(_ context.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var topCtx = context.Background()
|
|
||||||
|
|
||||||
func (s *apiSrv) handler(w http.ResponseWriter, req *http.Request) {
|
func (s *apiSrv) handler(w http.ResponseWriter, req *http.Request) {
|
||||||
t0 := time.Now()
|
t0 := time.Now()
|
||||||
|
|
||||||
@ -125,10 +123,10 @@ func (s *apiSrv) handler(w http.ResponseWriter, req *http.Request) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
reqID := requestID(rand.Int63())
|
reqID := requestID(rand.Int63())
|
||||||
ctx := context.WithValue(topCtx, idKey, reqID)
|
req = req.WithContext(context.WithValue(req.Context(), idKey, reqID))
|
||||||
|
|
||||||
if debug {
|
if debug {
|
||||||
log.Println(reqID, req.Method, req.URL)
|
log.Println(reqID, req.Method, req.URL, req.Proto)
|
||||||
}
|
}
|
||||||
|
|
||||||
remoteAddr := &net.TCPAddr{
|
remoteAddr := &net.TCPAddr{
|
||||||
@ -154,17 +152,17 @@ func (s *apiSrv) handler(w http.ResponseWriter, req *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch req.Method {
|
switch req.Method {
|
||||||
case "GET":
|
case http.MethodGet:
|
||||||
s.handleGET(ctx, lw, req)
|
s.handleGET(lw, req)
|
||||||
case "POST":
|
case http.MethodPost:
|
||||||
s.handlePOST(ctx, remoteAddr, lw, req)
|
s.handlePOST(remoteAddr, lw, req)
|
||||||
default:
|
default:
|
||||||
http.Error(lw, "Method Not Allowed", http.StatusMethodNotAllowed)
|
http.Error(lw, "Method Not Allowed", http.StatusMethodNotAllowed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *apiSrv) handleGET(ctx context.Context, w http.ResponseWriter, req *http.Request) {
|
func (s *apiSrv) handleGET(w http.ResponseWriter, req *http.Request) {
|
||||||
reqID := ctx.Value(idKey).(requestID)
|
reqID := req.Context().Value(idKey).(requestID)
|
||||||
|
|
||||||
deviceID, err := protocol.DeviceIDFromString(req.URL.Query().Get("device"))
|
deviceID, err := protocol.DeviceIDFromString(req.URL.Query().Get("device"))
|
||||||
if err != nil {
|
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) {
|
func (s *apiSrv) handlePOST(remoteAddr *net.TCPAddr, w http.ResponseWriter, req *http.Request) {
|
||||||
reqID := ctx.Value(idKey).(requestID)
|
reqID := req.Context().Value(idKey).(requestID)
|
||||||
|
|
||||||
rawCert, err := certificateBytes(req)
|
rawCert, err := certificateBytes(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user