github.com/cs3org/reva/v2@v2.27.7/internal/http/interceptors/requestid/requestid.go (about)

     1  // Copyright 2018-2021 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package requestid
    20  
    21  import (
    22  	"net/http"
    23  
    24  	"github.com/cs3org/reva/v2/pkg/rhttp/global"
    25  	"github.com/go-chi/chi/v5/middleware"
    26  )
    27  
    28  const (
    29  	defaultPriority = 100
    30  )
    31  
    32  func init() {
    33  	global.RegisterMiddleware("requestid", New)
    34  }
    35  
    36  // New returns a new HTTP middleware that adds the X-Request-ID to the context
    37  func New(m map[string]interface{}) (global.Middleware, int, error) {
    38  	rh := requestIDHandler{}
    39  	return rh.handler, defaultPriority, nil
    40  }
    41  
    42  type requestIDHandler struct {
    43  	h http.Handler
    44  }
    45  
    46  // handler is a request id middleware
    47  func (rh requestIDHandler) handler(h http.Handler) http.Handler {
    48  	rh.h = middleware.RequestID(h)
    49  	return rh
    50  }
    51  
    52  func (rh requestIDHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    53  	rh.h.ServeHTTP(w, r)
    54  }