github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/httptransport/context.go (about)

     1  package httptransport
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"os"
     7  
     8  	"github.com/machinefi/w3bstream/pkg/depends/base/consts"
     9  	"github.com/machinefi/w3bstream/pkg/depends/kit/kit"
    10  	"github.com/machinefi/w3bstream/pkg/depends/x/contextx"
    11  )
    12  
    13  // TODO move to conf
    14  
    15  type ServiceMeta struct {
    16  	Name    string
    17  	Version string
    18  }
    19  
    20  func (s *ServiceMeta) SetDefault() {
    21  	if s.Name == "" {
    22  		s.Name = os.Getenv(consts.EnvProjectName)
    23  	}
    24  	if s.Version == "" {
    25  		s.Version = os.Getenv(consts.EnvProjectVersion)
    26  	}
    27  }
    28  
    29  func (s ServiceMeta) String() string {
    30  	if s.Version == "" {
    31  		return s.Name
    32  	}
    33  	return s.Name + "@" + s.Version
    34  }
    35  
    36  type (
    37  	keyHttpRequest     struct{} // keyHttpRequest pass original *http.Request
    38  	keyServiceMeta     struct{} // keyServiceMeta pass service meta
    39  	keyRouteMetaID     struct{}
    40  	ckOperatorFactory  struct{}
    41  	ckQueryInBoyForGet struct{}
    42  	ckForRequestOut    struct{}
    43  )
    44  
    45  func ContextWithHttpRequest(ctx context.Context, req *http.Request) context.Context {
    46  	return contextx.WithValue(ctx, keyHttpRequest{}, req)
    47  }
    48  
    49  func HttpRequestFromContext(ctx context.Context) *http.Request {
    50  	r, _ := ctx.Value(keyHttpRequest{}).(*http.Request)
    51  	return r
    52  }
    53  
    54  func ContextWithServiceMeta(ctx context.Context, meta ServiceMeta) context.Context {
    55  	return contextx.WithValue(ctx, keyServiceMeta{}, meta)
    56  }
    57  
    58  func ServiceMetaFromContext(ctx context.Context) ServiceMeta {
    59  	r, _ := ctx.Value(keyServiceMeta{}).(ServiceMeta)
    60  	return r
    61  }
    62  
    63  func ContextWithRouteMetaID(ctx context.Context, id string) context.Context {
    64  	return contextx.WithValue(ctx, keyRouteMetaID{}, id)
    65  }
    66  
    67  func OperationIDFromContext(ctx context.Context) string {
    68  	r, _ := ctx.Value(keyRouteMetaID{}).(string)
    69  	return r
    70  }
    71  
    72  func ContextWithOperatorFactory(ctx context.Context, factory *kit.OperatorFactory) context.Context {
    73  	return contextx.WithValue(ctx, ckOperatorFactory{}, factory)
    74  }
    75  
    76  func OperatorFactoryFromContext(ctx context.Context) *kit.OperatorFactory {
    77  	r, _ := ctx.Value(ckOperatorFactory{}).(*kit.OperatorFactory)
    78  	return r
    79  }
    80  
    81  func EnableQueryInBodyForGet(ctx context.Context) context.Context {
    82  	return contextx.WithValue(ctx, ckQueryInBoyForGet{}, true)
    83  }
    84  
    85  func ShouldQueryInBodyForGet(ctx context.Context) bool {
    86  	v, ok := ctx.Value(ckQueryInBoyForGet{}).(bool)
    87  	return ok && v
    88  }
    89  
    90  func AsRequestOut(ctx context.Context) context.Context {
    91  	return contextx.WithValue(ctx, ckForRequestOut{}, true)
    92  }
    93  
    94  func IsRequestOut(ctx context.Context) bool {
    95  	if ctx == nil {
    96  		return false
    97  	}
    98  	if t, ok := ctx.Value(ckForRequestOut{}).(bool); ok {
    99  		return t
   100  	}
   101  	return false
   102  }