github.com/erda-project/erda-infra@v1.0.10-0.20240327085753-f3a249292aeb/pkg/transport/http/handle.go (about)

     1  // Copyright (c) 2021 Terminus, Inc.
     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  package http
    16  
    17  import (
    18  	"context"
    19  	"net/http"
    20  
    21  	"github.com/erda-project/erda-infra/pkg/transport/interceptor"
    22  )
    23  
    24  const (
    25  	// SupportPackageIsVersion1 These constants should not be referenced from any other code.
    26  	SupportPackageIsVersion1 = true
    27  )
    28  
    29  type (
    30  	// DecodeRequestFunc is decode request func.
    31  	DecodeRequestFunc func(*http.Request, interface{}) error
    32  	// EncodeResponseFunc is encode response func.
    33  	EncodeResponseFunc func(http.ResponseWriter, *http.Request, interface{}) error
    34  	// EncodeErrorFunc is encode error func.
    35  	EncodeErrorFunc func(http.ResponseWriter, *http.Request, error)
    36  	// Interceptor is the func to intercept request or response
    37  	Interceptor func(h http.HandlerFunc) http.HandlerFunc
    38  	// HandleOption is handle option.
    39  	HandleOption func(*HandleOptions)
    40  	// HandleOptions is handle options.
    41  	HandleOptions struct {
    42  		Decode          DecodeRequestFunc
    43  		Encode          EncodeResponseFunc
    44  		Error           EncodeErrorFunc
    45  		Interceptor     interceptor.Interceptor
    46  		HTTPInterceptor Interceptor
    47  	}
    48  )
    49  
    50  // WithInterceptor .
    51  func WithInterceptor(o interceptor.Interceptor) HandleOption {
    52  	return func(opts *HandleOptions) {
    53  		if opts.Interceptor != nil {
    54  			opts.Interceptor = interceptor.Chain(opts.Interceptor, o)
    55  		} else {
    56  			opts.Interceptor = o
    57  		}
    58  	}
    59  }
    60  
    61  // WithHTTPInterceptor .
    62  func WithHTTPInterceptor(i Interceptor) HandleOption {
    63  	return func(opts *HandleOptions) {
    64  		if opts.HTTPInterceptor != nil {
    65  			inter := opts.HTTPInterceptor
    66  			opts.HTTPInterceptor = func(h http.HandlerFunc) http.HandlerFunc {
    67  				h = i(h)
    68  				return inter(h)
    69  			}
    70  		} else {
    71  			opts.HTTPInterceptor = i
    72  		}
    73  	}
    74  }
    75  
    76  // WithDecoder .
    77  func WithDecoder(o DecodeRequestFunc) HandleOption {
    78  	return func(opts *HandleOptions) {
    79  		opts.Decode = o
    80  	}
    81  }
    82  
    83  // WithEncoder .
    84  func WithEncoder(o EncodeResponseFunc) HandleOption {
    85  	return func(opts *HandleOptions) {
    86  		opts.Encode = o
    87  	}
    88  }
    89  
    90  // WithErrorEncoder .
    91  func WithErrorEncoder(o EncodeErrorFunc) HandleOption {
    92  	return func(opts *HandleOptions) {
    93  		opts.Error = o
    94  	}
    95  }
    96  
    97  // HandlerFunc .
    98  type HandlerFunc func(http.ResponseWriter, *http.Request)
    99  
   100  // Router .
   101  type Router interface {
   102  	Add(method, path string, handler HandlerFunc)
   103  }
   104  
   105  // RouterFunc .
   106  type RouterFunc func(method, path string, handler HandlerFunc)
   107  
   108  // Add .
   109  func (fn RouterFunc) Add(method, path string, handler HandlerFunc) {
   110  	fn(method, path, handler)
   111  }
   112  
   113  type requestContextKey int8
   114  
   115  // RequestContextKey .
   116  const RequestContextKey = requestContextKey(0)
   117  
   118  // WithRequest .
   119  func WithRequest(ctx context.Context, req *http.Request) context.Context {
   120  	return context.WithValue(ctx, RequestContextKey, req)
   121  }
   122  
   123  // ContextRequest .
   124  func ContextRequest(ctx context.Context) *http.Request {
   125  	req, _ := ctx.Value(RequestContextKey).(*http.Request)
   126  	return req
   127  }