github.com/infraboard/keyauth@v0.8.1/apps/endpoint/endpoint_ext.go (about)

     1  package endpoint
     2  
     3  import (
     4  	"fmt"
     5  	"hash/fnv"
     6  	"strings"
     7  
     8  	http "github.com/infraboard/mcube/pb/http"
     9  	"github.com/infraboard/mcube/types/ftime"
    10  )
    11  
    12  // NewDefaultEndpoint todo
    13  func NewDefaultEndpoint() *Endpoint {
    14  	return &Endpoint{}
    15  }
    16  
    17  // NewEndpoint todo
    18  func NewEndpoint(serviceID, version string, entry http.Entry) *Endpoint {
    19  	return &Endpoint{
    20  		Id:        GenHashID(serviceID, entry.Path),
    21  		CreateAt:  ftime.Now().Timestamp(),
    22  		UpdateAt:  ftime.Now().Timestamp(),
    23  		ServiceId: serviceID,
    24  		Version:   version,
    25  		Entry:     &entry,
    26  	}
    27  }
    28  
    29  // GenHashID hash id
    30  func GenHashID(service, grpcPath string) string {
    31  	hashedStr := fmt.Sprintf("%s-%s", service, grpcPath)
    32  	h := fnv.New32a()
    33  	h.Write([]byte(hashedStr))
    34  	return fmt.Sprintf("%x", h.Sum32())
    35  }
    36  
    37  // LabelsToStr 扁平化标签  action:get;action:list;action-list-echo
    38  func (e *Endpoint) LabelsToStr() string {
    39  	labels := make([]string, 0, len(e.Entry.Labels))
    40  	for k, v := range e.Entry.Labels {
    41  		labels = append(labels, fmt.Sprintf("%s:%s;", k, v))
    42  	}
    43  	return strings.Join(labels, "")
    44  }
    45  
    46  // ParseLabels 解析Str格式的label
    47  func (e *Endpoint) ParseLabels(labels string) error {
    48  	kvs := strings.Split(strings.TrimSuffix(labels, ";"), ";")
    49  	for _, kv := range kvs {
    50  		kvItem := strings.Split(kv, ":")
    51  		if len(kvItem) != 2 {
    52  			return fmt.Errorf("labels format error, format: k:v;k:v;")
    53  		}
    54  		e.Entry.Labels[kvItem[0]] = kvItem[1]
    55  	}
    56  	return nil
    57  }
    58  
    59  // NewEndpointSet 实例化
    60  func NewEndpointSet() *Set {
    61  	return &Set{
    62  		Items: []*Endpoint{},
    63  	}
    64  }
    65  
    66  // Add 添加
    67  func (s *Set) Add(e *Endpoint) {
    68  	s.Items = append(s.Items, e)
    69  }
    70  
    71  // UpdatePath todo
    72  func (r *Resource) UpdatePath(path string) {
    73  	for _, p := range r.Paths {
    74  		if p == path {
    75  			return
    76  		}
    77  	}
    78  
    79  	r.Paths = append(r.Paths, path)
    80  }
    81  
    82  // UpdateMethod todo
    83  func (r *Resource) UpdateMethod(mothod string) {
    84  	for _, p := range r.Methods {
    85  		if p == mothod {
    86  			return
    87  		}
    88  	}
    89  
    90  	r.Methods = append(r.Methods, mothod)
    91  }
    92  
    93  // UpdateFunction todo
    94  func (r *Resource) UpdateFunction(fuction string) {
    95  	for _, p := range r.Functions {
    96  		if p == fuction {
    97  			return
    98  		}
    99  	}
   100  
   101  	r.Functions = append(r.Functions, fuction)
   102  }
   103  
   104  // UpdateAction todo
   105  func (r *Resource) UpdateAction(action string) {
   106  	for _, p := range r.Actions {
   107  		if p == action {
   108  			return
   109  		}
   110  	}
   111  
   112  	r.Actions = append(r.Actions, action)
   113  }
   114  
   115  // NewResourceSet todo
   116  func NewResourceSet() *ResourceSet {
   117  	return &ResourceSet{
   118  		Items: []*Resource{},
   119  	}
   120  }
   121  
   122  // AddEndpointSet todo
   123  func (s *ResourceSet) AddEndpointSet(eps *Set) {
   124  	for i := range eps.Items {
   125  		s.addEndpint(eps.Items[i])
   126  	}
   127  }
   128  
   129  func (s *ResourceSet) addEndpint(ep *Endpoint) {
   130  	if ep.Entry == nil || ep.Entry.Resource == "" {
   131  		return
   132  	}
   133  
   134  	rs := s.getOrCreateResource(ep.ServiceId, ep.Entry.Resource)
   135  	rs.UpdateMethod(ep.Entry.Method)
   136  	rs.UpdatePath(ep.Entry.Path)
   137  	rs.UpdateFunction(ep.Entry.FunctionName)
   138  	if v, ok := ep.Entry.Labels["action"]; ok {
   139  		rs.UpdateAction(v)
   140  	}
   141  }
   142  
   143  func (s *ResourceSet) getOrCreateResource(serviceID, name string) *Resource {
   144  	var rs *Resource
   145  
   146  	for i := range s.Items {
   147  		rs = s.Items[i]
   148  		if rs.ServiceId == serviceID && rs.Name == name {
   149  			return rs
   150  		}
   151  	}
   152  
   153  	// 添加新resource
   154  	rs = &Resource{
   155  		ServiceId: serviceID,
   156  		Name:      name,
   157  	}
   158  	s.Items = append(s.Items, rs)
   159  	return rs
   160  }